简体   繁体   中英

Crash initializing std::String

Though Android-NDK (JNI of Android) I'm trying to use the STL to make it easir work with strings.

The following code, crashes on execution:

    __android_log_print(ANDROID_LOG_DEBUG, "RMSDK:RMServices", "[%s]", "Converting");
    std::string str("mark");
    __android_log_print(ANDROID_LOG_DEBUG, "RMSDK:RMServices", "[%s]", str);

When executing, the following stack prints.

DEBUG/RMSDK:RMServices(11786): [Converting]
INFO/DEBUG(31): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
INFO/DEBUG(31): Build fingerprint: 'generic/sdk/generic/:2.2/FRF91/43546:eng/test-keys'
INFO/DEBUG(31): pid: 11786, tid: 11786  >>> br.com.iba <<<
INFO/DEBUG(31): signal 11 (SIGSEGV), fault addr deadbaad
INFO/DEBUG(31):  r0 00000000  r1 00000000  r2 00000027  r3 00000000
INFO/DEBUG(31):  r4 00000000  r5 deadbaad  r6 00001728  r7 4618bd80
INFO/DEBUG(31):  r8 00261938  r9 002a5df0  10 00000000  fp 00000000
INFO/DEBUG(31):  ip ffffffff  sp beb41880  lr afd154c5  pc afd11dc4  cpsr 40000030
INFO/DEBUG(31):          #00  pc 00011dc4  /system/lib/libc.so
INFO/DEBUG(31):          #01  lr afd154c5  /system/lib/libc.so
INFO/DEBUG(31): code around pc:
INFO/DEBUG(31): afd11da4 1c2bd00b 2d00682d e026d1fb 2b0068db 

As I can see, the "[Converting]" get printed then crashes on the std::String initialization.

PS: On my Application.MK (under jni folder) I has the following line:

APP_STL := gnustl_static

You can't pass a std::string to __android_log_print's "%s" formatter - it expects you to pass it a char * . Using std::string's c_str() will do the job:

std::string str("mark");
__android_log_print(ANDROID_LOG_DEBUG, "RMSDK:RMServices", "[%s]", str.c_str());

Are you perhaps running on 2.1 or older? There is a bug that has been fixed for the next NDK that sounds like your problem. See the link below for the related fix.

https://review.source.android.com//#change,21309

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM