Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple, efficient weak pointer that is set to NULL when target memory is deallocated

Is there a simple, efficient weak/guarded pointer? I need multiple pointers to the same object that are all automatically set to NULL when the object is deleted. There is one "master" pointer that is always used to delete the object, but there can be several other pointers that reference the same object.

Here are some solutions that don't quite match my needs:

  • QPointer: I am not developing a QT app; I do not wish to include this libary/derive from QObject.
  • boost::weak_ptr: an exception is thrown when accessing a deallocated object. Too expensive for my situation: it should be normal to test a weak pointer; I plan to do some manual clean-up when a weak pointer is no longer valid. update:weak_ptr can be tested without throwing exceptions
  • Low-Overhead Weak Pointers: This is very close to what I am looking for, except I don't like the fact "This scheme is only guaranteed to work as long as you don’t allocate 2**sizeof(int) times in the same location."

Why I need these weak/guarded pointers: I have a game with a list of game objects. Some objects are dependent on others, for example a debug/stats object that is associated with a game entity. The debug/status object displays useful info about the game entity, but it only makes sense while the game entity exists. So if the game entity is deleted, the debug/stats object should realize this and delete itself. (Another idea is a tracking missile: instead of deleting itself, it may search for a new target.)

I wish to keep the debug/stats logic separate from the game entity. The game entity should not have to know a debug/stats object is attached to it. While I'd prefer an answer for weak/guarded pointers, I also welcome different ways to approach my specific task. I am thinking I may have to implement a game object manager that tracks object lifetimes and uses handles instead of raw pointers to memory addresses.

I am developing in C++.

like image 374
Leftium Avatar asked Dec 09 '09 19:12

Leftium


People also ask

What happens when you set a pointer to null?

In C pointers can be set to a value NULL which means, the pointer is not yet set or invalid. Any other value other than NULL means a valid pointer pointing to a proper memory location.

Should I set a pointer to null after delete?

But of course it is a good practice to set the pointer to NULL/nullptr after deleting as the pointer location is invalid after deletion.

What is weak PTR used for?

std::weak_ptr models temporary ownership: when an object needs to be accessed only if it exists, and it may be deleted at any time by someone else, std::weak_ptr is used to track the object, and it is converted to std::shared_ptr to assume temporary ownership.

Do you need to set pointers to null?

It is always a good practice to assign the pointer NULL to a pointer variable in case you do not have exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer.


1 Answers

You can use the lock() member of boost::weak_ptr to be able to test (then use) the value of the weak_ptr without dealing with exceptions.

like image 169
Michael Burr Avatar answered Sep 30 '22 23:09

Michael Burr