referrence:http://www.cocos2d-x.org/boards/10/topics/5193?r=5202
based on the referrence link it solved thepreloadBackgroundMusicMethodID andplayBackgroundMusic crash problem, but playEffect still happens.
the followiing is article from referrence link . and I add my solution at the end.
----------------------------------------------------------------------------------------------------
I met a problem in my project using cocos2d-1.0.1-x-0.9.2 API on Android platform.
Calling to any function such as preloadBackgroundMusic and playBackgroundMusic in SimpleAudioEngine will cause a crash.
I found that actually calling to any function in class SimpleAudioEngine will cause a crash just at the very line "env->CallStaticVoidMethod(classOfCocos2dxActivity, preloadBackgroundMusicMethodID, stringArg);" where the global variable classOfCocos2dxActivity is NULL.
The questionable code is here:
static jmethodID getMethodID(const char *methodName, const char *paramCode)
{
jmethodID ret = 0;
// get jni environment and java class for Cocos2dxActivity
if (gJavaVM->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK)
{
LOGD("Failed to get the environment using GetEnv()");
return 0;
}
if (gJavaVM->AttachCurrentThread(&env, 0) < 0)
{
LOGD("Failed to get the environment using AttachCurrentThread()");
return 0;
}
classOfCocos2dxActivity = env->FindClass("org/cocos2dx/lib/Cocos2dxActivity");
if (! classOfCocos2dxActivity)
{
LOGD("Failed to find class of org/cocos2dx/lib/Cocos2dxActivity");
return 0;
}
if (env != 0 && classOfCocos2dxActivity != 0)
{
ret = env->GetStaticMethodID(classOfCocos2dxActivity, methodName, paramCode);
env->DeleteLocalRef(classOfCocos2dxActivity);
// ************************************************************************************************
// this line will release classOfCocos2dxActivity, but later, classOfCocos2dxActivity will be used again in preloadBackgroundMusicJNI
// by the line "env->CallStaticVoidMethod(classOfCocos2dxActivity, preloadBackgroundMusicMethodID, stringArg);"
// ************************************************************************************************
}
if (! ret)
{
LOGD("get method id of %s error", methodName);
}
return ret;
}
void preloadBackgroundMusicJNI(const char *path)
{
// void playBackgroundMusic(String,boolean)
jmethodID preloadBackgroundMusicMethodID = getMethodID("preloadBackgroundMusic", "(Ljava/lang/String;)V");
if (preloadBackgroundMusicMethodID)
{
jstring stringArg = env->NewStringUTF(path);
env->CallStaticVoidMethod(classOfCocos2dxActivity, preloadBackgroundMusicMethodID, stringArg);
// ************************************************************************
// above, classOfCocos2dxActivity is a NULL pointer which will cause crash in CallStaticVoidMethod
// ************************************************************************
env->DeleteLocalRef(stringArg);
}
}
For now, I have to fix this problem by following steps:
static jmethodID getMethodID(const char *methodName, const char *paramCode)
{
jmethodID ret = 0;
// get jni environment and java class for Cocos2dxActivity
if (gJavaVM->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK)
{
LOGD("Failed to get the environment using GetEnv()");
return 0;
}
if (gJavaVM->AttachCurrentThread(&env, 0) < 0)
{
LOGD("Failed to get the environment using AttachCurrentThread()");
return 0;
}
if (env == 0)
{ LOGD("Failed to get environment, env* = 0");
return 0;
}
// **************************************************************************************
if (!classOfCocos2dxActivity) classOfCocos2dxActivity = env->FindClass("org/cocos2dx/lib/Cocos2dxActivity"); // fix #1
// **************************************************************************************
if (! classOfCocos2dxActivity)
{
LOGD("Failed to find class of org/cocos2dx/lib/Cocos2dxActivity");
return 0;
}
ret = env->GetStaticMethodID(classOfCocos2dxActivity, methodName, paramCode);
LOGD("SimpleAudioEngine just got methodID: %d", ret);
//********************************************
//env->DeleteLocalRef(classOfCocos2dxActivity); // fix #2
//********************************************
if (! ret)
{
LOGD("get method id of %s error", methodName);
}
return ret;
}
My solution to fix playEffect still crash based on the previous fixs.
static jmethodID getMethodID(const char *methodName, const char *paramCode)
{
jmethodID ret = 0;
// get jni environment and java class for Cocos2dxActivity
if (gJavaVM->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK)
{
LOGD("Failed to get the environment using GetEnv()");
return 0;
}
if (gJavaVM->AttachCurrentThread(&env, 0) < 0)
{
LOGD("Failed to get the environment using AttachCurrentThread()");
return 0;
}
if (env == 0)
{ LOGD("Failed to get environment, env* = 0");
return 0;
}
// **************************************************************************************
// if (!classOfCocos2dxActivity) if this line added it will be crashed when we call playEffect(), it seems it will cause memory leak. but through my continuous verify on my android device, there is no memory leak happended. when I call playEffect hundreds of times in my program in a shot time. and the memory of this program will increase from 23k to 24k, but it will come to 23k again after I stop call playEffect.
classOfCocos2dxActivity = env->FindClass("org/cocos2dx/lib/Cocos2dxActivity"); // fix #1
// **************************************************************************************
if (! classOfCocos2dxActivity)
{
LOGD("Failed to find class of org/cocos2dx/lib/Cocos2dxActivity");
return 0;
}
ret = env->GetStaticMethodID(classOfCocos2dxActivity, methodName, paramCode);
LOGD("SimpleAudioEngine just got methodID: %d", ret);
//********************************************
//env->DeleteLocalRef(classOfCocos2dxActivity); // fix #2
//********************************************
if (! ret)
{
LOGD("get method id of %s error", methodName);
}
return ret;
}
the latest release version
static jmethodID getMethodID(const char *methodName, const char *paramCode)
{
jmethodID ret = 0;
// get jni environment and java class for Cocos2dxActivity
if (gJavaVM->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK)
{
LOGD("Failed to get the environment using GetEnv()");
return 0;
}
if (gJavaVM->AttachCurrentThread(&env, 0) < 0)
{
LOGD("Failed to get the environment using AttachCurrentThread()");
return 0;
}
classOfCocos2dxActivity = env->FindClass("org/cocos2dx/lib/Cocos2dxActivity");
if (! classOfCocos2dxActivity)
{
LOGD("Failed to find class of org/cocos2dx/lib/Cocos2dxActivity");
return 0;
}
if (env != 0 && classOfCocos2dxActivity != 0)
{
ret = env->GetStaticMethodID(classOfCocos2dxActivity, methodName, paramCode);
}
if (! ret)
{
LOGD("get method id of %s error", methodName);
}
return ret;
}