gpt4 book ai didi

jobjectarray 返回方法的 C++ NDK 库内存管理

转载 作者:搜寻专家 更新时间:2023-10-31 02:05:50 25 4
gpt4 key购买 nike

//***** 新问题。 *****

当将结构传递给线程时,下面存在内存泄漏。不明白为什么,因为如果在主线程中直接调用线程内的代码不会泄漏内存。

class PeopleCounting{

// Class variables
Ptr<cv::BackgroundSubtractorMOG2> pMOG2 = cv::createBackgroundSubtractorMOG2(500, 16);
Mat maskBackgroundSubtracted = Mat(resizeDimension.height, resizeDimension.width, CV_8UC1);

// Thread creation code below, code called from main.

//Create thread
pthread_t threads;
pthread_attr_t attr;
void *status;

// Initialize and set thread joinable
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

// Creating thread data and initializing it
BackgroundSubstractionThreadData threadData = {CamImage, maskBackgroundSubtracted, pMOG2};
int rc;
rc = pthread_create(&threads, NULL, performBackgroundSubstraction, (void *)&threadData);
if (rc)
{
__android_log_print(ANDROID_LOG_ERROR, APPNAME, "Error: peopleCountingMainMono unable to create thread - %d",rc);
}
// free attribute and wait for the other threads
pthread_attr_destroy(&attr);

// ************** Do something else in main thread **************

// Join thread i.e. wait till completion of thread
rc = pthread_join(threads, &status);
if (rc)
{
__android_log_print(ANDROID_LOG_ERROR, APPNAME, "Error: peopleCountingMainMono unable to join - %d",rc);
}

// Using class variable **maskBackgroundSubtracted** and **pMOG2** for later use. **CamImage** (opencv mat) usually gets released automatically in general due to smart pointer implementation, not sure if it is the source of leak

}

// Note: Outside class
void *performBackgroundSubstraction(void *threadarg)
{
struct BackgroundSubstractionThreadData *my_data;
my_data = (struct BackgroundSubstractionThreadData *)threadarg;

Mat fgMask;
my_data->pMOG2F->apply(my_data->leftCamImage, fgMask, 0.002);

morphologyEx(fgMask, fgMask, MORPH_OPEN, getStructuringElement(MORPH_RECT, Size(3, 3)),Point(-1,-1),1);
morphologyEx(fgMask, fgMask, MORPH_CLOSE, getStructuringElement(MORPH_RECT, Size(11, 11)),Point(-1,-1),1);
threshold(fgMask, my_data->dst, 128, 255, THRESH_BINARY);
pthread_exit(NULL);
};

//***** 问题结束 ****

我有一个 NDK 库,它有一个返回 jobjectArray

的 JNI 函数

在下面的代码中,我使用了一个静态全局 jPeopleCountArray,它通过循环填充了 jobject 并返回到 Java 调用方法。这个 JNI 函数通过我的 Java 代码中的循环一次又一次地调用,但一次只调用一个实例,因此允许全局返回对象。我通过遍历 jobject 数组并删除 jobjects 的本地引用,最后删除 jPeopleCountArray 的全局引用,在库使用结束时执行内存清理。内存清理仅在最后执行,因为迭代使用(但仅限单个实例)允许重用返回对象。

问题是当我通过 NewObjectArray 分配全局 jobjectArray 时。由于之前的调用,之前保留在 jobjectArray 中的所有 jobject 是否从内存中释放?

class PeopleCounting{
public:
static inline jobjectArray jPeopleCountArray = NULL;
static inline JNI_PEOPLECOUNT * jniPeopleCount = NULL;
// .... Rest of Code ...
}


// JNI function
PeopleCounting *obj = (PeopleCounting *) hEngineHandle;

obj->LoadJniPeopleCount(env);
Mat *pMatCGray = (Mat *) addrCamGray;

vector<PeopleSegment> peopleCountingFromContourRes = obj->peopleCountingMainMono(
*pMatCGray);

// ******** IMPORTANT BELOW *********
obj->jPeopleCountArray = env->NewObjectArray(peopleCountingFromContourRes.size(),
obj->jniPeopleCount->cls, NULL);
for (size_t i = 0; i < peopleCountingFromContourRes.size(); i++) {
jobject jPeopleCount = env->NewObject(obj->jniPeopleCount->cls,
obj->jniPeopleCount->constructortorID);
obj->FillPeopleCountValuesToJni(env, jPeopleCount, peopleCountingFromContourRes[i]);
env->SetObjectArrayElement(obj->jPeopleCountArray, i, jPeopleCount);
}
return obj->jPeopleCountArray;



// Memory cleanup at the end of library use.
PeopleCounting *obj = (PeopleCounting *) hEngineHandle;
if (obj->jPeopleCountArray != NULL){
__android_log_print(ANDROID_LOG_VERBOSE, APPNAME,
"Freeing memory of jobject array");
//https://www.ibm.com/developerworks/library/j-jni/index.html
int size = env->GetArrayLength(obj->jPeopleCountArray);
for(int i = 0; i < size; i++)
{
jobject row = env->GetObjectArrayElement(obj->jPeopleCountArray, i);
if(env->ExceptionOccurred()) {
break;
}
env->DeleteLocalRef(row);
}
env->DeleteGlobalRef(obj->jPeopleCountArray);
}

delete (PeopleCounting *)(hEngineHandle);

最佳答案

您的代码可能会耗尽非常有限的本地引用表(其大小取决于实现,但可能低至 256)。

您可以在 SetObjectArrayElement(…, jPeopleCount) 之后的创建它的循环中删除对 jPeopleCount 的本地引用。另一方面,所有这些本地引用将在 JNI 函数返回 obj->jPeopleCountArray 后自动释放。

同样,删除对 obj->jPeopleCountArray 元素的局部引用的循环是多余的。在使用 GetObjectArrayElement() 创建这些之前,不存在要处理的本地引用。

这演示了局部引用和全局引用之间的行为差​​异。您不需要为 jobjectArray 的每个元素创建全局引用。但是,如果您将 jPeopleCount 对象存储在 C++ 集合(例如数组)中,则每个对象都需要全局引用。在这种情况下,清理代码将遍历集合并释放这些全局引用,类似于您的代码。

关于jobjectarray 返回方法的 C++ NDK 库内存管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51221738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com