A while ago, there was a nice discussion on smart pointers at [WayBack] Smart Pointers – Generics vrs non-generic implementastion – RTL and Delphi Object Pascal – Delphi-PRAXiS [en].
Conclusion from that:
- many people think that reference counted interfaces are the same as Smart Pointers
(basically Smart Pointers are the next level and of course they are based on reference counting) - there are a lot of Smart Pointer implementations, but few have a test suite, nor are optimised , nor easy to use
- The combo
Shared
/IShared<T>
/TShared<T>
from Spring4D has all of the above advantages - in order to optmise Smart Pointer implementations, you really have to well know the effects of modern Delphi language constructs on the compiler in various target platforms
The discussion mentioned above includes both feature and speed comparisons.
I was a bit amazed that at CodeRage 2018, Marco Cantu introduced yet another smart pointer implementation: one worse than existing implementations, and one with only basic demonstration code, leaving out a test suite.
There have many posts on my blog about smart pointers (see the list below), but Spring4D smart pointer implementation has been around for such a long time that any well respected Delphi developer by now should use them. The source is at Shared/IShared (search for {$REGION 'Shared smart pointer'}
at the current repository).
This list below on my Smart Pointer related blog posts might not be fully complete, but at least mentions that by now you should be using Spring4D.
- Delphi: using IInterface to restore cursor at end of mehod (prelude to a memento that executes any code at end of method). which mentions Smart Pointers as the next level of such
IInterface
usage. - Some great reads from Barry Kelly, which lists a series of great posts that document compiler internals, and explains why they cannot be changed because so much code relies on them
- If you were using Managed / IManaged in Spring4D, be aware they got renamed to Shared / IShared
- How and when are variables referenced in Delphi’s anonymous methods captured? – Stack Overflow
- Spring4D – Shared, Weak references
- Smart Pointers code that compiles in FPC and Delphi
$RTTI
directive, you can use a little trick and redefine it in subclass inpublic
section asoverride; abstract;
this will cause the RTTI to be generated. – Honza RFeb 5 ’16 at 8:02