Ask Your Question
0

Possible memory leak cause - Videocapture ??

asked 2015-08-06 22:30:27 -0600

ragav gravatar image

updated 2015-08-07 04:59:39 -0600

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

        if(cap != NULL)
        {
            if (cap->isOpened() )  // if not success, exit program
            {
                cap->release();
            }
            delete(cap);
            cap = NULL;
        }
        return 0;

}

bool cameraClass::setCamhandle(int camId)
{
     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;
        }
    }
    catch ( cv::Exception & e )
    {
          const char* err_msg = e.what();
          cout << "exception caught: " << err_msg << endl;
          if(cap != NULL){
              delete(cap);
              cap=NULL;
          }
    }
    return 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 -3;
}


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

        if(captureCount % 13 == 0  || camOpenStatus == FALSE)
        {
            newCamHandle->closeCamhandle();
            Sleep(1*1000);
            camOpenStatus=newCamHandle->setCamhandle(0);
            if(camOpenStatus == TRUE)
            {
                cout << "!!!Cam handle success!!!" << endl;
                failCount=0;
            }
            else
            {
                cout << "!!!CAM handle failure!!!" << endl;
                failCount++;
                if(failCount>5)
                {
                    cout << "!!!Uanable to camera handle open limit exceeds!!!" << endl;
                    break;
                }
                continue;
            }
        }
        if(captureCount % 4 == 0)
            Sleep(2*1000);
        sprintf(name,"photo%d.jpg",captureCount);
        status = newCamHandle->captureImage(name,0);
        if ...
(more)
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-09-22 03:00:31 -0600

If this is in Windows, then try to disable the MSMF, it seems there is a bug in the cap_msmf.cpp

edit flag offensive delete link more

Comments

mingw can't compile MSMF, so this might not be the issue here.

(also, look at the date, 2.4.11 could not have any support for it in 2015)

berak gravatar imageberak ( 2018-09-22 03:08:16 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-08-06 22:03:42 -0600

Seen: 758 times

Last updated: Aug 07 '15