admin管理员组文章数量:1398831
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?
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?
- Can you provide some code? – Traveling Tech Guy Commented Jul 30, 2014 at 16:18
3 Answers
Reset to default 3This is likely due to holding zombie partments. 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 partments
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).
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");
}
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
本文标签:
版权声明:本文标题:javascript - How can I avoid state of "TypeError: can't access dead object" in my Firefox add-on? - St 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744124201a2591879.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论