From 57afced3f0f2cbf5fce3f0a1905f2d7620def369 Mon Sep 17 00:00:00 2001 From: "jwalden@mit.edu" Date: Thu, 8 Nov 2007 21:12:59 -0800 Subject: [PATCH] Bug 399096 - nsDOMCacheUpdateService leaks. r=dcamp, sr=biesi, a=blocker --- uriloader/prefetch/nsOfflineCacheUpdate.cpp | 29 +++++++++++++++------ uriloader/prefetch/nsOfflineCacheUpdate.h | 9 ++++++- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/uriloader/prefetch/nsOfflineCacheUpdate.cpp b/uriloader/prefetch/nsOfflineCacheUpdate.cpp index 2fddf0a827f..157029a8f91 100644 --- a/uriloader/prefetch/nsOfflineCacheUpdate.cpp +++ b/uriloader/prefetch/nsOfflineCacheUpdate.cpp @@ -419,9 +419,10 @@ nsOfflineCacheUpdate::Init(PRBool aPartialUpdate, nsresult rv; // Make sure the service has been initialized - if (!nsOfflineCacheUpdateService::GetInstance()) { + nsOfflineCacheUpdateService* service = + nsOfflineCacheUpdateService::EnsureService(); + if (!service) return NS_ERROR_FAILURE; - } LOG(("nsOfflineCacheUpdate::Init [%p]", this)); @@ -649,8 +650,8 @@ nsOfflineCacheUpdate::Finish() mState = STATE_FINISHED; - nsOfflineCacheUpdateService *service = - nsOfflineCacheUpdateService::GetInstance(); + nsOfflineCacheUpdateService* service = + nsOfflineCacheUpdateService::EnsureService(); if (!mPartialUpdate) { if (mSucceeded) { @@ -822,8 +823,8 @@ nsOfflineCacheUpdate::Schedule() { LOG(("nsOfflineCacheUpdate::Schedule [%p]", this)); - nsOfflineCacheUpdateService *service = - nsOfflineCacheUpdateService::GetInstance(); + nsOfflineCacheUpdateService* service = + nsOfflineCacheUpdateService::EnsureService(); if (!service) { return NS_ERROR_FAILURE; @@ -837,8 +838,8 @@ nsOfflineCacheUpdate::ScheduleOnDocumentStop(nsIDOMDocument *aDocument) { LOG(("nsOfflineCacheUpdate::ScheduleOnDocumentStop [%p]", this)); - nsOfflineCacheUpdateService *service = - nsOfflineCacheUpdateService::GetInstance(); + nsOfflineCacheUpdateService* service = + nsOfflineCacheUpdateService::EnsureService(); if (!service) { return NS_ERROR_FAILURE; @@ -930,6 +931,18 @@ nsOfflineCacheUpdateService::GetInstance() return gOfflineCacheUpdateService; } +nsOfflineCacheUpdateService * +nsOfflineCacheUpdateService::EnsureService() +{ + if (!gOfflineCacheUpdateService) { + // Make the service manager hold a long-lived reference to the service + nsCOMPtr service = + do_GetService(NS_OFFLINECACHEUPDATESERVICE_CONTRACTID); + } + + return gOfflineCacheUpdateService; +} + nsresult nsOfflineCacheUpdateService::Schedule(nsOfflineCacheUpdate *aUpdate) { diff --git a/uriloader/prefetch/nsOfflineCacheUpdate.h b/uriloader/prefetch/nsOfflineCacheUpdate.h index 31427bd9c35..542cf0062b5 100644 --- a/uriloader/prefetch/nsOfflineCacheUpdate.h +++ b/uriloader/prefetch/nsOfflineCacheUpdate.h @@ -177,8 +177,15 @@ public: nsIDOMDocument *aDocument); nsresult UpdateFinished(nsOfflineCacheUpdate *aUpdate); - static nsOfflineCacheUpdateService *GetInstance(); + /** + * Returns the singleton nsOfflineCacheUpdateService without an addref, or + * nsnull if the service couldn't be created. + */ + static nsOfflineCacheUpdateService *EnsureService(); + /** Addrefs and returns the singleton nsOfflineCacheUpdateService. */ + static nsOfflineCacheUpdateService *GetInstance(); + private: nsresult ProcessNextUpdate();