From bc5b1a28332d414475ac490e81f7d6c80f569b33 Mon Sep 17 00:00:00 2001 From: "pedemont%us.ibm.com" Date: Wed, 27 Sep 2006 15:18:27 +0000 Subject: [PATCH] Fix memory leaks. Not part of build. a=mkaply. Original committer: pedemont%us.ibm.com Original revision: 1.30 Original date: 2005/04/14 22:30:16 --- extensions/java/xpcom/src/nsJavaWrapper.cpp | 29 ++++++++++++++++----- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/extensions/java/xpcom/src/nsJavaWrapper.cpp b/extensions/java/xpcom/src/nsJavaWrapper.cpp index fea801b18f7f..ef511782efb6 100644 --- a/extensions/java/xpcom/src/nsJavaWrapper.cpp +++ b/extensions/java/xpcom/src/nsJavaWrapper.cpp @@ -572,14 +572,15 @@ SetupParams(JNIEnv *env, const jobject aParam, PRUint8 aType, PRBool aIsOut, if (uniLength > 0) { if (aType == nsXPTType::T_CHAR_STR) { jsize utf8Length = env->GetStringUTFLength(data); - buf = nsMemory::Alloc(utf8Length + 1); + buf = nsMemory::Alloc((utf8Length + 1) * sizeof(char)); if (!buf) { rv = NS_ERROR_OUT_OF_MEMORY; break; } - env->GetStringUTFRegion(data, 0, uniLength, (char*) buf); - ((char*)buf)[utf8Length] = '\0'; + char* char_str = NS_STATIC_CAST(char*, buf); + env->GetStringUTFRegion(data, 0, uniLength, char_str); + char_str[utf8Length] = '\0'; } else { // if T_WCHAR_STR buf = nsMemory::Alloc((uniLength + 1) * sizeof(jchar)); @@ -588,8 +589,9 @@ SetupParams(JNIEnv *env, const jobject aParam, PRUint8 aType, PRBool aIsOut, break; } - env->GetStringRegion(data, 0, uniLength, (jchar*) buf); - ((jchar*)buf)[uniLength] = '\0'; + jchar* jchar_str = NS_STATIC_CAST(jchar*, buf); + env->GetStringRegion(data, 0, uniLength, jchar_str); + jchar_str[uniLength] = '\0'; } } else { // create empty string @@ -1075,7 +1077,10 @@ FinalizeParams(JNIEnv *env, const nsXPTParamInfo &aParamInfo, PRUint8 aType, } // Ordinarily, we would delete 'iid' here. But we cannot do that until - // we've handled all of the params. See comment in CallXPCOMMethod + // we've handled all of the params. See comment in CallXPCOMMethod. + // We can safely delete array elements, though. + if (aIsArrayElement) + delete iid; break; } @@ -1217,6 +1222,18 @@ FinalizeParams(JNIEnv *env, const nsXPTParamInfo &aParamInfo, PRUint8 aType, } // cleanup + // If this is not an out param or if the invokeResult is a failure case, + // then the array elements have not been cleaned up. Do so now. + if (!aParamInfo.IsOut() || NS_FAILED(aInvokeResult)) { + nsXPTCVariant var; + for (PRUint32 i = 0; i < aArraySize; i++) { + rv = GetNativeArrayElement(aArrayType, aVariant.val.p, i, &var); + if (NS_SUCCEEDED(rv)) { + FinalizeParams(env, aParamInfo, aArrayType, var, aIID, PR_TRUE, + 0, 0, i, NS_ERROR_FAILURE, nsnull); + } + } + } PR_Free(aVariant.val.p); break; }