0

我正在尝试将一些数据(从 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++ 编程很陌生,所以我做错了什么?谢谢。

4

1 回答 1

0

早些时候CCDictionary是自动释放调用,friendsListCCArray是静态的。可能是因为这些,内存没有被清除。现在我正在CCDictionary使用新的和删除的静态 FriendsList CCArray 创建并使用类对象访问它,如下所示。现在一切正常。

创造

  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();
于 2013-01-03T11:32:34.460 回答