Board index » cppbuilder » Re: Error on EventHandler ???

Re: Error on EventHandler ???


2003-11-27 04:45:46 AM
cppbuilder77
"Mauro" < XXXX@XXXXX.COM >wrote:
Quote
In the OnClick routine there is a function that deletes this
button... Can it be that this causes my sporadical access
errors??
YES !!
Quote
how can I solve such a problem ???
First make the button invisible so that it looks to the user
like it's been deleted. Then use a timer to actually delete it.
~ JD
 
 

Re:Re: Error on EventHandler ???

I have a button with an OnClick event.
The button is created dynamically and the OnClick routine is assigned in
runtime.
In the OnClick routine there is a function that deletes this button...
Can it be that this causes my sporadical access errors?? Because after the
function has been executed,
the program is coming to my OnClick routine back and then back to his caller
?
And in my case this caller does not more exists, because I have deleted it.
If my assumptions are right, how can I solve such a problem ???
Thanks again for any hints...
Mauro (Soccer Manager Developer :-))
 

Re:Re: Error on EventHandler ???

"JD" < XXXX@XXXXX.COM >wrote in message
Quote
Then use a timer to actually delete it.
Alternatively, you can post a custom message to the form and let the form
handle the message to delete the button when convenient, when the button is
not being used anymore. A little bit more flexibl then using a timer. But
either will work. The trick is to delay the deletion until after the code
is done using the button, yes.
Gambit
 

{smallsort}

Re:Re: Error on EventHandler ???

JD, what is with timers theyself ???
I have a timer that calls every one second a timer event.
In the last call to this OnTimer event I disable my timer and
call a routine to delete the timer. Will it also causes problems ??
I mean no, because the OnTimer event goes not back to the
timer component as this is case with a button, I'm right??
Regards,
Mauro
"JD" < XXXX@XXXXX.COM >wrote:
Quote

"Mauro" < XXXX@XXXXX.COM >wrote:
>In the OnClick routine there is a function that deletes this
>button... Can it be that this causes my sporadical access
>errors??

YES !!

>how can I solve such a problem ???

First make the button invisible so that it looks to the user
like it's been deleted. Then use a timer to actually delete it.

~ JD


 

Re:Re: Error on EventHandler ???

One more example:
I have a button and in OnClick Event I change the OnClick
property to an other OnClick Event. Can it be that the program
goes back from the current OnClick Event and crashs because I have changed the OnClick property inside ??
Thanks,
Mauro
 

Re:Re: Error on EventHandler ???

Gambit,
I cannot follow you. Could you explain a liitle bit more?
Thanks,
Marius
"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >wrote:
Quote

"JD" < XXXX@XXXXX.COM >wrote in message
news:3fc51f0a$ XXXX@XXXXX.COM ...

>Then use a timer to actually delete it.

Alternatively, you can post a custom message to the form and let the form
handle the message to delete the button when convenient, when the button is
not being used anymore. A little bit more flexibl then using a timer. But
either will work. The trick is to delay the deletion until after the code
is done using the button, yes.


Gambit


 

Re:Re: Error on EventHandler ???

"Mauro" < XXXX@XXXXX.COM >wrote:
Quote
I have a button and in OnClick Event I change the OnClick
property to an other OnClick Event.
That by it self won't cause a problem but the new OnClick
could have a flaw in it.
Quote
Can it be that the program goes back from the current
OnClick Event and crashs because I have changed the OnClick
property inside ??
It sounds like you might be trying to get too 'fancy' and you
can't track down where the program is crashing. I would
suggest to you to allocate the desired objects in the forms'
constructor and delete them in it's destructor.
It's not a big deal to let them live for the life of the form.
The performance hit is almost nothing and it would make
debugging easier.
~ JD
 

Re:Re: Error on EventHandler ???

Thanks for all the explanation, nevertheless I have the
last question regarding this problems.
I have a timer that calles every second an OnTimer Routine
and the user clicks a button in which's OnClick routine the
timer is deleted. Are there some problems ? I mean no, because
I don't delete it from the OnTimer Routine. Or should I stop
the timer in the OnClick routine first and then delete the
timer??
Thanks again for any clarifications,
Regards Marius
"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >wrote:
Quote

"Mauro" < XXXX@XXXXX.COM >wrote in message
news:3fc5a358$ XXXX@XXXXX.COM ...

>I have a timer that calls every one second a timer event.
>In the last call to this OnTimer event I disable my timer and
>call a routine to delete the timer. Will it also causes problems ??

Yes, for the same reason - the VCL is still using the component after the
event handler returns, so deleting the component prematurely may crash the
code if the VCL tries to access any of the component's members again
afterwards. It is a very bad idead to delete *any* component from inside
its own event handlers.


Gambit



 

Re:Re: Error on EventHandler ???

"Mauro" < XXXX@XXXXX.COM >wrote in message
Quote
I cannot follow you. Could you explain a liitle bit more?
--- Unit1.h ---
#define APPWM_DELETE_COMPONENT (WM_APP + 100)
#define DELETE_COMP_MAGIC_NUMBER 0xAFAFAFAF
#pragma pack(push, 1)
TWMDeleteComp
{
unsigned Msg;
unsigned MagicNum;
TComponent* Component;
int Result;
};
#pragma pack(pop)
class TForm1 : public TForm
{
__published:
void __fastcall Button1Click(TObject *Sender);
private:
void __fastcall AppDeleteComponent(TWMDeleteComp &Message);
//...
public:
//...
BEGIN_MESSAGE_MAP
VCL_MESSAGE_HANDLER(APPWM_DELETE_COMPONENT, TWMDeleteComp,
AppDeleteComponent)
END_MESSAGE_MAP(TForm)
};
--- Unit.cpp ---
void __fastcall TForm1::Button1Click(TObject *Sender)
{
//...
PostMessage(Handle, APPWM_DELETE_COMPONENT,
DELETE_COMP_MAGIC_NUMBER, reinterpret_cast<int>(Button1));
}
void __fastcall TForm1::AppDeleteComponent(TWMDeleteComp &Message)
{
if( Message.MagicNum == DELETE_COMP_MAGIC_NUMBER )
{
delete Message.Component;
Message.Result = TRUE;
}
else
TForm::Dispatch(&Message);
}
Gambit
 

Re:Re: Error on EventHandler ???

"Mauro" < XXXX@XXXXX.COM >wrote in message
Quote
I have a timer that calls every one second a timer event.
In the last call to this OnTimer event I disable my timer and
call a routine to delete the timer. Will it also causes problems ??
Yes, for the same reason - the VCL is still using the component after the
event handler returns, so deleting the component prematurely may crash the
code if the VCL tries to access any of the component's members again
afterwards. It is a very bad idead to delete *any* component from inside
its own event handlers.
Gambit
 

Re:Re: Error on EventHandler ???

"Mauro" < XXXX@XXXXX.COM >wrote in message
Quote
I have a button and in OnClick Event I change the OnClick
property to an other OnClick Event. Can it be that the program
goes back from the current OnClick Event and crashs because
I have changed the OnClick property inside ??
No. That is a safe operation.
Gambit
 

Re:Re: Error on EventHandler ???

"Mauro" < XXXX@XXXXX.COM >wrote:
Quote
[...] Or should I stop the timer in the OnClick routine
first and then delete the timer??
To safely delete the timer, you should first set it's Enabled
property to false and then wait long enough to ensure that the
timer has finished executing any code that it might be working
on. You can accomplish this using sleep and depending on what
the timer was doing, Application->ProcessMessages() will also
work for you.
~ JD
 

Re:Re: Error on EventHandler ???

"Mauro" < XXXX@XXXXX.COM >wrote in message
Quote
I have a timer that calles every second an OnTimer Routine
and the user clicks a button in which's OnClick routine the
timer is deleted. Are there some problems ?
No, because the VCL is not accessing the timer at the time that you are
inside the OnClick event handler.
Quote
Or should I stop the timer in the OnClick routine first
and then delete the timer??
Deleting the timer automatically stops the timer first.
Gambit
 

Re:Re: Error on EventHandler ???

"JD" < XXXX@XXXXX.COM >wrote in message
Quote
To safely delete the timer, you should first set it's Enabled
property to false and then wait long enough to ensure that the
timer has finished executing any code that it might be working
on.
There is no need, the timer's destructor already does that internally.
Gambit