You can create a class that handles a list of objects and add itself to that list and free it in that same class, so that later the classes that inherit from it do not have to be freed since the list of the parent class is freeing it. .
This is neither possible in FPC as reference counting requires interfaces or managed records and cannot be build on classes alone, more is this even very useful. Such an inheritance based model would mean that this type of class would always be reference counted. What if you don't want that? Another problem is that this only works with newly created classes that do not need to inherit from anything else, it would for example not work for TStringList which already inherits from TStrings. A generic composite record like discussed here allows to make any class reference counted and also at all points individually rather than having only some classes which are always reference counted while others can't be
I find that often I just need my local variables to be freed, without reference counting. And ideally I would like not to add any more code than with arrays.
Here is a record to handle that for classes having a constructor without parameters:
Agreed (and I replied here rather than the 3+ month old topic).
There are a lot of things I don't use smart pointers for and instead use existing managed types like dynamic arrays that already do automatic cleanup.
Or advanced records rather than classes (which is what smart pointers leverage)
Also the defer mechanism I came up with that also uses advanced records.
https://forum.lazarus.freepascal.org/index.php/topic,55154.0.html
https://github.com/bogen85/fpc-defer
This is basically the dynamic C++ introduced with C++11 with their shared vs unique pointer. Shared pointer have reference counter to be used from multiple locations while unique pointer have always the lifetime of the owner (unique pointer as class member gets automatically destroyed when the class gets destroyed, as variable when the variable goes out of scope)
What's also quite useful would be scope managed stack objects, e.g. a file stream that automatically gets closed when the variable goes out of scope
var FS: TManagedFileStream;
begin
FS.open(filename);
FS.write(...);
end; // end of function automatically closes file
Or like we have arrays and strings, also other data structures like maps or sets that don't require create/destroy.
A few years ago I've build all of that using managed records, lists, sets, dictionaries, file streams and much more. But then I found out about a bug in FPC which can cause double finalize for managed records, which basically made all of this work unusable
The record types that did not rely on finalize I put in a
separate library, but all of the cool stuff sadly never worked
Probably the best solution for this would be to build it using interfaces instead. The problem here is that generic interfaces unlike records do not provide operator overloading, which has a bit less potential for some solutions