Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 6 of 6 total
Thread Create objects without calling Free
Sat, Jul 27 2013 5:26 AMPermanent Link

Christian Kaufmann

Hi,

since Javascript has garbage collection, do I really have to call
"Free" on all of my objects?

For example, I create a temporary object and pass it to different
methods in different classes. So I don't really know, when the object
isn't used anymore.

Will the object be deleted anyway, when the last reference is lost?
I'm aware of the fact, that in this case, Destroy() won't be called.
But I don't need this for complex objects like a TForm. It's more for
temporary structured data, that is passed arround in my application.


var
 tmp : TMyObject;
begin
 tmp := TMyObject.Create;
 tmp.DoSomething;

 other1.UseIt(tmp);
 other2.UseIt(tmp)
end;


cu Christian
Mon, Jul 29 2013 5:12 AMPermanent Link

Matthew Jones

I'd like a knowledgeable response to this, but from what I have heard on podcasts
javascript doesn't do garbage collection in any good way. It seems that most
javascript assumes that the user will soon click on another page, thus trashing
everything, and thus you don't need to care about memory. When you get to apps that
are doing a lot more and have a longer lifespan, caring about memory is more
important.

But if it really garbage collects, then it wouldn't matter in some cases. I look
forward to Tim's response.

/Matthew Jones/
Mon, Jul 29 2013 9:06 AMPermanent Link

Raul

Team Elevate Team Elevate

I concur that this needs Tim's response so following are just wild guesses :
- Free provides a way to run any cleanup and in a more predictable way
- compiler might be able analyze the calls and implement some additional
smarts to warn of issues compile time (like create/free pairs and using
the variable after etc.) and maybe even generate better javascript this way
- finally it makes it bit easier to use delphi style of coding and even
paste in existing delphi code

I personally like the last part since usage patterns match up with my
delphi coding and i don't have to do a mental switch every time  (it
always takes me little while to untrain my muscle memory when switching
from/to delphi and c#/Java to get the comparison and assignment right
for example. Smile

Raul



On 7/27/2013 5:26 AM, Christian Kaufmann wrote:
> Hi,
>
> since Javascript has garbage collection, do I really have to call
> "Free" on all of my objects?
>
> For example, I create a temporary object and pass it to different
> methods in different classes. So I don't really know, when the object
> isn't used anymore.
>
> Will the object be deleted anyway, when the last reference is lost?
> I'm aware of the fact, that in this case, Destroy() won't be called.
> But I don't need this for complex objects like a TForm. It's more for
> temporary structured data, that is passed arround in my application.
>
>
> var
>    tmp : TMyObject;
> begin
>    tmp := TMyObject.Create;
>    tmp.DoSomething;
>
>    other1.UseIt(tmp);
>    other2.UseIt(tmp)
> end;
>
>
> cu Christian
>
Mon, Jul 29 2013 9:23 AMPermanent Link

Christian Kaufmann

>- finally it makes it bit easier to use delphi style of coding and even
>paste in existing delphi code
>
>I personally like the last part since usage patterns match up with my
>delphi coding and i don't have to do a mental switch every time  (it

Well same for me. But since Delphi has no garbage collection, I
created many "interfaced" classes in Delphi to have a possiblity to
pass objects arround. So in my code you almost find no "Free" calls
anymore.
Mon, Jul 29 2013 9:59 AMPermanent Link

Matthew Jones

> and even
> paste in existing delphi code
>
> I personally like the last part since usage patterns match up with
> my delphi coding

I agree with this. I've been able to take whole units written in Delphi and use
them pretty much unchanged in EWB. And recently, I took code from EWB and used it
in my Delphi code.

/Matthew Jones/
Mon, Jul 29 2013 2:04 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Christian,

<< since Javascript has garbage collection, do I really have to call "Free"
on all of my objects? >>

Technically, no.  But, you should be very careful when doing so since Free
is very much like Dispose in .NET - you may have resources, etc. in use in
your object instances that need to be dealt with in order to indicate to the
web browser (or your application) that they are no longer being used.  Of
course, in your case you wrote the class/object so you know what's involved
with it.

In EWB, Free is the equivalent of "call these shutdown procedures (Destroy)
and then nil the variable so that the garbage collector knows to get rid of
the instance".

Tim Young
Elevate Software
www.elevatesoft.com
Image