473,320 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

c++ and event programming

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

Jul 23 '05 #1
5 1595
* 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: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2
* 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.


delete this;

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

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #3
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

Jul 23 '05 #4

"LuB" <lu*********@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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
Jul 23 '05 #5
Sam
LuB wrote:
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.
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: christopher diggins | last post by:
I just wrote an article on using template specializations for event-driven programming ( http://www.artima.com/weblogs/viewpost.jsp?thread=84958 ). Are there any other examples of this kind of...
15
by: Pohihihi | last post by:
This might sound little stupid to many but I was thinking that when we can use object why we really need event args to pass in any functions e.g. bool MyFunction(object sender, System.EventArgs...
3
by: LuB | last post by:
I'm writing a Win32 application - and more specifically, doing event programming. I want the application to be const compliant but I'm faced with a bit of a conundrum. Physically, many of my...
4
by: AzizMandar | last post by:
C++ Event Coding Questions I have done some simple programs in C++ and read a lot of good C++ books (Including The C++ Programing Language, and C++ Primer) I am trying to understand and...
4
by: LyzH | last post by:
Someone else had a question on how to emulate a mouse click. I tried posting in that thread but I have something of a twist on this problem and I'm really in trouble here! If I don't get help...
4
by: reggiestyles | last post by:
Hi, I've got a question about prototype and event handling. I've got several div's (dynamic number) on a page that I want to set as active or inactive (basically, I'm using scriptaculous'...
4
by: Jonathan Wood | last post by:
I'm building a Web application but this question should be common to all C# applications. When I use a class, and I want to add event handlers or override base class methods, how do I know the...
0
by: shonen | last post by:
I'm currently attempting to connect to a shoutcast server pull down the information from here and then I'll parse it. I got this working with the httplib, which was great, the problem is I want...
2
by: John Kotuby | last post by:
Hi guys, I am converting a rather complicated database driven Web application from classic ASP to ASP.NET 2.0 using VB 2005 as the programming language. The original ASP application works quite...
0
MMcCarthy
by: MMcCarthy | last post by:
VBA is described as an Event driven programming language. What is meant by this? Access, like most Windows programs, is an event driven application. This means that nothing happens unless it is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.