playBackgroundMusic and playEffect crash

referrence: http://www.cocos2d-x.org/boards/10/topics/5193?r=5202
based on the referrence link it solved the preloadBackgroundMusicMethodID and playBackgroundMusic 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;
     }






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值