How can I avoid the “TypeError: cannot access dead object” state in my Firefox add-on?

Checking seems to work with null, but is it the correct method? How can I correctly verify that the object is not dead? And where is the definition of a dead object?

+4
source share
3 answers

Dead object will mean the object whose parent document has been destroyed, and links are removed to eliminate memory leaks in add-ons. So you can check the element like:

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

See Link to Dead Objects

+2
source
+3

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

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

Unprivileged code has no way of recognizing dead objects without throwing an exception. Again, if an object throws an exception, no matter what you do, it is probably dead:

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

All Articles