Jump to content

Should objects be freed?


Recommended Posts

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;
Link to post
Share on other sites

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.

Link to post
Share on other sites

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.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...