From d6875c8d467c51ac5d73a8c3272d8858369a4209 Mon Sep 17 00:00:00 2001 From: "pedemont%us.ibm.com" Date: Wed, 27 Oct 2004 17:10:03 +0000 Subject: [PATCH] Fix memory leaks. Make JavaXPCOMInstance hold ref to XPCOM object. --- extensions/java/xpcom/nsJavaInterfaces.cpp | 25 +++++++------------ extensions/java/xpcom/nsJavaWrapper.cpp | 10 ++++++-- .../java/xpcom/nsJavaXPCOMBindingUtils.cpp | 20 ++++++++++++++- .../java/xpcom/nsJavaXPCOMBindingUtils.h | 6 ++--- extensions/java/xpcom/tests/TestArray.java | 1 + 5 files changed, 39 insertions(+), 23 deletions(-) diff --git a/extensions/java/xpcom/nsJavaInterfaces.cpp b/extensions/java/xpcom/nsJavaInterfaces.cpp index 9beab60fdda..e1a9201d13d 100644 --- a/extensions/java/xpcom/nsJavaInterfaces.cpp +++ b/extensions/java/xpcom/nsJavaInterfaces.cpp @@ -45,8 +45,6 @@ #include "nsIInputStream.h" #include "nsEnumeratorUtils.h" #include "nsArray.h" -#include "nsEventQueueUtils.h" -#include "nsProxyRelease.h" #define GECKO_NATIVE(func) Java_org_mozilla_xpcom_GeckoEmbed_##func #define XPCOM_NATIVE(func) Java_org_mozilla_xpcom_XPCOM_##func @@ -139,13 +137,14 @@ GECKO_NATIVE(newLocalFile) (JNIEnv *env, jclass, jstring aPath, } // Make call to given function - nsCOMPtr file; - nsresult rv = NS_NewLocalFile(path_str, aFollowLinks, getter_AddRefs(file)); + nsILocalFile* file = nsnull; + nsresult rv = NS_NewLocalFile(path_str, aFollowLinks, &file); if (NS_SUCCEEDED(rv)) { // wrap xpcom instance JavaXPCOMInstance* inst; inst = CreateJavaXPCOMInstance(file, nsnull); + NS_RELEASE(file); // JavaXPCOMInstance has owning ref if (inst) { // create java stub @@ -170,13 +169,14 @@ GECKO_NATIVE(getComponentManager) (JNIEnv *env, jclass) jobject java_stub = nsnull; // Call XPCOM method - nsCOMPtr cm; - nsresult rv = NS_GetComponentManager(getter_AddRefs(cm)); + nsIComponentManager* cm = nsnull; + nsresult rv = NS_GetComponentManager(&cm); if (NS_SUCCEEDED(rv)) { // wrap xpcom instance JavaXPCOMInstance* inst; inst = CreateJavaXPCOMInstance(cm, &NS_GET_IID(nsIComponentManager)); + NS_RELEASE(cm); // JavaXPCOMInstance has owning ref if (inst) { // create java stub @@ -201,13 +201,14 @@ GECKO_NATIVE(getServiceManager) (JNIEnv *env, jclass) jobject java_stub = nsnull; // Call XPCOM method - nsCOMPtr sm; - nsresult rv = NS_GetServiceManager(getter_AddRefs(sm)); + nsIServiceManager* sm = nsnull; + nsresult rv = NS_GetServiceManager(&sm); if (NS_SUCCEEDED(rv)) { // wrap xpcom instance JavaXPCOMInstance* inst; inst = CreateJavaXPCOMInstance(sm, &NS_GET_IID(nsIServiceManager)); + NS_RELEASE(sm); // JavaXPCOMInstance has owning ref if (inst) { // create java stub @@ -468,15 +469,7 @@ XPCOM_NATIVE(FinalizeStub) (JNIEnv *env, jclass that, jobject aJavaObject) void* obj = GetMatchingXPCOMObject(env, aJavaObject); NS_ASSERTION(!IsXPTCStub(obj), "Expecting JavaXPCOMInstance, got nsJavaXPTCStub"); - nsISupports* xpcom_obj = ((JavaXPCOMInstance*)obj)->GetInstance(); - RemoveJavaXPCOMBinding(env, aJavaObject, nsnull); delete (JavaXPCOMInstance*) obj; - - nsCOMPtr eventQ; - nsresult rv = NS_GetMainEventQ(getter_AddRefs(eventQ)); - if (NS_SUCCEEDED(rv)) - rv = NS_ProxyRelease(eventQ, xpcom_obj); - NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to get MainEventQ"); } diff --git a/extensions/java/xpcom/nsJavaWrapper.cpp b/extensions/java/xpcom/nsJavaWrapper.cpp index 5d2e71effb2..143be60aa4a 100644 --- a/extensions/java/xpcom/nsJavaWrapper.cpp +++ b/extensions/java/xpcom/nsJavaWrapper.cpp @@ -649,7 +649,10 @@ FinalizeParams(JNIEnv *env, const jobject aParam, if (NS_FAILED(rv)) return rv; - inst = CreateJavaXPCOMInstance((nsISupports*) aVariant.val.p, &iid); + nsISupports* variant = + NS_REINTERPRET_CAST(nsISupports*, aVariant.val.p); + inst = CreateJavaXPCOMInstance(variant, &iid); + NS_RELEASE(variant); // JavaXPCOMInstance has owning ref if (inst) { // create java stub @@ -816,7 +819,10 @@ SetRetval(JNIEnv *env, const nsXPTParamInfo &aParamInfo, if (NS_FAILED(rv)) return rv; - inst = CreateJavaXPCOMInstance((nsISupports*) aVariant.val.p, &iid); + nsISupports* variant = + NS_REINTERPRET_CAST(nsISupports*, aVariant.val.p); + inst = CreateJavaXPCOMInstance(variant, &iid); + NS_RELEASE(variant); // JavaXPCOMInstance has owning ref if (inst) { // create java stub diff --git a/extensions/java/xpcom/nsJavaXPCOMBindingUtils.cpp b/extensions/java/xpcom/nsJavaXPCOMBindingUtils.cpp index b9c733d53df..6454e0b70d3 100644 --- a/extensions/java/xpcom/nsJavaXPCOMBindingUtils.cpp +++ b/extensions/java/xpcom/nsJavaXPCOMBindingUtils.cpp @@ -42,6 +42,8 @@ #include "nsIInterfaceInfoManager.h" #include "pldhash.h" #include "nsILocalFile.h" +#include "nsEventQueueUtils.h" +#include "nsProxyRelease.h" #ifdef DEBUG @@ -507,6 +509,23 @@ FreeJavaGlobals(JNIEnv* env) /********************************************************** * JavaXPCOMInstance *********************************************************/ +JavaXPCOMInstance::JavaXPCOMInstance(nsISupports* aInstance, + nsIInterfaceInfo* aIInfo) + : mInstance(aInstance), + mIInfo(aIInfo) +{ + NS_ADDREF(mInstance); +} + +JavaXPCOMInstance::~JavaXPCOMInstance() +{ + nsCOMPtr eventQ; + nsresult rv = NS_GetMainEventQ(getter_AddRefs(eventQ)); + if (NS_SUCCEEDED(rv)) + rv = NS_ProxyRelease(eventQ, mInstance); + NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to get MainEventQ"); +} + nsIInterfaceInfo* JavaXPCOMInstance::InterfaceInfo() { @@ -548,7 +567,6 @@ CreateJavaXPCOMInstance(nsISupports* aXPCOMObject, const nsIID* aIID) inst = new JavaXPCOMInstance(aXPCOMObject, info); } } - NS_ADDREF(aXPCOMObject); return inst; } diff --git a/extensions/java/xpcom/nsJavaXPCOMBindingUtils.h b/extensions/java/xpcom/nsJavaXPCOMBindingUtils.h index 8c42ddc1260..ebcccc09806 100644 --- a/extensions/java/xpcom/nsJavaXPCOMBindingUtils.h +++ b/extensions/java/xpcom/nsJavaXPCOMBindingUtils.h @@ -115,10 +115,8 @@ void FreeJavaGlobals(JNIEnv* env); class JavaXPCOMInstance { public: - JavaXPCOMInstance(nsISupports* aInstance, nsIInterfaceInfo* aIInfo) - : mInstance(aInstance), - mIInfo(aIInfo) - {} + JavaXPCOMInstance(nsISupports* aInstance, nsIInterfaceInfo* aIInfo); + ~JavaXPCOMInstance(); nsISupports* GetInstance() { return mInstance; } nsIInterfaceInfo* InterfaceInfo(); diff --git a/extensions/java/xpcom/tests/TestArray.java b/extensions/java/xpcom/tests/TestArray.java index 64521052817..c95b1f05311 100644 --- a/extensions/java/xpcom/tests/TestArray.java +++ b/extensions/java/xpcom/tests/TestArray.java @@ -166,6 +166,7 @@ public class TestArray { System.gc(); dumpArray(array, 0, null, 0); + localFile = null; componentManager = null; System.gc(); GeckoEmbed.termEmbedding();