Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 2 of 2 total
Thread Using Free in With frees main form instead of object
Thu, May 24 2012 12:01 AMPermanent Link

Rick

Tim

Consider the following example code:

procedure TForm1.Button1Click(Sender: TObject);
begin
with TPanel.Create(Self) do
begin
 Width:=100;
 Height:=100;
 Free;
end;
end;

I expected the Free to apply to the newly created TPanel via the "with" but
it seems to free the main form when the application starts. The main form
isn't even displayed and there is no chance to click the button.

It doesn't matter if the "with" object is newly created or pre-existing.

--
Rick

Tue, Jul 3 2012 3:05 PMPermanent Link

Rick

"Rick" <NOSPAMradwal@NOSPAMncable.net.au> wrote in message
news:5F251E43-8898-4A83-9A12-2E2210C455A7@news.elevatesoft.com...
> procedure TForm1.Button1Click(Sender: TObject);
> begin
> with TPanel.Create(Self) do
> begin
>  Width:=100;
>  Height:=100;
>  Free;
> end;
> end;
>
> I expected the Free to apply to the newly created TPanel via the "with"
> but it seems to free the main form when the application starts. The main
> form isn't even displayed and there is no chance to click the button.
>
> It doesn't matter if the "with" object is newly created or pre-existing.
>
>

The July 3 build gives a compile time error if using Free in With for a
newly created object. It requires an instance variable to work properly.
This is better than a blank screen when the application starts.

Free in With is also working correctly for pre-existing objects.

Problem solved, thanks.

--
Rick

Image