Ask Your Question

Assigning OpenCV Mat object causes a memory leak

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

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;




The code for C++ function (setCurrentFrame)

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

    if (frame.cols > frame.rows) {
        imgRes.width = 640.0f;
        resRatio = frame.cols / 640.0f;
        imgRes.height = floor(frame.rows / resRatio);
    } else {
        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;

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

Still the same error!

edit retag flag offensive close merge delete


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 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

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

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


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 -0500 )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 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


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

Seen: 2,051 times

Last updated: Oct 19 '15