无法在cocos2dx中为CCDictionary设置对象

时间:2012-12-11 10:32:43

标签: java android c++ java-native-interface cocos2d-x

我正在尝试将一些数据(从java类获得)设置为CCDictionary,然后将该字典对象添加到c ++类中的CCArray,如下所示。从本机方法调用addFriends(...)。

void RecJNICommunicator::addFriends(const char * nativeStringUserName, const char * nativeStringUserId) {
CCLog(" --- inside RecJNICommunicator::addFriends --- ");

  CCDictionary * cdictionary =  CCDictionary::create();

  CCString *str1 = CCString::create(nativeStringUserName);
  CCString *str2 = CCString::create(nativeStringUserId);

  cdictionary->setObject(str1, "name");
  cdictionary->setObject(str2, "id");

  if(!FacebookFriendListScreen::friendsList) {
      CCLog(" --- FacebookFriendListScreen::friendsList is NULL... creating new --- ");
      FacebookFriendListScreen::friendsList = new CCArray;
  }
  

//将字典对象添加到CCArray

  FacebookFriendListScreen::friendsList->addObject(cdictionary);

  CCLog(" --- added successfully --- ");
}

第一次成功添加,但后来的应用程序崩溃了以下堆栈跟踪

12-11 10:21:39.707: INFO/DEBUG(24299):          #00  pc 001363d2  

/data/data/com.humit/lib/libgame.so (_ZN7cocos2d12CCDictionary15setObjectUnSafeEPNS_8CCObjectERKSs)
12-11 10:21:39.707: INFO/DEBUG(24299):          #01  pc 00135258  /data/data/com.humit/lib/libgame.so (_ZN7cocos2d12CCDictionary9setObjectEPNS_8CCObjectERKSs)
12-11 10:21:39.707: INFO/DEBUG(24299):          #02  pc 000f2d9a  /data/data/com.humit/lib/libgame.so (_ZN18RecJNICommunicator10addFriendsEPKcS1_)
12-11 10:21:39.707: INFO/DEBUG(24299):          #03  pc 000f2c42  /data/data/com.humit/lib/libgame.so (Java_com_humit_android_HumIt_addFriends)
12-11 10:21:39.707: INFO/DEBUG(24299):          #04  pc 0001ed70  /system/lib/libdvm.so (dvmPlatformInvoke)
12-11 10:21:39.707: INFO/DEBUG(24299):          #05  pc 0005902c  /system/lib/libdvm.so (_Z16dvmCallJNIMethodPKjP6JValuePK6MethodP6Thread)
12-11 10:21:39.712: INFO/DEBUG(24299):          #06  pc 0005ad5c  /system/lib/libdvm.so (_Z22dvmResolveNativeMethodPKjP6JValuePK6MethodP6Thread)
12-11 10:21:39.712: INFO/DEBUG(24299):          #07  pc 00030bcc  /system/lib/libdvm.so
12-11 10:21:39.712: INFO/DEBUG(24299):          #08  pc 000343b0  /system/lib/libdvm.so (_Z12dvmInterpretP6ThreadPK6MethodP6JValue)
12-11 10:21:39.712: INFO/DEBUG(24299):          #09  pc 0006c8c6  /system/lib/libdvm.so (_Z15dvmInvokeMethodP6ObjectPK6MethodP11ArrayObjectS5_P11ClassObjectb)
12-11 10:21:39.712: INFO/DEBUG(24299):          #10  pc 00073eba  /system/lib/libdvm.so
12-11 10:21:39.712: INFO/DEBUG(24299):          #11  pc 00030bcc  /system/lib/libdvm.so
12-11 10:21:39.712: INFO/DEBUG(24299):          #12  pc 000343b0  /system/lib/libdvm.so (_Z12dvmInterpretP6ThreadPK6MethodP6JValue)
12-11 10:21:39.712: INFO/DEBUG(24299):          #13  pc 0006cb96  /system/lib/libdvm.so (_Z14dvmCallMethodVP6ThreadPK6MethodP6ObjectbP6JValueSt9__va_list)
12-11 10:21:39.712: INFO/DEBUG(24299):          #14  pc 00054ff6  /system/lib/libdvm.so
12-11 10:21:39.717: INFO/DEBUG(24299):          #15  pc 00049d8a  /system/lib/libandroid_runtime.so
12-11 10:21:39.717: INFO/DEBUG(24299):          #16  pc 0004b68e  /system/lib/libandroid_runtime.so (_ZN7android14AndroidRuntime5startEPKcS2_)
12-11 10:21:39.717: INFO/DEBUG(24299):          #17  pc 00008f0a  /system/bin/app_process
12-11 10:21:39.717: INFO/DEBUG(24299):          #18  pc 0001685c  /system/lib/libc.so (__libc_init)

我对c ++编程很陌生,所以我做错了什么?感谢。

1 个答案:

答案 0 :(得分:0)

早先CCDictionary是autorelease调用,friendsList CCArray是静态的。可能是因为这些,记忆没有被清除。现在我使用新的和删除的静态friendsList CCArray创建CCDictionary并使用类对象访问它,如下所示。现在一切正常。

  

创建

  CCDictionary * cdictionary =  new CCDictionary();

  CCString *str1 = new CCString(nativeStringUserName);
  CCString *str2 = new CCString(nativeStringUserId);

  cdictionary->setObject(str1, "name");
  cdictionary->setObject(str2, "id");

  if(facebookFriendsListscreen->friendsList != NULL && facebookFriendsListscreen->friendsInstaledList != NULL) {
      facebookFriendsListscreen->friendsList->addObject(cdictionary);
      facebookFriendsListscreen->friendsInstaledList->addObject(cdictionary);
  } else {
      CCLog(" --- CCArrays are null in addInstalledFriends --- ");
  }
  

以同样的方式释放

  cdictionary->release();
  str1->release();
  str2->release();