Fix leak of preferences JS context by making prefs hold a reference to the JS runtime service and using that reference to avoid "runtime mismatch, leaking context" under normal conditions. b=76288 r=shaver, bnesse sr=waterson

This commit is contained in:
dbaron%fas.harvard.edu 2001-08-03 02:35:32 +00:00
Родитель b2c1b3b770
Коммит b5094fcf5c
1 изменённых файлов: 16 добавлений и 8 удалений

Просмотреть файл

@ -60,6 +60,9 @@ static nsresult openPrefFileSpec(nsIFileSpec* aFilespec, PRBool aIsErrorFatal, P
static nsresult savePrefFile(nsIFile* aFile); static nsresult savePrefFile(nsIFile* aFile);
// needed so we can still get the JS Runtime Service during XPCOM shutdown
static nsIJSRuntimeService* gJSRuntimeService = nsnull; // owning reference
/* /*
* Constructor/Destructor * Constructor/Destructor
@ -81,6 +84,7 @@ nsPrefService::~nsPrefService()
{ {
PREF_Cleanup(); PREF_Cleanup();
NS_IF_RELEASE(mCurrentFile); NS_IF_RELEASE(mCurrentFile);
NS_IF_RELEASE(gJSRuntimeService);
} }
@ -714,16 +718,20 @@ extern "C" JSRuntime* PREF_GetJSRuntime()
{ {
nsresult rv; nsresult rv;
nsCOMPtr<nsIJSRuntimeService> rtsvc = if (!gJSRuntimeService) {
do_GetService("@mozilla.org/js/xpc/RuntimeService;1", &rv); rv = CallGetService("@mozilla.org/js/xpc/RuntimeService;1",
if (NS_SUCCEEDED(rv)) { &gJSRuntimeService);
JSRuntime* rt; if (NS_FAILED(rv)) {
rv = rtsvc->GetRuntime(&rt);
if (NS_SUCCEEDED(rv)) {
return rt;
}
}
NS_WARNING("nsJSRuntimeService is missing"); NS_WARNING("nsJSRuntimeService is missing");
gJSRuntimeService = nsnull;
return nsnull;
}
}
JSRuntime* rt;
rv = gJSRuntimeService->GetRuntime(&rt);
if (NS_SUCCEEDED(rv))
return rt;
return nsnull; return nsnull;
} }