[fpc-devel] Suggestion: reference counted objects

Constantine Yannakopoulos alfasud.ti at gmail.com
Sat Sep 20 17:37:42 CEST 2014

On Sat, Sep 20, 2014 at 1:52 PM, Sven Barth <pascaldragon at googlemail.com>

> It's basically easy, yes, but then one has to deal with code like this:
> === code begin ===
> somestrlist.AddObject('Foobar', TObject(42));
> === code end ===

This sort of typecast was usually used to implement associative arrays in
Delphi versions that did not have generics. In later versions with generics
it is always better to use a TDictionary<string, TObject>, or some other
generic associative array class instead. In fact if one expects their code
to compile and run with the nextgen compiler they should refrain from using
TStringList like that altogether or they are certain to get AVs and leaks
in mobile devices -has happened to me.

The only problem would arise where this technique has been used with
TStrings properties in classes one has no control over (e.g.
TListBox.Items) that cannot be converted into TDictionary<string, TObject>
but this could be solved with a generic boxing class:

  TBox<T> = class
    FItem: T:
    constructor Create(const AItem: T);
    property Item: T read FItem write FItem;
    operator Implicit(const ABox: TBox<T>): T; // Result := ABox.FItem;
    operator Implicit(const AItem: T): TBox<T>; // Result :=


  MyListBox.Items.Add('someint', TBox<Integer>.Create(42));
  MyInteger := TBox<Integer>(MyListBox.Items.Objects['someint']); // Safe
ObjRef to ObjRef hardcast, then operator overload

In a refcounted version of Object Pascal it might make sense to prohibit
casting from a bare pointer or ordinal data type to TObject and vice-versa,
thus turning object pointers into object reference variables, like in
languages with garbage collection. This would be of great help to a person
who is converting legacy code, and it is something I miss in Delphi. In
cases where an object reference does indeed need to be turned into or from
a non refcounted variable a helper generic class could be used to perform
the typecast, something like:

  ANativeInt := TObjRefMarshal<TStream>.MarshalTo<NativeInt>(AStream);
​  AStream​ := TObjRefMarshal<TStream>.UnmarshalFrom<NativeInt>(ANativeInt);

​Regarding TObject.Free, in Delphi nextgen ​it is a nop and any calls to it
are optimized away. The object will be destroyed when the variable "o" goes
out of scope and its one and only reference is released.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20140920/92b78533/attachment.html>

More information about the fpc-devel mailing list