In short, yes.
The issue is that in the first snippet, the line
type does not “own” the pointer. Destroying a line object then doesn’t deallocate the points. In the second example, we introduce our own “destructor” for the line type, but the line object returned from the constructor is a separate entity from the variable you’re about to assign it to. Thus that object gets “destroyed” before it moves on to the next line, and deallocates the points that my_line
is now pointing to.
Indeed. If you are interested in trying out something similar, @rouson put together a library: GitHub - sourceryinstitute/smart-pointers: An object-oriented, extensible reference-counting utility for Fortran