Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 10 of 25 total
Thread Freeing component and subsequent Assigned checking
Tue, Jan 5 2016 12:36 PMPermanent Link

Trinione

I am using Assigned to check for the existence of a component.

After issuing a ComponentName.Free though the component no longer exists Assigned returns True.

-----------------------------------------------------
if Assigned(frm) then
 s := 'Assigned'
else
 s := 'Not Assigned';

frm := Tfrm.Create(nil);    // create form
frm.Free;    // free form

if Assigned(frm) then
 s := s + ' || Assigned'
else
 s := s + ' || Not Assigned';

ShowMessage(s);
---------------------------------------------------------

This returns 'Not Assigned || Assigned'. How can I get a 'Not Assigned' after freeing the component?
Tue, Jan 5 2016 12:50 PMPermanent Link

Trinione

In this post I am seeing Tim stated that Free works as FreeAndNil.

However, this is a message from year 2013 and EWB1.

http://www.elevatesoft.com/forums?action=view&category=ewb&id=ewb_general&msg=2191
Tue, Jan 5 2016 12:51 PMPermanent Link

Raul

Globestar Systems

Team Elevate Team Elevate

<<
Trinione wrote:

I am using Assigned to check for the existence of a component.
After issuing a ComponentName.Free though the component no longer exists Assigned returns True.

frm := Tfrm.Create(nil);    // create form
frm.Free;    // free form

>>


That would be correct - technically that just frees memory.

If you need to check for assign after freeing the object then you must assign nil value to it.

...
frm.Free;
frm := nil;


Raul

Tue, Jan 5 2016 1:11 PMPermanent Link

Trinione

Ha ha ha ha ha ha ha... I am here laughing at myself! Free And NIL... Free and NIL!!!

Thanks Raul.
Tue, Jan 5 2016 1:48 PMPermanent Link

Trinione

After getting an error and reading up on it, first dereference the object before deallocating the memory.

 frm := nil;
 frm.Free;
Tue, Jan 5 2016 2:21 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

<< After getting an error and reading up on it, first dereference the object before deallocating the memory. >>

No, that is backwards.  Calling Free will make sure that all resources owned by the object are disposed of.  Setting the variable to nil will make sure that the garbage collector in the JS engine in the browser knows that the instances is no longer referenced, and the GC will reclaim the memory in the next cleanup cycle.  If you set the reference to nil and then call Free, you're just introducing a NULL exception bug in your application that may or may not appear at runtime, depending upon when the GC gets around to cleaning up the reference.

EWB 1.x had a "magic" compiler FreeAndNil() type of operation, but I got rid of it in EWB 2.x because it didn't work well in certain constructs like when used with a "with" block.  So, in EWB 2.x, you explicitly need to do both:

MyInstance.Free;
MyInstance:=nil;

Tim Young
Elevate Software
www.elevatesoft.com
Tue, Jan 5 2016 2:23 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

That should be "instance is", not "instances is".

Tim Young
Elevate Software
www.elevatesoft.com
Tue, Jan 5 2016 2:39 PMPermanent Link

Trinione

<< MyInstance.Free;
MyInstance:=nil; >>

When I do it this way I get an instant error every time.

I am calling it after an animation when I move the form off the screen. Putting nil first is the only way it works.

I got the approach from the description on this page:
http://www.delphibasics.co.uk/RTL.asp?Name=freeandnil
which states: It actually does this in reverse order - first dereferencing the object before deallocating the memory.
Tue, Jan 5 2016 3:27 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com


<< When I do it this way I get an instant error every time. >>

Then you've got a bug somewhere. Smile

<< I am calling it after an animation when I move the form off the screen. Putting nil first is the only way it works. >>

Email me an example of what you're trying to do and I'll respond here with the resolution.

<< I got the approach from the description on this page:
http://www.delphibasics.co.uk/RTL.asp?Name=freeandnil
which states: It actually does this in reverse order - first dereferencing the object before deallocating the memory. >>

Yes, but it *saves* a reference to the instance in a local variable before setting the original reference to nil, and then frees *the local variable*.  The logic there is that you don't want the *original* variable still pointing to memory that has been freed because there was an exception during one of the Destroy calls inside of the class hierarchy.

You can't do the exact same thing as FreeAndNil with EWB because it doesn't support variable parameters.

Tim Young
Elevate Software
www.elevatesoft.com
Wed, Jan 6 2016 4:31 AMPermanent Link

Matthew Jones

Tim Young [Elevate Software] wrote:

> EWB 1.x had a "magic" compiler FreeAndNil() type of operation, but I
> got rid of it in EWB 2.x because it didn't work well in certain
> constructs like when used with a "with" block.

Sounds like you fixed that one the wrong way around. Removing "with"
would have been better.  8-)

--

Matthew Jones
Page 1 of 3Next Page
Jump to Page:  1 2 3
Image