Android 当我尝试复制一个字符串时 NDK strcpy 崩溃
Android NDK strcpy crashes when I try to put copy a String
这就是我想要做的:
static char* callerURI; //declared globally
strcpy(callerURI, reqParticipants.URI[idUser]);
其中:
@var VidyoClientRequestParticipants::URI [out]
Array of values for Vidyo URI of remote participants in a conference.
我是从 SDK 中获取的,所以不确定它是什么。
如果我记录:LOGI("VIDYO_CLIENT_OUT_EVENT_VIDEO_FRAME_RECEIVED AskViewType callerURI 1 REAL: %s" ,reqParticipants.URI[idUser]);
我会得到一个字符串。
现在如果我这样做:
callerURI = reqParticipants.URI[idUser];
这将起作用,并将该字符串设置为我的 callerURI,但它以某种方式未分配。所以我认为 strcpy 是执行此操作的正确方法。
但是尝试 strcpy(callerURI, reqParticipants.URI[idUser]);
会导致此错误:
06-13 05:14:08.044: E/SQLiteDatabase(1165): Error inserting value=true key=com.vidyo.vidyomod
06-13 05:14:08.044: E/SQLiteDatabase(1165): android.database.sqlite.SQLiteException: no such table: KeyValueMap (code 1): , while compiling: INSERT INTO KeyValueMap(value,key) VALUES (?,?)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at com.jb.ga0.commerce.util.io.DataBaseHelper.insert(ZeroCamera:323)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at com.jiubang.commerce.tokencoin.database.d.a(KeyValueMapTable.java:47)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at com.jiubang.commerce.tokencoin.integralwall.c.onAppUninstalled(AppAdsDataManager.java:101)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at com.jb.ga0.commerce.util.observer.AppChangeObserver.notifyAppUninstalled(ZeroCamera:129)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at com.jb.ga0.commerce.util.observer.AppChangeObserver.onReceive(ZeroCamera:158)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at com.jb.ga0.commerce.util.observer.AppChangeObserver.access0(ZeroCamera:18)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at com.jb.ga0.commerce.util.observer.AppChangeObserver$AppChangedReceiver.onReceive(ZeroCamera:215)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:768)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at android.os.Handler.handleCallback(Handler.java:733)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at android.os.Handler.dispatchMessage(Handler.java:95)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at android.os.Looper.loop(Looper.java:136)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at java.lang.reflect.Method.invokeNative(Native Method)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at java.lang.reflect.Method.invoke(Method.java:515)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at dalvik.system.NativeStart.main(Native Method)
编辑:
我试过这个:
LOGI("VIDYO_CLIENT_OUT_EVENT_VIDEO_FRAME_RECEIVED AskViewType callerURI 1 REAL: %s" ,reqParticipants.URI[idUser]);
char* callerTemp = reqParticipants.URI[idUser];
LOGI("VIDYO_CLIENT_OUT_EVENT_VIDEO_FRAME_RECEIVED AskViewType callerURI 12 REAL: %s" ,callerTemp);
LOGI("VIDYO_CLIENT_OUT_EVENT_VIDEO_FRAME_RECEIVED AskViewType callerURI 13 REAL: %s" ,strlen(callerTemp));
*callerURI= (char *) malloc(sizeof(char *) * (strlen(callerTemp) + 1 ));
strcpy(callerURI, reqParticipants.URI[idUser]);
LOGI("VIDYO_CLIENT_OUT_EVENT_VIDEO_FRAME_RECEIVED AskViewType callerURI 2 REAL: %s" ,reqParticipants.URI[idUser]);
LOGI("VIDYO_CLIENT_OUT_EVENT_VIDEO_FRAME_RECEIVED AskViewType callerURI 2 : %s" ,callerURI);
但我只记录了这个:
06-13 05:40:27.294: I/VidyoMobile app/src/main/jni/ndkVidyoSample.c(19744): VIDYO_CLIENT_OUT_EVENT_VIDEO_FRAME_RECEIVED AskViewType callerURI 1 REAL: scip:CsAPI1528869039-4c6-42722300022166be-d30ff886a73e0ebc;transport=TLS
06-13 05:40:27.294: I/VidyoMobile app/src/main/jni/ndkVidyoSample.c(19744): VIDYO_CLIENT_OUT_EVENT_VIDEO_FRAME_RECEIVED AskViewType callerURI 12 REAL: scip:CsAPI1528869039-4c6-42722300022166be-d30ff886a73e0ebc;transport=TLS
所以 strlen() 也因为某种原因崩溃了。这是为什么?
static char* callerURI;
这里你并没有分配内存,所以callerURI
只是一个擦鞋垫,而不是可以进入的房子。
在调用 strcpy
之前,您必须为 callerURI
分配内存。也许你可以使用 reqParticipants.URI[idUser]
找出你需要分配的内存。
这就是我想要做的:
static char* callerURI; //declared globally
strcpy(callerURI, reqParticipants.URI[idUser]);
其中:
@var VidyoClientRequestParticipants::URI [out]
Array of values for Vidyo URI of remote participants in a conference.
我是从 SDK 中获取的,所以不确定它是什么。
如果我记录:LOGI("VIDYO_CLIENT_OUT_EVENT_VIDEO_FRAME_RECEIVED AskViewType callerURI 1 REAL: %s" ,reqParticipants.URI[idUser]);
我会得到一个字符串。
现在如果我这样做:
callerURI = reqParticipants.URI[idUser];
这将起作用,并将该字符串设置为我的 callerURI,但它以某种方式未分配。所以我认为 strcpy 是执行此操作的正确方法。
但是尝试 strcpy(callerURI, reqParticipants.URI[idUser]);
会导致此错误:
06-13 05:14:08.044: E/SQLiteDatabase(1165): Error inserting value=true key=com.vidyo.vidyomod
06-13 05:14:08.044: E/SQLiteDatabase(1165): android.database.sqlite.SQLiteException: no such table: KeyValueMap (code 1): , while compiling: INSERT INTO KeyValueMap(value,key) VALUES (?,?)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at com.jb.ga0.commerce.util.io.DataBaseHelper.insert(ZeroCamera:323)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at com.jiubang.commerce.tokencoin.database.d.a(KeyValueMapTable.java:47)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at com.jiubang.commerce.tokencoin.integralwall.c.onAppUninstalled(AppAdsDataManager.java:101)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at com.jb.ga0.commerce.util.observer.AppChangeObserver.notifyAppUninstalled(ZeroCamera:129)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at com.jb.ga0.commerce.util.observer.AppChangeObserver.onReceive(ZeroCamera:158)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at com.jb.ga0.commerce.util.observer.AppChangeObserver.access0(ZeroCamera:18)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at com.jb.ga0.commerce.util.observer.AppChangeObserver$AppChangedReceiver.onReceive(ZeroCamera:215)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:768)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at android.os.Handler.handleCallback(Handler.java:733)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at android.os.Handler.dispatchMessage(Handler.java:95)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at android.os.Looper.loop(Looper.java:136)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at java.lang.reflect.Method.invokeNative(Native Method)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at java.lang.reflect.Method.invoke(Method.java:515)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
06-13 05:14:08.044: E/SQLiteDatabase(1165): at dalvik.system.NativeStart.main(Native Method)
编辑:
我试过这个:
LOGI("VIDYO_CLIENT_OUT_EVENT_VIDEO_FRAME_RECEIVED AskViewType callerURI 1 REAL: %s" ,reqParticipants.URI[idUser]);
char* callerTemp = reqParticipants.URI[idUser];
LOGI("VIDYO_CLIENT_OUT_EVENT_VIDEO_FRAME_RECEIVED AskViewType callerURI 12 REAL: %s" ,callerTemp);
LOGI("VIDYO_CLIENT_OUT_EVENT_VIDEO_FRAME_RECEIVED AskViewType callerURI 13 REAL: %s" ,strlen(callerTemp));
*callerURI= (char *) malloc(sizeof(char *) * (strlen(callerTemp) + 1 ));
strcpy(callerURI, reqParticipants.URI[idUser]);
LOGI("VIDYO_CLIENT_OUT_EVENT_VIDEO_FRAME_RECEIVED AskViewType callerURI 2 REAL: %s" ,reqParticipants.URI[idUser]);
LOGI("VIDYO_CLIENT_OUT_EVENT_VIDEO_FRAME_RECEIVED AskViewType callerURI 2 : %s" ,callerURI);
但我只记录了这个:
06-13 05:40:27.294: I/VidyoMobile app/src/main/jni/ndkVidyoSample.c(19744): VIDYO_CLIENT_OUT_EVENT_VIDEO_FRAME_RECEIVED AskViewType callerURI 1 REAL: scip:CsAPI1528869039-4c6-42722300022166be-d30ff886a73e0ebc;transport=TLS
06-13 05:40:27.294: I/VidyoMobile app/src/main/jni/ndkVidyoSample.c(19744): VIDYO_CLIENT_OUT_EVENT_VIDEO_FRAME_RECEIVED AskViewType callerURI 12 REAL: scip:CsAPI1528869039-4c6-42722300022166be-d30ff886a73e0ebc;transport=TLS
所以 strlen() 也因为某种原因崩溃了。这是为什么?
static char* callerURI;
这里你并没有分配内存,所以callerURI
只是一个擦鞋垫,而不是可以进入的房子。
在调用 strcpy
之前,您必须为 callerURI
分配内存。也许你可以使用 reqParticipants.URI[idUser]
找出你需要分配的内存。