Jump to content


Photo

Should objects be freed?


  • Please log in to reply
3 replies to this topic

#1 markus_ja

markus_ja
  • Members
  • 319 posts
  • LocationAustria

Posted 03 February 2016 - 10:52 AM

Hello,

 

shall I free objects? I know, javascript manages the lifecycle by itself, but why do we have a .Free function?

Is it a good a approche, to call free after an object is no longer needed? Or doesn't it matter?

 

e.g.:

procedure TForm1.MainContentAnimation;
var
  props: Variant;
begin
  props := new JObject;
  props['backgroundColor'] := '#fff';

  jQuery(divContent).animate(props, 1000);

  props.Free; //<= Should I call free here?
end;


#2 Christian-W. Budde

Christian-W. Budde
  • Administrators
  • 334 posts
  • LocationGermany

Posted 03 February 2016 - 04:49 PM

It is up to you whether you want to free objects manually or let the memory be freed automatically by the garbage collection.

 

In case of custom classes it could make sense to use 'Free' manually as you may want to perform certain actions upon destruction (with this approach the destructor is called). If you rely on the garbage collection the destructor is not (never) called.

 

For an external JObject class (as shown in the example) it doesn't make much (any) sense. It could even cause an error in the browser as 'Free' is not defined. However, in fact it does just nothing.


  • ielite and BobLawrence like this

#3 ielite

ielite
  • Members
  • 690 posts

Posted 03 February 2016 - 07:08 PM

Great explanation Christian!  Thank You!

 

It is up to you whether you want to free objects manually or let the memory be freed automatically by the garbage collection.

 

In case of custom classes it could make sense to use 'Free' manually as you may want to perform certain actions upon destruction (with this approach the destructor is called). If you rely on the garbage collection the destructor is not (never) called.

 

For an external JObject class (as shown in the example) it doesn't make much (any) sense. It could even cause an error in the browser as 'Free' is not defined. However, in fact it does just nothing.



#4 markus_ja

markus_ja
  • Members
  • 319 posts
  • LocationAustria

Posted 03 February 2016 - 09:55 PM

Thanks.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

IPB Skin By Virteq