Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Delete object in SpiderMonkey

95 views
Skip to first unread message

Kirill Pekarov

unread,
Nov 23, 2006, 10:13:21 AM11/23/06
to dev-tech-...@lists.mozilla.org
Hi, All.

I can't delete object created in my plugin.

I create object in plugin with JS_NewObject, return this object to
script. All methods and properties works fine, but later in the script
I tried command "delete myObj;" but in this case I
don't fall in JSDestructor() of this class.
I fall in it only on exit from current namespace like function, or
else.

Is there something trick with delete object? Or may be something wrong
in registering or creating this class in SpiderMonkey?

Unfortunately, I can't find nothing in documentation.

Please, help me resolve this problem.

--
With best regards
Kirill mailto:kirill_...@valentina-db.com

Peter Wilson

unread,
Nov 28, 2006, 7:40:46 AM11/28/06
to
Kirill Pekarov wrote:
> Hi, All.
>
> I can't delete object created in my plugin.
>
> I create object in plugin with JS_NewObject, return this object to
> script. All methods and properties works fine, but later in the script
> I tried command "delete myObj;" but in this case I
> don't fall in JSDestructor() of this class.
> I fall in it only on exit from current namespace like function, or
> else.
>
> Is there something trick with delete object? Or may be something wrong
> in registering or creating this class in SpiderMonkey?
There isn't anything wrong with your code - just how you understand JavaScript
works.

'delete' simply removes a reference to an object. It doesn't delete the object.
Example:

var myChildObject={text:"Hello World"};

var parent = {};

parent.child = myChildObject;

delete myChildObject;

That last delete doesn't delete the object you created on the first line - it
just removed the reference to it called 'myChildObject'. The object you created
is still referenced in the object referenced by 'parent' so if I do:

write(parent.child.text)

I'd still get "Hello World".

Now - if I do:

delete parent.child

I've removed the reference to the original object from 'parent'. Now no-one
references that original object. It's effectively an orphan. Will it now get
deleted?

No - at least not right away. The object continues to exist until garbage
collection. That process will go look for orphans and call their finalizers.
When that happen is somewhat arbitrary.

So..... 'delete' does *not* delete an object, just a reference to an object.

Hope that helps!
Peter Wilson
--
Server side JavaScript : http://www.whitebeam.org
http:/www.yellowhawk.co.uk
--

Kirill Pekarov

unread,
Nov 28, 2006, 9:07:42 AM11/28/06
to dev-tech-...@lists.mozilla.org
Hi, Peter.

You wrote 28 November 2006 y., 14:40:46:

>> Is there something trick with delete object? Or may be something wrong
>> in registering or creating this class in SpiderMonkey?
> There isn't anything wrong with your code - just how you understand JavaScript
> works.

> No - at least not right away. The object continues to exist until garbage


> collection. That process will go look for orphans and call their finalizers.
> When that happen is somewhat arbitrary.

> So..... 'delete' does *not* delete an object, just a reference to an object.

Yes, thanks for the answer. It help me but not completely.

I'm developing plugin for database.
In my case, object, that I need delete, it is a cursor with many
records, and I can't leave it alive until the garbage collector delete
it. It is a object which may have size in dozen or hundreds megabytes,
and it is infeasible solution for me keep alive it.

As I'm right understand, JS is not have any mechanism to do manually delete
object?


Thanks!

Peter Wilson

unread,
Nov 28, 2006, 9:26:46 AM11/28/06
to

No - it does not. The way I've dealt with this in the past is to add a method
that achieves what you want. My own database interface in Whitebeam works as
follows :

var myDbase = new PgsqlConnection;

myDbase.connect("database");

myDbase.exec("SELECT * FROM mytable WHERE ...");

// Use the result data
myDbase.close()


My native implementation of 'close' releases all the resources being held by the
underlying object. The only thing left is a stub - when garbage collection gets
round to its work the finalize operation will be called.

Note that the finalize function should also clean up the resource in case the
author failed to call 'close()' explicity.

Does that help?
Pete

Kirill Pekarov

unread,
Nov 28, 2006, 9:40:35 AM11/28/06
to dev-tech-...@lists.mozilla.org
Hi, Peter.

You wrote 28 November 2006 y., 16:26:46:

>> As I'm right understand, JS is not have any mechanism to do manually delete
>> object?

> No - it does not. The way I've dealt with this in the past is to add a method

> Note that the finalize function should also clean up the resource in case the


> author failed to call 'close()' explicity.

> Does that help?
Yes of course, thanks for help Peter.

0 new messages