As most of you knows there are managed types in Delphi which memory is automatically managed – like strings, dynamic arrays, interfaces etc. And as well there are some non-managed types like Integer, Double, classes. Sure we have TInterfacedObject but it only releases automatically when you assign it to interface variable and it goes out of scope. But we also often use non-interfaced objects like TStream (all descendants), TStrings, TList<T>, TDictionary<K,V>.
It will be really nice to have them automatically managed as well, but that’s available only in ARC platforms (mobile). Can we get that in Win32/Win64? The partial answer is to use the Spring framework which introduces IList<T>, IDictionary<T> and many other good things which you can start using by adding Spring.Collections to your uses list and then constructing them through the TCollections.CreateList<T>, TCollections.CreateDictionary<K,V> and other calls. But that solves the problem only with the limited list of generic types…
The idea about how to create that is not new. One version is demonstrated by Delphi guru Marco Cantu in his book. The thing is that I actually did not like the name of the class he used. I wrote my implementation based on his one which is more comfortable for me. And as well I call that record Arc (automatic reference count).
So what it does? There I show you few examples:
uses uArc; var L: Arc<TList<Integer>>; begin L := TList<Integer>.Create; L.This.Add(1); L.This.Add(2); // and no need to release the list anymore! end;
uses uArc; var M: Arc<TMemoryStream>; begin M := TMemoryStream.Create; M.This.LoadFromFile('...'); // and no need to release the memory stream! end;
The full source code is available at GitHub.