orizis
orizis

Reputation: 166

Memory leaking using OpenCV CascadeClassifier

I'm writing a program that uses PCL kinfu and I'm using openCV face detection. I had some heap problems so I isolated the openCV's code to try and check if the problem is over there. After commenting out almost everything, I came across something strange. There is only a global declaration of the 'CascadeClassifier' class, and it causes valgrind to alert on possibly lost and still reachable blocks. Commenting out this declaration works fine. I'm really not sure what's going on and would appreciate any help. I'm attaching my problematic code (without the commented out parts). Thanks!

#include "opencv2/objdetect/objdetect.hpp"

using namespace cv;
CascadeClassifier c;

int main() {
    return 0;
}

**Valgrind output:**
==3316== Memcheck, a memory error detector
==3316== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==3316== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==3316== Command: ./test
==3316== 
==3316== 
==3316== HEAP SUMMARY:
==3316==     in use at exit: 297,370 bytes in 1,393 blocks
==3316==   total heap usage: 3,446 allocs, 2,053 frees, 655,130 bytes allocated
==3316== 
==3316== LEAK SUMMARY:
==3316==    definitely lost: 0 bytes in 0 blocks
==3316==    indirectly lost: 0 bytes in 0 blocks
==3316==      possibly lost: 4,676 bytes in 83 blocks
==3316==    still reachable: 292,694 bytes in 1,310 blocks
==3316==         suppressed: 0 bytes in 0 blocks
==3316== Rerun with --leak-check=full to see details of leaked memory
==3316== 
==3316== For counts of detected and suppressed errors, rerun with: -v
==3316== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Upvotes: 0

Views: 629

Answers (2)

berak
berak

Reputation: 39796

try again like this:

#include "opencv2/objdetect/objdetect.hpp"

using namespace cv;

void testmem() {
    CascadeClassifier c;
}

int main() {
    for (int i=0; i<10; i++)
        testmem();
    return 0;
}

Upvotes: 1

marol
marol

Reputation: 4074

For me there're two possible explanations:

  1. If you trust Valgrind - might be some leak in opencv code and therefore somebody should investigate it more in details from opencv devs (why don't you create an appropriate ticket with that issue?)

or

  1. There is not any leak cause you clearly have 'possibly' word there. And then you shouln't care much. What kind of heap issue did you have on the beginning before you commented out some code?

BTW using classes that are not POD (Plain Old Data) in the global scope (static memory) is not recommended. Do you have the same problem when you move CascadeClassifier object to the local scope (main())?

Upvotes: 1

Related Questions