L
LuB
I am programming in an event model - and I need to delete dynamically
allocated memory. I apologize if this question is to Windows specific -
but I think it has more to do with alloc and deallocating heap based
mem.
Imagine if you will, a parent window - creating a child window
dynamically.
When the child window is Destroyed, the child window needs to delete
itself. I don't believe the child window tells the parent when he is
about to be destroyed.
Child:
WM_DESTROY:
DeleteSelf(..)
But I'm not sure of a safe way to go about doing this. The event
handler is a member method - and I can't imagine that "delete this"
would be safe in a member method.
Is there a common idiom to do this?
Parent:
void eventhandler(..)
{
SuperDuperMdiWindow* mdiWindow = new SuperDuperMdiWindow();
}
Child
void eventhandler(..)
{
WM_DESTROY:
delete this;
// or
PostMessage(parentHwnd, DELETE_ME_SOON, 0, (LPARAM)this);
return 0;
}
If the child Sends a message to the Parent to delete him, (Send waits
for response, synchronous) it breaks.
If the child Posts a message to the Parent to delete him, (Post does
not wait for response - async), it works.
I'm sure that is just lucky - the child must be completing his
eventhandler method before the parent has a chance to actually receive
or act on the request to delete the child. Consequently, the child is
out of his member method.
But, am I write to assume that even if I'm not using any more member
properties, its unsafe to be caught in the tail end of a member method
- whilst the member is deleted?
Suggestions? Many thanks,
-Luther
allocated memory. I apologize if this question is to Windows specific -
but I think it has more to do with alloc and deallocating heap based
mem.
Imagine if you will, a parent window - creating a child window
dynamically.
When the child window is Destroyed, the child window needs to delete
itself. I don't believe the child window tells the parent when he is
about to be destroyed.
Child:
WM_DESTROY:
DeleteSelf(..)
But I'm not sure of a safe way to go about doing this. The event
handler is a member method - and I can't imagine that "delete this"
would be safe in a member method.
Is there a common idiom to do this?
Parent:
void eventhandler(..)
{
SuperDuperMdiWindow* mdiWindow = new SuperDuperMdiWindow();
}
Child
void eventhandler(..)
{
WM_DESTROY:
delete this;
// or
PostMessage(parentHwnd, DELETE_ME_SOON, 0, (LPARAM)this);
return 0;
}
If the child Sends a message to the Parent to delete him, (Send waits
for response, synchronous) it breaks.
If the child Posts a message to the Parent to delete him, (Post does
not wait for response - async), it works.
I'm sure that is just lucky - the child must be completing his
eventhandler method before the parent has a chance to actually receive
or act on the request to delete the child. Consequently, the child is
out of his member method.
But, am I write to assume that even if I'm not using any more member
properties, its unsafe to be caught in the tail end of a member method
- whilst the member is deleted?
Suggestions? Many thanks,
-Luther