Re: Problem freeing components with .free



TeChNoInSiDe wrote:
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?

Post a message to the form. In that message handler, free the button.

const
am_ImagesSaved = wm_App + 1;

type
TTechnoForm = class(TForm)
private
procedure AMImagesSaved(var Message: TMessage); message am_ImagesSaved;
procedure SaveButtonClick(Sender: TObject);
end;

procedure TTechnoForm.SaveButtonClick(Sender: TObject);
begin
SaveImages;
PostMessage(Handle, am_ImagesSaved, 0, 0);
end;

procedure TTechnoForm.AMImagesSaved(var Message: TMessage);
begin
SaveButton.Free;
SaveButton := nil;
end;

As a user, I find it disconcerting when a button disappears as soon as I click it. If I'm not paying very close attention, or if my finger slips, I don't have any visual cue about _what_ I just accidentally pressed.

Will the button re-appear when there are more images to edit? Would it make sense to simply disable the button instead of destroying it?

--
Rob
.


Quantcast