Weak references – or why you should enable ReportMemoryLeaksOnShutdown » twm's blog


http://blog.dummzeuch.de/2014/06/19/weak-references-or-why-you-should-enable-reportmemoryleaksonshutdown/

Comments

  1. Imo you should declare weak interface references as Pointer and not as interface. That removes the necessity for the extra SetWeak in the destructor.

    ReplyDelete
  2. Using pointers for weak references is fraught with danger. There are several weak reference implementations out there, this is mine :

    https://github.com/VSoftTechnologies/Delphi-WeakReferences

    It's not perfect but it's in heavy use in FinalBuilder with no issues so far.

    ReplyDelete
  3. Vincent Parrett Using pointers for weak references where you don't have to check if the weak reference is still valid is a better solution than storing it as interface because you always have to manually nil it because otherwise the compiler generated finalization code will try to call _ClearIntf on it.
    While your solution might be safer and supports alive checking it is limited to be used only for interfaces that are backed by an object that implements IWeakReferenceableObject.

    ReplyDelete
  4. Agreed it's limited, there are other implementations out there but those generally involve more low level hacking of the objects.. which may or may not break with later compiler versions. Choose your poisen wisely ;)

    ReplyDelete

Post a Comment