and that's it. When there are no more users of the pointer (for example, when the container object disappears), it will get destroyed automatically. There is no magic -- this is a simple reference counting pointer, and the compiler will automatically add the release call to the object destructor, on function exits, or in any other place when the variable is no longer accessible.
I found these things extremely helpful in the modern languages without GC. They are obviously not perfect -- if you have a complex structure with a loop it will never get destroyed -- but they eliminate 99% of all possible memory leaks with a very little effort. And the good news, they have almost no overhead. They are also fully thread-safe, so they become extremely helpful if you have a multi-threaded application and you want to pass the data between the threads.
This is the simplest of things that modern non-GC languages, and this is one of the big reasons why I no longer do C, just C++. There are more advanced features, of course, but I don't think you can call a language "modern" if it does not even have the smart pointers.
You can use interfaces as class wrappers if you want referenced-counted class instances in Delphi. All arrays and strings are automatically reference-counted, and records (structs) are values that can be passed around without allocating memory (Delphi supports variable parameters, or pass-by-reference, but without requiring pointers or address-of notation).
As a Delphi application developer, you will most likely not be manually allocating/freeing memory a lot. Delphi's visual component library has a component ownership model. For example, any components/controls that you drop on a form are automatically "owned" by the form and are created/destroyed for you.
In the class definition:
In the constructor (or anywhere else in the program): and that's it. When there are no more users of the pointer (for example, when the container object disappears), it will get destroyed automatically. There is no magic -- this is a simple reference counting pointer, and the compiler will automatically add the release call to the object destructor, on function exits, or in any other place when the variable is no longer accessible.I found these things extremely helpful in the modern languages without GC. They are obviously not perfect -- if you have a complex structure with a loop it will never get destroyed -- but they eliminate 99% of all possible memory leaks with a very little effort. And the good news, they have almost no overhead. They are also fully thread-safe, so they become extremely helpful if you have a multi-threaded application and you want to pass the data between the threads.
This is the simplest of things that modern non-GC languages, and this is one of the big reasons why I no longer do C, just C++. There are more advanced features, of course, but I don't think you can call a language "modern" if it does not even have the smart pointers.