Is there a Design Pattern for preventing dangling references?

Posted by iFreilicht on Programmers See other posts from Programmers or by iFreilicht
Published on 2014-06-05T15:06:53Z Indexed on 2014/06/05 15:36 UTC
Read the original article Hit count: 274

Filed under:
|
|
|

I was thinking about a design for custom handles. The thought is to prevent clients from copying around large objects. Now a regular handle class would probably suffice for that, but it doesn't solve the "dangling reference problem";

If a client has multiple handles of the same object and deletes the object via one of them, all the others would be invalid, but not know it, so the client could write or read parts of the memory he shouldn't have access to.

Is there a design pattern to prevent this from happening?


Two ideas:

  1. An observer-like pattern where the destructor of an object would notify all handles.

  2. "Handle handles" (does such a thing even exist?). All the handles don't really point to the object, but to another handle. When the object gets destroyed, this "master-handle" invalidates itself and therefore all that point to it.

© Programmers or respective owner

Related posts about design

Related posts about design-patterns