Re: Problem freeing components with .free



TeChNoInSiDe wrote:
Delphi help says:

FREE Method

"Never explicitly free a component within one of its own event
handlers or the event handler of a component it owns or contains. For
example, don't free a button, or the form that owns the button, in its
OnClick event handler."

----------------------------------------------------------------------------------------

In my form I have a image listing section, a save button and a
SaveImages() function associated with save button's onclick handler.

SaveImages() do some saving operations and then destroys (with .free
method) images and the save button. As said in delphi warning, i am
getting access violation after freeing the save button.
I tried to make a SaveButtonHandler for onclick handler and inside it
calling the SaveImages function but seems it has the same effect.

I created a threaded timer and activated it on savebuttonhandler and
then called saveimages() on timer event. This worked great but lead me
to another programming limitations (because of a isolated thread
process).

So, does anybody know any workaround for using .free on this
situation, without using timers?


This problem is not only bound to Delphi...

But anyways why do you want to free the Button there? Wouldn't it just be better and easier to set Enabled:=False; and Visible:=False; ?
If the Button is not Dynamically created at runtime you do not need to worry about freeing it.

You could of course instead use some kind of flag to see if the Button is not used anymore and then free the Button in the next event that might be called...
.


Quantcast