Support for TInterfacedPersistent

Issue #107 closed
Khalil Dowlut created an issue

I have just come across TInterfacedPersistent and it seems that this class does not support reference counting (unlike TInterfacedObject, which does do ref. counting). I would like to ask if the DI container would properly dispose of a class that descends from TInterfacedPersistent.

Comments (6)

  1. Stefan Glienke repo owner

    Can you please provide some suggestions how the container should do that? How can it know if something is not needed anymore and can be disposed when it does not do reference counting?

  2. Khalil Dowlut reporter

    My apologies, I could be mistaken about the internals of the container.

    Maybe I should rephrase the question... Assuming that the container disposes all classes upon destruction, does it free the object (object.Free) or does it null the interface (intf := nil). If it is nulling the interface then the object will not get released if it descends from TInterfacedPersistent.

    I don't recall seeing in the documentation that objects in the container must be freed by the caller. If this is the case then there is no issue.

  3. Stefan Glienke repo owner

    "Assuming that the container disposes all classes upon destruction"

    I assume you mean instances and not classes. The container only does that for components that are registered as singleton. And on their registration you can specify if the implementation does reference counting (in such case it holds them by an interface reference) or not (in that case the container holds an object reference).

    However if we are talking about the lifetime transient (which is the commonly used one) then you should care about reference counting when resolving a TInterfacedPersistent as interface because otherwise there is no easy way to prevent a memory leak.

    A DI container is not much more than a factory on steroids. Except for singletons where it holds the ownership or other special lifetimes like pooled or singletonperthread the container does not keep a reference to the things it created. It either injects them into the dependency it created them for or returns them to the caller.

    Also the statement that TInterfacePersistent "does not support reference counting" is not true. The truth is that it delegates the reference counting to an owner interface if it has one (look at its code). Now that could be used by the container to inject an owner into the FOwnerInterface to plug reference counting into a TInterfacedPersistent instance.

  4. Khalil Dowlut reporter

    Thanks for pointing out the typo about "instances".

    Looks like the DI has some features that I was not aware of. Anyway I really appreciate you taking the time to explain this feature.

  5. Log in to comment