c++ and event programming

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
 
A

Alf P. Steinbach

* LuB:
WM_DESTROY:
DeleteSelf(..)


But I'm not sure of a safe way to go about doing this.

delete *this;

Don't access anything in the object on the return path from that
statement.
 
A

Alf P. Steinbach

* Alf P. Steinbach:
* LuB:

delete *this;

Don't access anything in the object on the return path from that
statement.

delete this;

of course. You may have to set a flag to avoid recursive delete.
 
L

LuB

Thanks. Sorry for the typo in my delete statement.

Just to complete the story ... initially, that threw an exception,

Unhandled exception at 0x007c4f98 in skate.exe: 0xC0000096: Privileged
instruction.

So given your response, I had to find out what I was missing. Turns
out, after WM_DESTROY, a few more events were being passed to my
"destroyed" member's handler thunk --- obviously a bad thing ;-) So in
my case, I must first subclass or redirect subsequent events for said
window to a visible function (DefMDIChildProc) ... and then I delete
the instance ... and all works just fine ;)

After the error though, I wouldn't have pursued it thinking it was just
bad to delete an object while inside one of its methods. Thanks for
reassuring me its legal.

Thats great!

Thanks Alf.

-Luther
 
H

Howard

LuB said:
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

I think the most usual method for deleting an object is to have whoever
created it do the destroying. If you need a parent window to delete a child
object, then POSTing a message to the parent window might be appropriate.
But a better place to ask would be a Windows newsgroup, where they can
discuss with you the "normal" procedures that are followed when dealing with
MDI window classes.

-Howard
 
S

Sam

LuB said:
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.
I m interested to know whether there is any C++ library for event
programming in Unix?

Sam.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,204
Messages
2,571,064
Members
47,672
Latest member
svaraho

Latest Threads

Top