【发布时间】:2015-07-16 05:36:03
【问题描述】:
如果我注释掉方法 (*a)->CallVoidMethod(a, b, meth, "FROM JNI"); 应用程序不会崩溃,否则它会崩溃。
假设我没有释放可能导致内存泄漏的资源“jstr”,那么为什么只有在 CallVoidMethod() 时才会发生?
我应该怎么做才能解决这个问题? 感谢所有花时间写下解决方案或阅读本文的人
错误:
android.process.acore E/StrictMode:在附加的堆栈跟踪中获取了资源,但从未释放。有关避免资源泄漏的信息,请参阅 java.io.Closeable。
java.lang.Throwable:未调用显式终止方法“关闭”
这是我的原生“C”代码...
#include "com_example_prabhu_helloworldnative_HelloWorld.h"
#include <jni.h>
#include <android/log.h>
#define LOG_TAG "testjni"
#define ALOG(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
JNIEXPORT jstring JNICALL Java_com_example_prabhu_helloworldnative_HelloWorld_HelloJNI(JNIEnv *a, jobject b)
{
//jstring jstr = (*a)->NewStringUTF(a, "This comes from jni....");
jclass clazz =(*a)->GetObjectClass(a, b);
jmethodID meth = (*a)->GetMethodID(a, b, "messageMe", "(Ljava/lang/String;)V");
__android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "\n this is log messge \n");
ALOG("Hello World");
if(meth == 0)
{
return;
}
(*a)->CallVoidMethod(a, b, meth, "FROM JNI");
//(*a)->Release
ALOG("REACHING HERE");
return (*a)->NewStringUTF(a, "APXOR");
}
【问题讨论】:
标签: java android memory-leaks android-ndk java-native-interface