rlib
rlib

Reputation: 7867

How can I avoid state of "TypeError: can't access dead object" in my Firefox add-on?

It seems checking against null works, but is it a correct method? How can I correctly check that object is not dead? And where is the definition of dead object?

Upvotes: 5

Views: 9086

Answers (3)

Blagoh
Blagoh

Reputation: 1235

This is likely due to holding zombie compartments. If you are storing a window in a variable you should use weak reference, otherwise it will keep the process alive.

Great read right here:

MDN article about Zombie compartments

This is how to use weak references:

MDN documentation for Components.utils.getWeakReference

A dead object, is holding a strong (keep alive) reference to a DOM element (usually) that persists even after it was destroyed in the DOM.

Sometimes checking if it is undefined or null does not work, a trick I saw once and use sometimes is to check if parentNode exists (so not null or undefined).

Upvotes: 3

Wladimir Palant
Wladimir Palant

Reputation: 57651

If you cannot use weak references as suggested by Blagoh, then you can use Components.utils.isDeadWrapper() function to check (added in Firefox 17 but still not really documented):

if (Components.utils.isDeadWrapper(element))
  alert("I won't touch that, it's a dead object");

Unprivileged code doesn't really have a way of recognizing dead objects without triggering an exception. Then again, if an object throws an exception no matter what you do then it is probably dead:

try
{
  String(element);
}
catch (e)
{
  alert("Better not touch that, it's likely a dead object");
}

Upvotes: 1

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

Dead object would mean an object whose parent document has been destroyed, and the references are removed to eliminate memory leaks in add-ons. So you could check for the element, as:

if( typeof some_element !== 'undefined') {
    //its not dead
}

See Dead Object Reference

Upvotes: -1

Related Questions