isspek / cvblob

Automatically exported from code.google.com/p/cvblob
0 stars 0 forks source link

CvUpdateTracks and cvRenderTracks memory leaks #7

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hello. First of all I want to thank you for sharing your code. It is very
useful and works very well.

However I found some memory leaks using CvUpdateTracks and cvRenderTracks
functions. I find them out when running Valgrind memory checker.

* What steps will reproduce the problem?
Use this code in a loop and memory will start growing after some time (must
be more than 10 minutes to notice that increase):

CvSize imSize = cvSize(640, 480);
IplImage* binaryImage =  cvCreateImage(imSize, IPL_DEPTH_8U, 1);
IplImage* labelImg = cvCreateImage(imSize, IPL_DEPTH_LABEL, 1);
.
.
.
while(1)
{
//Read from a camera or video source and save binarized image in
binaryImage variable
.
.
.

  CvBlobs blobs;
  CvTracks tracks;

  cvLabel(binaryImage, labelImg, blobs);

  cvUpdateTracks(blobs, tracks, 5., 10); //Do the tracking
  cvRenderTracks(tracks, binaryImage, binaryImage, CV_TRACK_RENDER_ID
        | CV_TRACK_RENDER_TO_LOG);

  cvReleaseBlobs(blobs);
}

* What version of the product are you using? On what operating system?
I'm using Ubuntu 8.04 64 bits, developing under Eclipse Galileo. What I use
to check the memory leaks is Valgrind (http://valgrind.org/)

* Please provide any additional information below.
Valgrind output message shows something like this after every execution:

==1314== 2,576 bytes in 46 blocks are definitely lost in loss record 84 of 139
==1314==    at 0x4C23809: operator new(unsigned long) (vg_replace_malloc.c:230)
==1314==    by 0x5DA8D5: cvRenderTracks (in
/home/jorge/Eclipse_Projects/VIVAC_Server/Debug/VIVAC_Server)
==1314==    by 0x42014B: CaballeroDetector(_IplImage*, MotionDetectionOut*)
(Caballero.cpp:603)
==1314==    by 0x4095C0: motionDetection(MotionDetParam const*, ImageOut
const*, MotionDetectionOut*) (KernelFunctions.cpp:218)
==1314==    by 0x40E293: main (VIVAC_Server.cpp:412)

==1314== 18,048 bytes in 376 blocks are definitely lost in loss record 87
of 139
==1314==    at 0x4C23809: operator new(unsigned long) (vg_replace_malloc.c:230)
==1314==    by 0x5DB624: cvUpdateTracks (in
/home/jorge/Eclipse_Projects/VIVAC_Server/Debug/VIVAC_Server)
==1314==    by 0x4200F7: CaballeroDetector(_IplImage*, MotionDetectionOut*)
(Caballero.cpp:600)
==1314==    by 0x4095C0: motionDetection(MotionDetParam const*, ImageOut
const*, MotionDetectionOut*) (KernelFunctions.cpp:218)
==1314==    by 0x40E293: main (VIVAC_Server.cpp:412)

Thank you very much.

Tarki.

Original issue reported on code.google.com by djta...@gmail.com on 26 Nov 2009 at 9:34

GoogleCodeExporter commented 8 years ago
You're welcome!

I will try to reproduce the problem as soon as possible. I passed Valgrind when 
I
coded that part with no issues, but I certainly I don't test it enough.

Thank you for post it.

Original comment by grendel....@gmail.com on 26 Nov 2009 at 6:39

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
I tried again yesterday with a brand-new test code ;). I post it here in case 
you
need it:

#include <iostream>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <cvblob.h>

using namespace std;

int
main()
{

  uchar nFrame = 0;
  CvSize imSize = cvSize(640, 480); //Change to your resolution camera
  IplImage* binaryImage = cvCreateImage(imSize, IPL_DEPTH_8U, 1);
  IplImage* labelImg = cvCreateImage(imSize, IPL_DEPTH_LABEL, 1);
  IplImage* image;

  CvCapture* camera = cvCaptureFromAVI(0);

  cvNamedWindow("IP Camera", 1);

  while (1)
    {
      image = cvQueryFrame(camera); //Gets a frame from the source

      //IP source is upside down, so flip the image in order to see it correctly
      cvFlip(image, image, -1);

      cvCvtColor(image, binaryImage, CV_RGB2GRAY);

      cvThreshold(binaryImage, binaryImage, 120, 255, CV_THRESH_BINARY);
      CvBlobs blobs;
      CvTracks tracks;

      cvLabel(binaryImage, labelImg, blobs);

      cvUpdateTracks(blobs, tracks, 5., 10); //Do the tracking
      cvRenderTracks(tracks, image, image, CV_TRACK_RENDER_ID
          | CV_TRACK_RENDER_TO_LOG);

      cvShowImage("IP Camera", image);
      cvReleaseBlobs(blobs);

      if (nFrame == 127)
        break;
      nFrame++;
    }

  cvReleaseCapture(&camera);
  cvReleaseImage(&binaryImage);
  cvReleaseImage(&labelImg);
  return 0;
}

Same memory leaks have been reproduced using this new code.

==9087== 7,168 bytes in 128 blocks are definitely lost in loss record 100 of 111
==9087==    at 0x4C23809: operator new(unsigned long) (vg_replace_malloc.c:230)
==9087==    by 0x409995: cvRenderTracks (in
/home/jorge/Eclipse_Projects/TestTracks/Debug/TestTracks)
==9087==    by 0x401FCD: main (TestTracks.cpp:49)
==9087== 
==9087== 
==9087== 398,592 bytes in 8,304 blocks are definitely lost in loss record 110 
of 111
==9087==    at 0x4C23809: operator new(unsigned long) (vg_replace_malloc.c:230)
==9087==    by 0x40A6E4: cvUpdateTracks (in
/home/jorge/Eclipse_Projects/TestTracks/Debug/TestTracks)
==9087==    by 0x401F9B: main (TestTracks.cpp:47)
==9087== 

See you ;)

Original comment by djta...@gmail.com on 27 Nov 2009 at 7:45

GoogleCodeExporter commented 8 years ago
Thanks!

I've just fixed one of the memory leaks. The one in cvRenderTracks (I don't 
know how
I coded that :-S ). I have commited this changes.

Now I'm working in the cvUpdateTracks code. This is more difficult.

Bye!

Original comment by grendel....@gmail.com on 27 Nov 2009 at 9:33

GoogleCodeExporter commented 8 years ago
Cool Grendel.

Good luck with the other one ;)

Thanks

Original comment by djta...@gmail.com on 27 Nov 2009 at 9:38

GoogleCodeExporter commented 8 years ago
Hi again!

I believe I just have fixed the memory leak. Can you check it with your code 
and the
cvBlob svn version?

When I have time I'll build a package.

Thank you for your patience!

Original comment by grendel....@gmail.com on 11 Dec 2009 at 1:08

GoogleCodeExporter commented 8 years ago
Hi Grendel.

I have just downloaded from the SVN and compiled it.

I'm afraid the problem is still there (I checked it for three times with two
different codes).

Here is the error:

==13900== 448,896 bytes in 9,352 blocks are definitely lost in loss record 110 
of 111
==13900==    at 0x4C23809: operator new(unsigned long) (vg_replace_malloc.c:230)
==13900==    by 0x40ABF1: cvUpdateTracks (in
/home/jorge/Eclipse_Projects/TestTracks/Debug/TestTracks)
==13900==    by 0x401F9B: main (TestTracks.cpp:47)
.
.
Keep on trying please ;) 

Thanks for your big effort. 

Tarki.

Original comment by djta...@gmail.com on 11 Dec 2009 at 1:44

GoogleCodeExporter commented 8 years ago
Hello Tarki,

The code of cvBlob is fine (as much as I know :D ). I have used the code you 
posted
in comment 3 and I have seen that you do a pair of things wrong:

First, you must declare "CvTracks tracks" outside the loop, because the tracking
information must be updating from iteration to iteration. Unlike the blob
information, which is new in each frame. Let's say that "tracks" save 
information of
the correspondence of blobs through frames.

Second, at the end (outside the loop) you must release the last tracks that 
remain in
memory with the function "cvReleaseTracks".

You can see it in the "test_tracking.cpp" code. Briefly:

-------------------------------------------------------------------------
int main()
{
  ...
  CvTracks tracks;
  ...
  while (1)
  {
    ...
    CvBlobs blobs;
    unsigned int result = cvLabel(chB, labelImg, blobs);
    ...
    cvUpdateTracks(blobs, tracks, 5., 10);
    cvRenderBlobs(labelImg, blobs, frame, frame,
CV_BLOB_RENDER_CENTROID|CV_BLOB_RENDER_BOUNDING_BOX);
    cvRenderTracks(tracks, frame, frame, CV_TRACK_RENDER_ID);
    ...
    cvReleaseBlobs(blobs);
    ...
  }
  ...
  cvReleaseTracks(tracks);
  ...
}
-------------------------------------------------------------------------

Tell me if this works!

Cristóbal

Original comment by grendel....@gmail.com on 12 Dec 2009 at 7:59

GoogleCodeExporter commented 8 years ago
Hi again Cristóbal.

It was definitely my fault. As you said I used cvTracks incorrectly: CvTracks
declaration wasn't declared outside the loop (in any of the programs I tested)  
and I
did not used cvReleaseTracks() because I did not know that existed inside your 
library!

Now I have tried again following your instructions and there are no memory 
leaks any
more.

Thanks a lot Cristóbal. Let me know if you need anything ;)

Original comment by djta...@gmail.com on 14 Dec 2009 at 8:05

GoogleCodeExporter commented 8 years ago
Hello Tarki,

I'm glad to hear it's working now. It was not all your fault, there was bugs in 
my
library at the beginning, thanks for post it. The cvReleaseTracks is very new, 
I need
to improve the documentation :-S

Thanks for support!

PS: I'm curious to know for what kind of application are you using cvBlob :D

Original comment by grendel....@gmail.com on 14 Dec 2009 at 11:33

GoogleCodeExporter commented 8 years ago
Thanks to you ;)

I use your library together with a motion detector that I am optimizing in 
order to
achieve real time.

Regards.

Original comment by djta...@gmail.com on 14 Dec 2009 at 11:37