<p>In a world where manual memory allocation and pointers still rule (Borland Delphi) I need a general solution for what I think is a general problem:</p>

<p>At a given moment an object can be referenced from multiple places (lists, other objects, ...). Is there a good way to keep track of all these references so that I can update them when the object is destroyed?
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­</p>

[{'answer_id': 18155, 'author': 'Steve', 'author_id': 2019, 'author_profile': 'https://Stackoverflow.com/users/2019', 'pm_score': 1, 'selected': False, 'text': "<p>I can't quite figure out why you'd want to do this. Surely you would just check a reference in not Nil before using it?</p>\n\n<p>Anwyays, two possible solutions I would consider are:</p>\n\n<ol>\n<li>Have objects manager their own reference counts.</li>\n<li>Create a reference counting manager class.</li>\n</ol>\n\n<p>I would probably add AddRef() and ReleaseRef() functions to either the manager or the reference-aware class. You can then use these to check how many references exist at any point. COM does it this way.</p>\n\n<p>The reference-aware class would manage only it's own reference count. The manager could use a Map to associate pointers with an integer for counting.</p>\n"}, {'answer_id': 18165, 'author': 'Herms', 'author_id': 1409, 'author_profile': 'https://Stackoverflow.com/users/1409', 'pm_score': 0, 'selected': False, 'text': '<p>Are you trying to keep track of who\'s referencing an object so you can clear those references when the object is destroyed, or are you trying to keep track of when it\'s safe to destroy the object?</p>\n\n<p>If the latter then it sounds like you\'re looking for a garbage collector.  I\'ve never dealt with Delphi so I don\'t know if there are GCs for it you can use, but I\'d be surprised if there weren\'t.</p>\n\n<p>If the former then a GC probably wouldn\'t help.  If Delphi supports OOP/inheritence (I honestly don\'t know if it does) you could do something like this (pseudocode):</p>\n\n<pre><code>// Anything that will use one of your tracked objects implements this interface\ninterface ITrackedObjectUser {\n  public void objectDestroyed(TrackedObject o);\n}\n\n// All objects you want to track extends this class\nclass TrackedObject {\n  private List&lt;ITrackedObjectUser&gt; users;\n\n  public void registerRef(ITrackedObjectUser u) {\n    users.add(u);\n  }\n\n  public void destroy() {\n    foreach(ITrackedObjectUser u in users) {\n      u.objectDestroyed(this);\n    }\n  }\n}\n</code></pre>\n\n<p>Basically, whenever you add one of your tracked objects to a collection that collection would register itself with that object.  When the object is being destroyed (I figure you\'d call destroy() in the object\'s destructor) then the object signals the collection that it\'s being destroyed so the collection can do whatever it needs to.</p>\n\n<p>Unfortunately, this isn\'t really a good solution if you want to use build-in collections.  You\'d have to write your own collection objects (they could just wrap build-in ones though).  And it would require to to make sure you\'re registering everywhere you want to track the object.  It\'s not what I would consider a "happy" solution, though for small projects it probably wouldn\'t be too bad.  I\'m mainly hoping this idea will help spawn other ideas. :)</p>\n'}, {'answer_id': 18490, 'author': 'Lars Truijens', 'author_id': 1242, 'author_profile': 'https://Stackoverflow.com/users/1242', 'pm_score': 3, 'selected': True, 'text': '<p>If you want to notify others of changes you should implement the <a href="http://en.wikipedia.org/wiki/Observer_pattern" rel="nofollow noreferrer">"Observer Pattern"</a>. Delphi has already done that for you for TComponent descendants. You can call the TComponent.FreeNotification method and have your object be notified when the other component gets destroyed. It does that by calling the Notification method. You can remove yourself from the notification list by calling TComponent.RemoveFreeNotification. Also see <a href="http://delphi.about.com/library/bluc/text/uc083101a.htm" rel="nofollow noreferrer">this page</a>.</p>\n\n<p>Most Garbage Collectors do not let you get a list of references, so they won\'t help in this case. Delphi can do reference counting if you would use interfaces, but then again you need to keep track of the references yourself.</p>\n'}, {'answer_id': 267969, 'author': 'Ferry van Genderen', 'author_id': 34987, 'author_profile': 'https://Stackoverflow.com/users/34987', 'pm_score': 0, 'selected': False, 'text': '<p>Is there a specific reason you want this? Are you running into problems with rogue pointers, or are you thinking it might be a problem one day?</p>\n\n<p>IMHO it will not be a problem if you design your application right, and using the appropriate patterns really helps you.</p>\n\n<p>Some info about patters:</p>\n\n<p><a href="http://delphi.about.com/od/oopindelphi/a/aa010201a.htm" rel="nofollow noreferrer">http://delphi.about.com/od/oopindelphi/a/aa010201a.htm</a></p>\n\n<p><a href="http://www.obsof.com/delphi_tips/pattern.html" rel="nofollow noreferrer">http://www.obsof.com/delphi_tips/pattern.html</a></p>\n'}]
['https://Stackoverflow.com/questions/18119', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']