我实际上遇到了一个我无法解决的问题。 我的应用程序启动后大约10秒后出现堆内存损坏。
我正在与JNI合作,我认为问题可能来自这里:
JNIEXPORT void JNICALL Java_net_coop_rouler_motion_MotionActivityEngineProxy_getMotionData(JNIEnv *env, jobject thiz, jbyteArray bufferOut, jobject annotations)
{
jbyte *cBufferOut = env->GetByteArrayElements(bufferOut, 0);
MotionActivityEngine *mae = getMotionActivityEngine(env, thiz);
bool boolMotionData[16];
mae->getMotionData(boolMotionData);
for (int i = 0; i < 16; ++i) {
cBufferOut[i] = boolMotionData[i] ? 1 : 0;
}
jclass clazz = env->GetObjectClass (annotations);
jmethodID mid = env->GetMethodID (clazz, "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;");
if (mid == 0)
return;
std::string a;
mae->getAnnotations(a, 9);
jstring _jstring = env->NewStringUTF (a.c_str());
env->CallObjectMethod (annotations, mid, _jstring);
env->ReleaseByteArrayElements(bufferOut, cBufferOut, JNI_ABORT);
}
有什么问题吗?也许我应该释放字符串,但我已经尝试过它似乎不起作用。
这是我的Logcat:
12-08 17:50:14.126: A/dalvikvm(10616): @@@ ABORTING: DALVIK: HEAP MEMORY CORRUPTION IN dispose_chunk addr=0x0
12-08 17:50:14.126: A/libc(10616): Fatal signal 11 (SIGSEGV) at 0xdeadbaad (code=1), thread 10663 (Thread-13334)
非常感谢任何帮助!
修改
以下是我的getMotionActivityEngine的代码:
static MotionActivityEngine * getMotionActivityEngine(JNIEnv *env, jobject thiz)
{
jclass clazz = env->GetObjectClass(thiz);
jfieldID fieldId = env->GetFieldID(clazz, "motionActivityEngine", "J");
jlong value = env->GetLongField(thiz, fieldId);
return (MotionActivityEngine *)value;
}
修改
这就是我在getMotionData之前做的事情:
byte motionData[] = new byte[8];
StringBuffer annotations = new StringBuffer("");
motionActivityEngine.getMotionData(motionData, annotations);