As for your problem, have a look at TRefCountedObject in LazUtils.
You need to add and remove references by hand. (i.e. always call AddReferenc or ReleaseReference whenever any instance is used).
And, you need to avoid circular references. As they will not be detected, and never be freed.
That may not be easy to achieve. You may need new classes that act as owners, and can't be owned.
There is also FreeNotifiyingObject in the same unit. So if you have backreferences, then you can use that, to make sure they get set to nil.
e.g. maybe
- a point can not own anything
- a line owns (adds references) to the points
- a surface adds references to all the lines it has
...
if a point knows about a line, then it adds a free notification. So when the Line is destroyed, it will send a notification and the point can remove it from its list.
EDIT:
Proper ownership design is a real important issue for bigger apps. Without it, never mind what you do, it will get complicated...
Btw, if you have a need where points need to know and (reference) lines, then maybe split point into the actual point, and a FooPoint that can own the point, and reference the line (but then will not be referenced by a line ever.
E.g. maybe you need to split your objects into
- the coordinate
- the actor that works with it
- user generated objects => hold explicitly user created stuff of any kind
Then within each of those groups you have a hierarchy ensuring no circles. But the outer actor group can own anything in the coordinate group. (coordinates never refer to actors).