Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Possible memory leak cause - Videocapture ??

I am trying to use opencv for simple image capture and saving the image. I am fairly new to OPENCV and currently using 2.4.11 version.

It has been noted whenever i close my camera and open it - i notice memory leak and thread count increase in windows resource monitor.However, if i open my camera once and capture images i do not notice leak in the system. so i doubt videocapture. Request some lights in addressing this leak.

However, after fixing this leak will have to create a CPP wrapper to access api via C

Platform : Win 7 32 bit compiler : Mingw Gcc OPenCv : 2.4.11

#include <iostream>
#include <stdio.h>
using namespace std;
#include "cameraCore.h"
#include "opencv2/highgui/highgui.hpp"
#include <windows.h>
using namespace cv;
VideoCapture *cap = NULL;
int cameraClass::closeCamhandle()
{
    try
    {
        if(cap != NULL)
        {
        if (cap->isOpened())  // if not success, exit program
        {
            cap->release();
            //return 0;
        }

            delete(cap);
            cap = NULL;
        }
        return 0;
    }
    catch ( cv::Exception &e )
    {
          const char* err_msg = e.what();
          cout << "exception caught: " << err_msg << endl;
    }
           return 0;
}

bool cameraClass::setCamhandle(int camId)
{
     try
     {
         bool bSuccess= FALSE;
        cap=new VideoCapture();
        bSuccess=cap->open(camId);
        if(bSuccess == TRUE)
        {
            cout << "!!!Camera open success!!!" << endl;
        }else{
            cout << "!!!Camera open FASILURE!!!" << endl;
        }
        return bSuccess;
    }
    catch ( cv::Exception & e )
    {
          const char* err_msg = e.what();
          cout << "exception caught: " << err_msg << endl;
    }
           return FALSE;
}

int cameraClass::captureImage(char *imageName, int roatation)
{
     try
     {
         cout << "ImageName " << imageName << endl;
         char image[50] ={0};
        if (strlen(imageName) > 1 )
        {
            strcpy(image,imageName);
        }
        bool bSuccess= FALSE;
        if (!cap->isOpened())  // if not success, exit program
        {
            cout  << "Cannot open the video file" << endl;
            return -1;
        }
        double dWidth = cap->get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
        double dHeight = cap->get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the  video
        cout << "Frame size : " << dWidth << " x " << dHeight << endl;
        if (dWidth != frameWidth)
        {
            cap->set(CV_CAP_PROP_FRAME_WIDTH,frameWidth);
        }
        if (dHeight != frameHeight)
        {
            cap->set(CV_CAP_PROP_FRAME_HEIGHT,frameHeight);
        }
       Mat *frame = new Mat;
       int frameCount = 20;
       while (!bSuccess && frameCount)
       {
           cout << "Data for fame missing " << frameCount << endl;
           bSuccess = cap->grab();
           if(bSuccess)
               bSuccess = cap->retrieve(*frame, 0);
           if(bSuccess)
               break;
           frameCount--;
           Sleep(5);
       }
       if (!bSuccess) //if not succes->, break loop
        {
         cout << "Cannot read a frame from video file" << endl;
         frame->release();
         delete(frame);
         return -2;
        }
       bSuccess=imwrite(image,*frame);
       frame->release();
       delete(frame);
       return 0;
     }
    catch ( cv::Exception & e )
    {
          const char* err_msg = e.what();
          cout << "exception caught: " << err_msg << endl;
    }
    return 0;
}


int main()
{
    int camOpenStatus = FALSE;
    int status=0;
    cameraClass *newCamHandle = new cameraClass(status=newCamHandle->setCamhandle(0);
    int captureCount=1;
    int failCount=0;
    char name[30] = {0};
    while (captureCount != 678)
    {

        if(captureCount % 13 == 0  || camOpenStatus == FALSE)
        {
            cout << "!!!Cam handle success!!!" << endl; // prints !!!Hello World!!!
            newCamHandle->closeCamhandle();
            Sleep(1*1000);
            status=newCamHandle->setCamhandle(0);
            if(status == TRUE)
            {
                cout << "!!!Cam handle success!!!" << endl; // prints !!!Hello World!!!
                camOpenStatus=TRUE;
            }
            else
            {
                cout << "!!!CAM handle failure!!!" << endl; // prints !!!Hello World!!!

            }
        }
        if(captureCount % 4 == 0)
            Sleep(10*1000);
        sprintf(name,"photo%d.jpg",captureCount);
        status = newCamHandle->captureImage(name,0);
        if(status == 0)
        {
            cout << "!!!CIamge capture success!!!" << endl; // prints !!!Hello World!!!
        }
        else
        {
            cout << "!!!CIamge capture failure!!!" << endl; // prints !!!Hello World!!!
            failCount++;
            if(failCount>2)
                break;
        }
        Sleep(0.6*1000);
        captureCount++;
    }
    newCamHandle->closeCamhandle();
    delete(newCamHandle);
    Sleep(30*1000);
    return 0;
}

Header file

#ifndef CAMERACLASS_H_
#define CAMERACLASS_H_
//#include "opencv2/highgui/highgui.hpp"
//#include "opencv2/highgui/highgui.hpp"
//using namespace cv;

class cameraClass
{
public:
    //VideoCapture cap;
    int frameWidth;
    int frameHeight;
    int cameraModule;

    cameraClass()
    {
        cameraModule=0;
        frameWidth=320;
        frameHeight=240;
    }
    cameraClass(int cameraModel)
    {
        cameraModule=cameraModel;
        frameWidth=320;
        frameHeight=240;
    }

    cameraClass( int frameWid, int frameHght)
    {

        frameWidth=frameWid;
        frameHeight=frameHght;
    }

    cameraClass(int camModel, int frameWid, int frameHght)
    {
        cameraModule=camModel;
        frameWidth=frameWid;
        frameHeight=frameHght;
    }

    int captureImage(char *imageName, int roatation);
    bool setCamhandle(int camId);
    int closeCamhandle();

};
#endif /* CAMERACLASS_H_ */

Possible memory leak cause - Videocapture ??

I am trying to use opencv for simple image capture and saving the image. I am fairly new to OPENCV and currently using 2.4.11 version.

It has been noted whenever i close my camera and open it - i notice memory leak and thread count increase in windows resource monitor.However, if i open my camera once and capture images i do not notice leak in the system. so i doubt videocapture. Request some lights in addressing this leak.

However, after fixing this leak will have to create a CPP wrapper to access api via C

Platform : Win 7 32 bit compiler : Mingw Gcc OPenCv : 2.4.11

#include <iostream>
#include <stdio.h>
using namespace std;
#include "cameraCore.h"
#include "opencv2/highgui/highgui.hpp"
#include <windows.h>
using namespace cv;
VideoCapture *cap = NULL;
int cameraClass::closeCamhandle()
{
    try
    {
         if(cap != NULL)
        {
         if (cap->isOpened()) (cap->isOpened() )  // if not success, exit program
        {
    {
                cap->release();
            //return 0;
        }

}
            delete(cap);
            cap = NULL;
        }
        return 0;
    }
    catch ( cv::Exception &e )
    {
          const char* err_msg = e.what();
          cout << "exception caught: " << err_msg << endl;
    }
           return 0;
 }

bool cameraClass::setCamhandle(int camId)
{
     try
     {
         bool bSuccess= FALSE;
     try
     {
        cap=new VideoCapture();
        bSuccess=cap->open(camId);
        if(bSuccess == TRUE)
        {
            cout << "!!!Camera open success!!!" << endl;
        }else{
            delete(cap);
            cap=NULL;
            cout << "!!!Camera open FASILURE!!!" << endl;
        }
        return bSuccess;
    }
    catch ( cv::Exception & e )
    {
          const char* err_msg = e.what();
          cout << "exception caught: " << err_msg << endl;
    }
             if(cap != NULL){
              delete(cap);
              cap=NULL;
          }
    }
    return FALSE;
bSuccess;
}

int cameraClass::captureImage(char *imageName, int roatation)
{
     try
     {
         cout << "ImageName " << imageName << endl;
         char image[50] ={0};
        if (strlen(imageName) > 1 )
        {
            strcpy(image,imageName);
        }
        bool bSuccess= FALSE;
        if (!cap->isOpened())  // if not success, exit program
        {
            cout  << "Cannot open the video file" << endl;
            return -1;
        }
        double dWidth = cap->get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
        double dHeight = cap->get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the  video
        cout << "Frame size : " << dWidth << " x " << dHeight << endl;
        if (dWidth != frameWidth)
        {
            cap->set(CV_CAP_PROP_FRAME_WIDTH,frameWidth);
        }
        if (dHeight != frameHeight)
        {
            cap->set(CV_CAP_PROP_FRAME_HEIGHT,frameHeight);
        }
       Mat *frame = new Mat;
       int frameCount = 20;
       while (!bSuccess && frameCount)
       {
           cout << "Data for fame missing " << frameCount << endl;
           bSuccess = cap->grab();
           if(bSuccess)
           {
               bSuccess = cap->retrieve(*frame, 0);
               if(bSuccess == FALSE)
               {
                  delete(frame);
                  frame=NULL;
                  frame = new Mat;
               }
           }
           if(bSuccess)
               break;
           frameCount--;
           Sleep(5);
       }
       if (!bSuccess) //if not succes->, break loop
        {
         cout << "Cannot read a frame from video file" << endl;
         frame->release();
         delete(frame);
         return -2;
        }

       if (frame != NULL){
           bSuccess=imwrite(image,*frame);
        frame->release();
        delete(frame);
           frame=NULL;
       }
       if(bSuccess == TRUE)
           return 0;

       return -3;
     }
    catch ( cv::Exception & e )
    {
          const char* err_msg = e.what();
          cout << "exception caught: " << err_msg << endl;
    }
    return 0;
-3;
}


int main()
{
    int camOpenStatus = FALSE;
    int status=0;
    cameraClass *newCamHandle = new cameraClass(status=newCamHandle->setCamhandle(0);
cameraClass(0,320,240);
    int captureCount=1;
    int failCount=0;
    char name[30] = {0};
    Sleep(5*1000);
    while (captureCount != 678)
400)
    {

        if(captureCount % 13 == 0  || camOpenStatus == FALSE)
        {
            newCamHandle->closeCamhandle();
            Sleep(1*1000);
            camOpenStatus=newCamHandle->setCamhandle(0);
            if(camOpenStatus == TRUE)
            {
                cout << "!!!Cam handle success!!!" << endl; // prints !!!Hello World!!!
            newCamHandle->closeCamhandle();
            Sleep(1*1000);
            status=newCamHandle->setCamhandle(0);
            if(status == TRUE)
            {
                cout << "!!!Cam handle success!!!" << endl; // prints !!!Hello World!!!
                camOpenStatus=TRUE;
endl;
                failCount=0;
            }
            else
            {
                cout << "!!!CAM handle failure!!!" << endl; // prints !!!Hello World!!!

endl;
                failCount++;
                if(failCount>5)
                {
                    cout << "!!!Uanable to camera handle open limit exceeds!!!" << endl;
                    break;
                }
                continue;
            }
        }
        if(captureCount % 4 == 0)
            Sleep(10*1000);
Sleep(2*1000);
        sprintf(name,"photo%d.jpg",captureCount);
        status = newCamHandle->captureImage(name,0);
        if(status == 0)
        {
            cout << "!!!CIamge capture success!!!" << endl; // prints !!!Hello World!!!
endl;
        }
        else
        {
            cout << "!!!CIamge capture failure!!!" << endl; // prints !!!Hello World!!!
failure!!! Error val "<<  status << endl;
            failCount++;
            if(failCount>2)
                break;
        }
        Sleep(0.6*1000);
        captureCount++;
    }
    newCamHandle->closeCamhandle();
    delete(newCamHandle);
    Sleep(30*1000);
    return 0;
}

Header file

#ifndef CAMERACLASS_H_
#define CAMERACLASS_H_
//#include "opencv2/highgui/highgui.hpp"
//#include "opencv2/highgui/highgui.hpp"
//using namespace cv;

class cameraClass
{
public:
    //VideoCapture cap;
    int frameWidth;
    int frameHeight;
    int cameraModule;

    cameraClass()
    {
        cameraModule=0;
        frameWidth=320;
        frameHeight=240;
    }
    cameraClass(int cameraModel)
    {
        cameraModule=cameraModel;
        frameWidth=320;
        frameHeight=240;
    }

    cameraClass( int frameWid, int frameHght)
    {

        frameWidth=frameWid;
        frameHeight=frameHght;
    }

    cameraClass(int camModel, int frameWid, int frameHght)
    {
        cameraModule=camModel;
        frameWidth=frameWid;
        frameHeight=frameHght;
    }

    int captureImage(char *imageName, int roatation);
    bool setCamhandle(int camId);
    int closeCamhandle();

};
#endif /* CAMERACLASS_H_ */