Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8381397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:41:24+00:00 2026-06-09T16:41:24+00:00

I’m using the function cvHaarDetectObjects to do face detection and there is a memory

  • 0

I’m using the function cvHaarDetectObjects to do face detection and there is a memory leak checking with valgrind even though I think I freed all the memories. I really don’t know how to fix the memory leak. Here is my code:

int Detect(MyImage* Img,MyImage **Face)
{

  Char* Cascade_name = new Char[1024];
  strcpy(Cascade_name,"/usr/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml");

    // Create memory for calculations
    CvMemStorage* Storage = 0;


    // Create a new Haar classifier
    CvHaarClassifierCascade* Cascade = 0;

    int Scale = 1;

    // Create two points to represent the face locations
    CvPoint pt1, pt2;
    int Loop;

    // Load the HaarClassifierCascade
    Cascade = (CvHaarClassifierCascade*)cvLoad( Cascade_name, 0, 0, 0 );

    // Check whether the cascade has loaded successfully. Else report and error and quit
    if( !Cascade )
    {
        fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
        exit(0);
    }

    // Allocate the memory storage
    Storage = cvCreateMemStorage(0);

    // Clear the memory storage which was used before
    cvClearMemStorage( Storage );

    // Find whether the cascade is loaded, to find the faces. If yes, then:
    if( Cascade )
    {
        // There can be more than one face in an image. So create a growable sequence of faces.
        // Detect the objects and store them in the sequence

      CvSeq* Faces = cvHaarDetectObjects( Img->Image(), Cascade, Storage,
                                            1.1, 2, CV_HAAR_DO_CANNY_PRUNING,
                                            cvSize(40, 40) );

        int MaxWidth = 0;
        int MaxHeight = 0;
        if(Faces->total == 0)
        {
           cout<<"There is no face."<<endl;
           return 1;
        }


       //just get the first face 
        for( Loop = 0; Loop <1; Loop++ )
        {
           // Create a new rectangle for drawing the face
            CvRect* Rect = (CvRect*)cvGetSeqElem( Faces, Loop );

            // Find the dimensions of the face,and scale it if necessary
            pt1.x = Rect->x*Scale;
            pt2.x = (Rect->x+Rect->width)*Scale;
            if(Rect->width>MaxWidth) MaxWidth = Rect->width;
            pt1.y = Rect->y*Scale;
            pt2.y = (Rect->y+Rect->height)*Scale;
            if(Rect->height>MaxHeight) MaxHeight = Rect->height;
            cvSetImageROI( Img->Image(), *Rect );

            MyImage* Dest = new MyImage(cvGetSize(Img->Image()),IPL_DEPTH_8U, 1); 

            cvCvtColor( Img->Image(), Dest->Image(), CV_RGB2GRAY );

            MyImage* Equalized = new MyImage(cvGetSize(Dest->Image()), IPL_DEPTH_8U, 1);

            // Perform histogram equalization
            cvEqualizeHist( Dest->Image(), Equalized->Image());
            (*Face) = new MyImage(Equalized->Image());

            if(Equalized)
               delete Equalized;
            Equalized = NULL;

            if(Dest)
               delete Dest;
            Dest = NULL;

            cvResetImageROI(Img->Image());

        }

        if(Cascade)
        {
           cvReleaseHaarClassifierCascade( &Cascade ); 
           delete Cascade;
           Cascade = NULL;
        }


        if(Storage)
        {
           cvClearMemStorage(Storage);

           cvReleaseMemStorage(&Storage);
           delete Storage;
           Storage = NULL;
        }
        if(Cascade_name)
           delete [] Cascade_name;
        Cascade_name = NULL;
    return 0;
}

In the code, MyImage is a wrapper class of IplImage containing IplImage* p as a member. if the constructor takes a IplImage* ppara as parameter, then the member p will create memory using cvCreateImage(cvGetSize(ppara), ppara->depth, ppara->nChannels) and cvCopy(ppara, p). if it takes size,depth and channels as parameter, then only do cvCreateImage. Then the destructor do cvReleaseImage(&p). The function int Detect(MyImage *Img, MyImage **Face) is called like:

    IplImage *Temp = cvLoadImage(ImageName);

    MyImage* Img = new MyImage(Temp);
    if(Temp)
       cvReleaseImage(&Temp);
    Temp = NULL;     
    MyImage * Face = NULL;      
    Detect(Img, &Face);

I released Img and Face in the following code once the operations on them is done. And the memory leak is happened inside the Detect function. I’m using OpenCV 2.3.1 on 64 bit OS fedora 16. The whole program can terminate normally except for the memory leak.

Thanks a lot.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:41:26+00:00Added an answer on June 9, 2026 at 4:41 pm

    I found out why there is a memory leak. The reason is:

    In the MyImage class constructor, I passed in a IplImage* p pointer, and do the following:

    mp = cvCloneImage(p);
    

    where mp is a IplImage* member of the MyImage class. I free the IplImage* pointer that I passed in after creating a new MyImage class object, since cvCloneImage() will create some memories. However I free the member pointer mp in the class destructor when it actually doesn’t new any memory. It just points to memories that created by cvCloneImage(). So the memories created by cvCloneImage() isn’t freed. This is where the memory leak came from.

    Thus I do the following in the constructor given a IplImage* p passed in as parameter:

    mp = cvCreateImage(cvGetSize(p), p->depth, p->nChannels);
    cvCopy(p, mp);
    

    And free the mp pointer in the class destructor will free the memory that it creates.

    After doing this, definitely lost and indirectly lost memories are turned to 0, but there are still possibly lost memories, and valgrind points all the lost record to the cvHaarDetectObjects() function which is from OpenCV. And mostly are caused by some “new threads” issues. Thus I googled this problem and found out valgrind does give possibly lost memories sometimes when new thread is involved. So I monitored the memory usage of the system. The result shows no memory usage building up as the program executed repeatedly.

    That’s what I found.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.