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 delete
d 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 delete
s 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_ptr
s (still an early learner here), or am I missing something fundamental here?
Thanks.