Ask Your Question
0

Assigning OpenCV Mat object causes a memory leak

asked 2015-10-18 14:20:45 -0600

adhamenaya gravatar image

I want to assign frame (Mat object) from function parameter into object variable, as shown in the code below. But this function should be called may times (for each frame from the video camera). but this line

this->nFrame = frame;

causes a memory leak (when commented there is no error!).

NOTE The function setCurrentFrame is called inside JNI function, where this JNI function is called every time I want to process the frame from the video camera.

The JNI function is like:

JNIEXPORT jbyteArray JNICALL Java_com_adhamenaya_Native_run(JNIEnv * env,
        jobject obj, jstring faceCascadeFile, jstring noseCascadeFile,
        jstring landmarks, jlong frame) {

    MyClass gsys;
    cv::Mat& inFrame = *(cv::Mat*) frame;
    gsys.setCurrentFrame(inFrame);


    // SOME PROCESSING FOR THE FRAME 


    inFrame.release();
    gsys.release();

    ......
    ......
}

The code for C++ function (setCurrentFrame)

void MyClass::setCurrentFrame(cv::Mat& frame) {
    cv::Size2d imgRes;
    float resRatio;

    if (frame.cols > frame.rows) {
        //landscape
        imgRes.width = 640.0f;
        resRatio = frame.cols / 640.0f;
        imgRes.height = floor(frame.rows / resRatio);
    } else {
        //portrait
        imgRes.height = 640.0f;
        resRatio = frame.rows / 640.0f;
        imgRes.width = floor(frame.cols / resRatio);
    }

    //save scaled height, width for further use
    this->frameWidth = nFrame.cols;
    this->frameHeight = nFrame.rows;

    //set frame and increment frameCount
    this->nFrame = frame;
    this->frameCount++;
}

Kindly, can you help me to solve this problem ? I tried to release the frame by calling :

void MyClass::release(void) {
    this->nFrame = cv::Mat();
}

nothing happened, even like this:

void MyClass::release(void) {
    this->nFrame.release();
}

Still the same error!

edit retag flag offensive close merge delete

Comments

I'm not familiar with java but this line cv::Mat& inFrame = *(cv::Mat*) frame; mean frame value are copying in inFrame. After you release inFrame. i think with this line you release frame too.

LBerger gravatar imageLBerger ( 2015-10-18 15:32:20 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-10-19 06:24:10 -0600

thdrksdfthmn gravatar image

Your problem is linked to the line:

  this->nFrame = frame;

(as you mentioned it) and it is because cv::Mat m = otherMat is not doing a deep copy, it is just pointing to the same address as otherMat, so if you modify the otherMat you'll get a modification in this->nFrame.

For doing a deep copy you need to use copyTo or clone functions:

  this->nFrame = frame.clone();

What I do not understand is: Have you created a C++ class that is used by JNI to make it a Java class? Otherwise why talking about java and JNI while posting c++ code?

edit flag offensive delete link more

Comments

What I don't understand "C++ class that is used by JNI to make it a Java class" .. What do you mean in to make it a Java class ?!

adhamenaya gravatar imageadhamenaya ( 2015-10-19 10:10:41 -0600 )edit

I meant interpret it in Java :) I have not done JNI ever, but I heard that it may "transform" c++ code to be readable/used by Java

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-10-20 03:53:33 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-10-18 14:20:45 -0600

Seen: 3,829 times

Last updated: Oct 19 '15