diff --git a/dom/src/base/nsJSEnvironment.cpp b/dom/src/base/nsJSEnvironment.cpp index 6cf98b25513d..15f404f39e42 100644 --- a/dom/src/base/nsJSEnvironment.cpp +++ b/dom/src/base/nsJSEnvironment.cpp @@ -531,6 +531,12 @@ nsJSContext::CallFunctionObject(void *aObj, void *aFunObj, PRUint32 argc, if (NS_FAILED(rv) || NS_FAILED(stack->Push(mContext))) return NS_ERROR_FAILURE; + // this context can be deleted unexpectedly if the JS closes + // the owning window. we ran into this problem specifically + // when going through the "close window" key event handler + // (that is, hitting ^W on Windows). the addref just below + // prevents our untimely destruction. + nsCOMPtr kungFuDeathGrip(this); mRef = nsnull; mTerminationFunc = nsnull;