fixed #727: invoke DeleteLocalRef() to resolve memory leak

This commit is contained in:
minggo 2011-09-20 14:25:16 +08:00
parent 5ff9bd0c9e
commit 762e342b4d
6 changed files with 38 additions and 16 deletions

View File

@ -45,6 +45,7 @@ extern "C"
if (env != 0 && classOfCocos2dxActivity != 0) if (env != 0 && classOfCocos2dxActivity != 0)
{ {
ret = env->GetStaticMethodID(classOfCocos2dxActivity, methodName, paramCode); ret = env->GetStaticMethodID(classOfCocos2dxActivity, methodName, paramCode);
env->DeleteLocalRef(classOfCocos2dxActivity);
} }
if (! ret) if (! ret)
@ -62,8 +63,9 @@ extern "C"
if (preloadBackgroundMusicMethodID) if (preloadBackgroundMusicMethodID)
{ {
jstring StringArg = env->NewStringUTF(path); jstring stringArg = env->NewStringUTF(path);
env->CallStaticVoidMethod(classOfCocos2dxActivity, preloadBackgroundMusicMethodID, StringArg); env->CallStaticVoidMethod(classOfCocos2dxActivity, preloadBackgroundMusicMethodID, stringArg);
env->DeleteLocalRef(stringArg);
} }
} }
@ -74,9 +76,9 @@ extern "C"
if (playBackgroundMusicMethodID) if (playBackgroundMusicMethodID)
{ {
jstring StringArg = env->NewStringUTF(path); jstring stringArg = env->NewStringUTF(path);
env->CallStaticVoidMethod(classOfCocos2dxActivity, playBackgroundMusicMethodID, StringArg, isLoop); env->CallStaticVoidMethod(classOfCocos2dxActivity, playBackgroundMusicMethodID, stringArg, isLoop);
//env->ReleaseStringUTFChars(StringArg, path); env->DeleteLocalRef(stringArg);
} }
} }
@ -172,8 +174,9 @@ extern "C"
if (playEffectMethodID) if (playEffectMethodID)
{ {
jstring StringArg = env->NewStringUTF(path); jstring stringArg = env->NewStringUTF(path);
ret = env->CallStaticIntMethod(classOfCocos2dxActivity, playEffectMethodID, StringArg, bLoop); ret = env->CallStaticIntMethod(classOfCocos2dxActivity, playEffectMethodID, stringArg, bLoop);
env->DeleteLocalRef(stringArg);
} }
return (unsigned int)ret; return (unsigned int)ret;
@ -233,8 +236,9 @@ extern "C"
if (preloadEffectMethodID) if (preloadEffectMethodID)
{ {
jstring StringArg = env->NewStringUTF(path); jstring stringArg = env->NewStringUTF(path);
env->CallStaticVoidMethod(classOfCocos2dxActivity, preloadEffectMethodID, StringArg); env->CallStaticVoidMethod(classOfCocos2dxActivity, preloadEffectMethodID, stringArg);
env->DeleteLocalRef(stringArg);
} }
} }
@ -245,8 +249,9 @@ extern "C"
if (unloadEffectMethodID) if (unloadEffectMethodID)
{ {
jstring StringArg = env->NewStringUTF(path); jstring stringArg = env->NewStringUTF(path);
env->CallStaticVoidMethod(classOfCocos2dxActivity, unloadEffectMethodID, StringArg); env->CallStaticVoidMethod(classOfCocos2dxActivity, unloadEffectMethodID, stringArg);
env->DeleteLocalRef(stringArg);
} }
} }
} }

View File

@ -69,9 +69,16 @@ public:
* and data. * and data.
* use this appoach to decrease the jni call number * use this appoach to decrease the jni call number
*/ */
jstring jstrText = methodInfo.env->NewStringUTF(text);
jstring jstrFont = methodInfo.env->NewStringUTF(pFontName);
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, methodInfo.env->NewStringUTF(text), methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, methodInfo.env->NewStringUTF(text),
methodInfo.env->NewStringUTF(pFontName), (int)fontSize, eAlignMask, nWidth, nHeight); methodInfo.env->NewStringUTF(pFontName), (int)fontSize, eAlignMask, nWidth, nHeight);
methodInfo.env->DeleteLocalRef(jstrText);
methodInfo.env->DeleteLocalRef(jstrFont);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
return true; return true;
} }

View File

@ -54,6 +54,7 @@ extern "C"
"()V")) "()V"))
{ {
t.env->CallStaticVoidMethod(t.classID, t.methodID); t.env->CallStaticVoidMethod(t.classID, t.methodID);
t.env->DeleteLocalRef(t.classID);
} }
} }

View File

@ -80,19 +80,23 @@ extern "C"
, "showMessageBox" , "showMessageBox"
, "(Ljava/lang/String;Ljava/lang/String;)V")) , "(Ljava/lang/String;Ljava/lang/String;)V"))
{ {
jstring StringArg1; jstring stringArg1;
if (! pszTitle) if (! pszTitle)
{ {
StringArg1 = t.env->NewStringUTF(""); stringArg1 = t.env->NewStringUTF("");
} }
else else
{ {
StringArg1 = t.env->NewStringUTF(pszTitle); stringArg1 = t.env->NewStringUTF(pszTitle);
} }
jstring StringArg2 = t.env->NewStringUTF(pszMsg); jstring stringArg2 = t.env->NewStringUTF(pszMsg);
t.env->CallStaticVoidMethod(t.classID, t.methodID, StringArg1, StringArg2); t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg1, stringArg2);
t.env->DeleteLocalRef(stringArg1);
t.env->DeleteLocalRef(stringArg2);
t.env->DeleteLocalRef(t.classID);
} }
} }
@ -110,6 +114,7 @@ extern "C"
, "()V")) , "()V"))
{ {
t.env->CallStaticObjectMethod(t.classID, t.methodID); t.env->CallStaticObjectMethod(t.classID, t.methodID);
t.env->DeleteLocalRef(t.classID);
} }
} }

View File

@ -68,6 +68,7 @@ extern "C"
"()V")) "()V"))
{ {
t.env->CallStaticVoidMethod(t.classID, t.methodID); t.env->CallStaticVoidMethod(t.classID, t.methodID);
t.env->DeleteLocalRef(t.classID);
} }
} }
@ -81,6 +82,7 @@ extern "C"
"()V")) "()V"))
{ {
t.env->CallStaticVoidMethod(t.classID, t.methodID); t.env->CallStaticVoidMethod(t.classID, t.methodID);
t.env->DeleteLocalRef(t.classID);
} }
} }
} }

View File

@ -50,6 +50,7 @@ extern "C"
"()Ljava/lang/String;")) "()Ljava/lang/String;"))
{ {
jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID); jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
t.env->DeleteLocalRef(t.classID);
CCString *ret = new CCString(JniHelper::jstring2string(str).c_str()); CCString *ret = new CCString(JniHelper::jstring2string(str).c_str());
ret->autorelease(); ret->autorelease();
@ -74,6 +75,7 @@ extern "C"
, "()Ljava/lang/String;")) , "()Ljava/lang/String;"))
{ {
jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID); jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
t.env->DeleteLocalRef(t.classID);
CCString *ret = new CCString(JniHelper::jstring2string(str).c_str()); CCString *ret = new CCString(JniHelper::jstring2string(str).c_str());
ret->autorelease(); ret->autorelease();