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

时间:2018-07-07 09:23:31

标签: c++ memory-management memory-leaks android-ndk java-native-interface

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

将结构传递给线程时,下面存在内存泄漏。无法理解原因,因为如果直接在主线程中调用该线程内的代码不会泄漏内存。

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);
  };

// *****问题结尾****

我有一个带有JNI函数的NDK库,该函数返回 jobjectArray

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

问题是当我通过 NewObjectArray 分配全局 jobjectArray 时。由于先前的调用,先前保存在jobjectArray中的所有工作对象是否从内存中释放出来?

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);

1 个答案:

答案 0 :(得分:2)

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

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

类似地,删除对 obj-> jPeopleCountArray 元素的本地引用的循环是多余的。使用 GetObjectArrayElement()创建本地引用之前,没有本地引用可以处理。

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