Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling delete on two pointers to the same object

I'm having a problem with a couple of event handler classes I'm trying to write. Basically, the idea is to have an event handler class for each logical group of objects. In most cases, the events are between objects and their handler, but in some cases events are also sent between handler objects as well.

I've written the code such that the events are placed on a stack (stack as in user created structure; the events themselves are allocated using new) and deleted after their information is read and acted upon. This is giving me some problems because in one case, the event is sent along a chain of three handlers. Say, HandlerA sends a new Event to HandlerB, which places it on the stack and reads it, sending it to HandlerC, which reads it and performs whatever it needs to perform, after which it deletes the Event pointer and sets it to NULL. Now, we pop back to HandlerB and, well, it also wants to delete and NULL the pointer to the event. But the pointer is a local variable, and it ends up deleting the same address twice, giving an exception.

How do you go around this? Do you need to use one of those fancy auto_ptrs (still an early learner here), or am I missing something fundamental here?

like image 400
Kristian D'Amato Avatar asked Jul 14 '10 11:07

Kristian D'Amato


3 Answers

I've written the code such that the events are placed on a stack and deleted after their information is read and acted upon.

There is some confusion here - objects on the stack should not be deleted. Objects created with new (on the heap) should.

In general, you should define a clear ownership strategy for your objects on the heap. Each object should have one owner, and it should be clear who the owner is at any point in time. That owner - and it alone - shall delete the object.

You may also want to use boost::shared_ptr (it may be available also as std::tr1::shared_ptr, depending on your compiler) instead of raw pointers. This keeps count of the references to the object, and deletes it when the ref count drops to 0.

like image 154
Péter Török Avatar answered Sep 21 '22 07:09

Péter Török


What you want is some pointer wrapper that for instances uses reference counting to check if other variables reference the same instance. The idea is, that the object the pointer points to is only deallocated when that object is not used by any other pointer. Those kind of pointers are generally refered to as Smart Pointers. In c++ you can for instance use the ones provided by Boost.

like image 41
Janick Bernet Avatar answered Sep 21 '22 07:09

Janick Bernet


The problem as I see it is that there is no clear owner of the pointer. One solution will be smart pointers as pointed out in inflagranti's answer. Alternatively you could stop forwarding the event twice - when a handler (Handler B in your example) receives an event that it needs to forward to another handler, it creates a new event rather than passing on the pointer to the existing event.

That said; if you are wiling to spend the time looking into them I think that the Smart Pointer solution is probably better!

like image 39
Jackson Avatar answered Sep 19 '22 07:09

Jackson