views:

75

answers:

2

Is there any method that is called or event that is dispatched right before an Element is cleaned up by the JavaScript garbage collector?

In Perl I would write:

package MyObj;

sub new {bless {}}

sub DESTROY {print "cleaning up @_\n"}

and then later:

{
    my $obj = MyObj->new;

    # do something with obj

}  # scope ends, and assuming there are no external references to $obj,
   #   the DESTROY method will be called before the object's memory is freed

My target platform is Firefox (and I have no need to support other browsers), so if there is only a Firefox specific way of doing this, that is fine.

And a little background: I am writing the Perl module XUL::Gui which serves as a bridge between Perl and Firefox, and I am currently working on plugging a few memory leaks related to DOM Elements sticking around forever, even after they are gone and no more references remain on the Perl side. So I am looking for ways to either figure out when JavaScript Elements will be destroyed, or a way to force JavaScript to cleanup an object.

If there is no way to do this in pure JavaScript, a solution using XPConnect/XPCOM or any other Mozilla specific technology is acceptable.

+1  A: 

There's no mechanism for that in pure JavaScript.

Matthew Flaschen
firefox specific javascript or XPCOM is fine
Eric Strom
+3  A: 

Does XUL::Gui allow you to interact with the browser at the SpiderMonkey API layer? If so, https://developer.mozilla.org/en/SpiderMonkey/JSAPI_Reference/JSClass.finalize might be useful to you. Otherwise, you may be stuck since as Matthew Flaschen says above, there's no way to do it inside Javascript.

Dan
Interesting, this might be a possibility. Currently, all of `XUL::Gui`'s interaction with the browser is at the JavaScript level, running from a chrome url. Is there a way to get at the JSAPI from JavaScript running with chrome privileges?
Eric Strom