Jump to content
markus_ja

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;

Share this post


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.

Share this post


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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×