2

我正在尝试在我的 c++ 类中实现 facebook 登录。从 java 我正在尝试获取用户的 facebook 信息。一切都正确地从 Java 传递到 C++。

但是,当这段代码未注释时,我的应用程序因信号 11 而崩溃(根据此处其他类似答案的内存问题)

    JNIEXPORT void JNICALL Java_com_humit_android_HumIt_javaCallJNI(JNIEnv* env, jobject obj,
                                        jstring javaString1, jstring javaString2, jstring javaString3)
{
    //Get the native string from javaString
     const char *nativeString1 = env->GetStringUTFChars(javaString1, 0);
     const char *nativeString2 = env->GetStringUTFChars(javaString2, 0);
     const char *nativeString3 = env->GetStringUTFChars(javaString3, 0);

// 下面 2 行给出错误...如果有注释,没问题...!

    LoginScreen * loginScreen = new LoginScreen();
    loginScreen->facebookProfileInfoFetch("hi", "hii", "hii");




    env->ReleaseStringUTFChars(javaString1, nativeString1);
    env->ReleaseStringUTFChars(javaString2, nativeString2);
    env->ReleaseStringUTFChars(javaString3, nativeString3);

}

facebookProfileInfoFetch 是:

void LoginScreen::facebookProfileInfoFetch(const char * value1, const char * value2, const char * value3) {
LOGD(" ----------------- facebookProfileInfoFetch ------------- %s ------------ ", value1);
LOGD(" ----------------- facebookProfileInfoFetch ------------- %s ------------ ", value2);
LOGD(" ----------------- facebookProfileInfoFetch ------------- %s ------------ ", value3);

facebookUserName = value1;
facebookUserId = value2;
facebookUserGender = value3;

this->loginHumServer();

}

调用 loginHumServer() 时崩溃:

loginhumserver() 是:

void LoginScreen::loginHumServer()
{
LOGD(" ----------------- loginHumServer ------------------------- ");
facebookDataRetrievedCount++;

if(facebookDataRetrievedCount<2)
    return;


CCHttpRequest* request = new CCHttpRequest();
LOGD(" ----------------- constructing url ------------------------- ");
std::string url="http://humsrer.co.in/user/sign_in?";
url=url+"name"+"="+facebookNoSpaceUserName+"&";
url=url+"facebook_thumbnail"+"="+facebookUserPictureUrl+"&";
url=url+"facebook_id"+"="+facebookUserId+"&";
url=url+"gender"+"="+facebookUserGender+"&";
url=url+"ios_push_id"+"="+"12346";

CCLog("UUUUUUUUUUUUUUUUUUU %s ",url.c_str());
request->setUrl(url.c_str());
request->setRequestType(CCHttpRequest::kHttpGet);
request->setResponseCallback(this, callfuncND_selector(LoginScreen::onHttpRequestCompleted));
// optional fields
request->setTag("GET test2");
LOGD(" ----------------- loginHumServer getInstance ------------------------- ");
CCHttpClient::getInstance()->send(request);

// don't forget to release it, pair to new
request->release();
LOGD(" ----------------- loginHumServer request->release ------------------------- ");
loginLabel->setString("Loging to hum server..");
}

这是应用程序崩溃时我得到的 logcat 错误:

 12-06 13:49:45.380: INFO/DEBUG(5637): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr deadbaad
12-06 13:49:45.380: INFO/DEBUG(5637):  r0 deadbaad  r1 01ddc9e8  r2 40000000  r3 00000000
12-06 13:49:45.380: INFO/DEBUG(5637):  r4 00000000  r5 00000027  r6 00000000  r7 57797f3c
12-06 13:49:45.380: INFO/DEBUG(5637):  r8 5c6d9c10  r9 57797f34  10 00000008  fp 5c6d9c24
12-06 13:49:45.380: INFO/DEBUG(5637):  ip ffffffff  sp 5c6d9a90  lr 400ff649  pc 400fb9d8  cpsr 60000030
12-06 13:49:45.380: INFO/DEBUG(5637):  d0  4448000044480000  d1  0000000000000000
12-06 13:49:45.380: INFO/DEBUG(5637):  d2  449a0000449a0000  d3  000004d0c41a0000
12-06 13:49:45.380: INFO/DEBUG(5637):  d4  3f8000004b7fffff  d5  449a000044480000
12-06 13:49:45.380: INFO/DEBUG(5637):  d6  4499e0004447c000  d7  0000000000000000
12-06 13:49:45.380: INFO/DEBUG(5637):  d8  0000000000000000  d9  0000000000000000
12-06 13:49:45.380: INFO/DEBUG(5637):  d10 0000000000000000  d11 0000000000000000
12-06 13:49:45.380: INFO/DEBUG(5637):  d12 0000000000000000  d13 0000000000000000
12-06 13:49:45.380: INFO/DEBUG(5637):  d14 0000000000000000  d15 0000000000000000
12-06 13:49:45.380: INFO/DEBUG(5637):  d16 5000000000000000  d17 0000000000000000
12-06 13:49:45.380: INFO/DEBUG(5637):  d18 0000000000000000  d19 0000000000000000
12-06 13:49:45.380: INFO/DEBUG(5637):  d20 0000000000000000  d21 0000000000000000
12-06 13:49:45.380: INFO/DEBUG(5637):  d22 0000000000000000  d23 0000000000000000
12-06 13:49:45.380: INFO/DEBUG(5637):  d24 0000000000000000  d25 0000000000000000
12-06 13:49:45.380: INFO/DEBUG(5637):  d26 0000000000000000  d27 0000000000000000
12-06 13:49:45.380: INFO/DEBUG(5637):  d28 0000000000000000  d29 0000000000000000
12-06 13:49:45.380: INFO/DEBUG(5637):  d30 0000000000000000  d31 0000000000000000
12-06 13:49:45.380: INFO/DEBUG(5637):  scr 60000010
12-06 13:49:45.650: INFO/DEBUG(5637):          #00  pc 000179d8  /system/lib/libc.so
12-06 13:49:45.650: INFO/DEBUG(5637):          #01  pc 0001ef44  /system/lib/libc.so (__assert2)
12-06 13:49:45.650: INFO/DEBUG(5637):          #02  pc 000fb552  /data/data/com.humit/lib/libgame.so (_ZN11LoginScreen22onHttpRequestCompletedEPN7cocos2d6CCNodeEPv)
12-06 13:49:45.655: INFO/DEBUG(5637):          #03  pc 001c3e32  /data/data/com.humit/lib/libgame.so (_ZN7cocos2d9extension12CCHttpClient25dispatchResponseCallbacksEf)
12-06 13:49:45.655: INFO/DEBUG(5637):          #04  pc 000ff9aa  /data/data/com.humit/lib/libgame.so (_ZN7cocos2d7CCTimer6updateEf)
12-06 13:49:45.655: INFO/DEBUG(5637):          #05  pc 001053d6  /data/data/com.humit/lib/libgame.so (_ZN7cocos2d11CCScheduler6updateEf)
12-06 13:49:45.655: INFO/DEBUG(5637):          #06  pc 0012f7f8  /data/data/com.humit/lib/libgame.so (_ZN7cocos2d10CCDirector9drawSceneEv)
12-06 13:49:45.660: INFO/DEBUG(5637):          #07  pc 00130df8  /data/data/com.humit/lib/libgame.so (_ZN7cocos2d21CCDisplayLinkDirector8mainLoopEv)
12-06 13:49:45.660: INFO/DEBUG(5637):          #08  pc 0016c66c  /data/data/com.humit/lib/libgame.so (Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeRender)
12-06 13:49:45.660: INFO/DEBUG(5637):          #09  pc 0001ed70  /system/lib/libdvm.so (dvmPlatformInvoke)
12-06 13:49:45.660: INFO/DEBUG(5637):          #10  pc 0005902c  /system/lib/libdvm.so (_Z16dvmCallJNIMethodPKjP6JValuePK6MethodP6Thread)

我无法调试它崩溃的原因。请帮忙。

编辑 我添加了 onHttpRequestCompleted 方法:

void LoginScreen::onHttpRequestCompleted(CCNode *sender, void *data)
{
    LOGD(" ----------------- LoginScreen onHttpRequestCompleted ------------------------- ");
    CCLOG("request completed");

    CCHttpResponse *response = (CCHttpResponse*)data;

    if (!response)
    {
        LOGD(" ----------------- LoginScreen onHttpRequestCompleted return ------------------------- ");
        return;
    }

    // You can get original request type from: response->request->reqType
    if (0 != strlen(response->getHttpRequest()->getTag()))
    {
        CCLog("%s completed", response->getHttpRequest()->getTag());
    }

    int statusCode = response->getResponseCode();
    char statusString[64] = {};
    sprintf(statusString, "HTTP Status Code: %d, tag = %s", statusCode, response->getHttpRequest()->getTag());
    //m_labelStatusCode->setString(statusString);
    CCLog("response code: %d", statusCode);

    if (!response->isSucceed())
    {
        CCLog(" %s response failed", response->getResponseData());
        CCLog("error buffer: %s", response->getErrorBuffer());
        LOGD(" ----------------- LoginScreen onHttpRequestCompleted response failed ------------------------- ");
        return;
    }

     //dump data
    std::vector<char> *buffer = response->getResponseData();

    char * charbuffer = new char[buffer->size()];

    printf("Http Test, dump data: ");

    unsigned int size=0;
    for (unsigned int i = 0; i < buffer->size(); i++)
    {
        charbuffer[i] = (*buffer)[i];
        size=i+1;
    }

    charbuffer[size] = 0;

    char *errorPos = 0;
    char *errorDesc = 0;
    int errorLine = 0;
    block_allocator allocator(1 << 10); // 1 KB per block

    json_value *root = json_parse(charbuffer, &errorPos, &errorDesc, &errorLine, &allocator);

    root = root->first_child;

    assert(root->type ==JSON_STRING );

    assert(std::string(root->name) == "user_id" );

    loginLabel->setString("Loading active games..");
    GameManager::create(root->json_value::string_value,facebookUserId.c_str(),facebookUserName.c_str());

//  GameManager::create("b37e329c-37ac-11e2-abf1-1231381a9805");

    delete[] charbuffer;

    this->next();

}
4

2 回答 2

1

assert() 正在触发。您在一个非常具体的地址失败:

INFO/DEBUG(5637): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr deadbaad

0xdeadbaad由仿生 abort() 函数使用。(不是每个人都喜欢这个 - 请参阅http://code.google.com/p/android/issues/detail?id=16672。)

堆栈跟踪会告诉您失败的地方:

INFO/DEBUG(5637):          #00  pc 000179d8  /system/lib/libc.so
INFO/DEBUG(5637):          #01  pc 0001ef44  /system/lib/libc.so (__assert2)
INFO/DEBUG(5637):          #02  pc 000fb552  /data/data/com.humit/lib/libgame.so (_ZN11LoginScreen22onHttpRequestCompletedEPN7cocos2d6CCNodeEPv)
INFO/DEBUG(5637):          #03  pc 001c3e32  /data/data/com.humit/lib/libgame.so (_ZN7cocos2d9extension12CCHttpClient25dispatchResponseCallbacksEf)

注意__assert2,在仿生的 <assert.h> 中定义。

它是从 调用的LoginScreen::onHttpRequestCompleted(cocos2d::CCNode*, void*)。您可以使用带有库中符号的地址转行工具将其转换为特定的行号,但理想情况下,失败的断言本身将出现在本机崩溃上方的 logcat 中。

于 2012-12-11T00:55:25.500 回答
0

您在某处有一个指向无效位置的指针。这就是导致分段错误的原因。似乎崩溃始于LoginScreen::onHttpRequestCompleted. 你能把那个贴出来吗?

于 2012-12-06T16:13:28.270 回答