Fix memory leaks. Make JavaXPCOMInstance hold ref to XPCOM object.

This commit is contained in:
pedemont%us.ibm.com 2004-10-27 17:10:03 +00:00
Родитель 25d0b06038
Коммит d6875c8d46
5 изменённых файлов: 39 добавлений и 23 удалений

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

@ -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<nsILocalFile> 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<nsIComponentManager> 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<nsIServiceManager> 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<nsIEventQueue> 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");
}

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

@ -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

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

@ -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<nsIEventQueue> 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;
}

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

@ -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();

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

@ -166,6 +166,7 @@ public class TestArray {
System.gc();
dumpArray(array, 0, null, 0);
localFile = null;
componentManager = null;
System.gc();
GeckoEmbed.termEmbedding();