【问题标题】:Failed to set object for CCDictionary in cocos2dx在 cocos2dx 中为 CCDictionary 设置对象失败
【发布时间】:2012-12-11 10:32:42
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    之前的CCDictionary 是自动释放调用,而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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多