i'm not using interfaces (so objects has no reference counting). objects may referenced many others, , need deal dangling pointers. freeandnil() doesn't solves problem multiple references. need when object destroyed pointers references setted nil automatically. alternatively expired() method of std::weak_ptr in c++.
i implement "weak-smart pointer" this, i'm not sure if over-complicated implementation. suggest solution?. untested possible solution i'm thinking:
type tweakreferenceable = class constructor create(); destructor destroy(); override; //set referencedobject:=nil weak references in fweakreferencelist private fweakreferencelist: tlist; //list of weak references object protected procedure registerweakreference(const aweakreference: tweakreference<tweakreferenceable>); //adds weak reference procedure unregisterweakreference(const aweakreference: tweakreference<tweakreferenceable>); //removes weak reference end; type tweakreference<tobjecttype: tweakreferenceable> = class constructor create(); destructor destroy(); override; //destroys object , calls unregisterweakreference(self) referenced object private fobjectreference: tobjecttype; procedure setreference(areferencedobject: tobjecttype); //calls unregisterweakreference(self) current reference, updates fobjectreference , calls registerweakreference(self) referenced object public property referencedobject: tobjecttype read fobjectreference write setreference; end;
for mobile platforms, delphi uses arc (automatic reference counting) objects, , compiler has [weak] attribute declaring weak pointer gets nilled automatically when referenced object freed.
for desktop platforms, delphi not use arc objects. however, tcomponent has own mechanism handling "weak" references - freenotification() method. when component interested in component's lifetime, has call other component's freenotification() method. tcomponent maintains internal list of interested components. when tcomponent object freed, calls notication() method of interested components can nil references component being freed.
the code showed not using tcomponent, though. have create own registration/notification system nil references freed objects.
Comments
Post a Comment