Delphi's memory management design strategies: object or interface?

Regarding Delphi's memory management, what are your development strategies?

  • What are the use cases when you prefer to create and release objects manually?
  • What are the uses of interfaces, InterfacedObjects and their link counting mechanism?

Have you identified some traps or difficulties with references to counted objects?

Thank you for sharing your experience here.

+7
memory-management design interface delphi
source share
2 answers

Whenever you exchange objects between threads, it is better to use interfaces. A shared object does not necessarily have one identifiable owner, so providing a stream that returns the last link to an interface that implements the object for free is natural. See OmniThreadLibrary for a good example of how to use interfaces both for design and for overcoming some complex ownership problems in multi-threaded code.

+7
source share

You should always choose interfaces if this is not possible due to VCL restrictions. I suspect that if interfaces were available in Delphi 1.0, VCL would have turned out very differently.

One of the few considerations is to keep track of reference loops. If A has an interface with B and B, then it has an interface with A, they will both live forever.

+1
source share

All Articles