OpenCV blob Tracking: Vectors of Class objects

Hi everyone,

I seem to have a memory leak. I am using OpenCV to track blobs in each frame, create a Body (Class) object for each blob and then track appendages for each Body object.

I am using a vector to store Body objects every frame and then at the beginning of each frame, erase the vector of Body objects.

I seem to have a memory leak (after a couple of minutes I reach 2 GB of Memory used) and I get the this error:

**malloc: *** Deallocation of a pointer not malloced: 0x5492e4; This could be a double free(), or free() called with the middle of an allocated block; Try setting environment variable MallocHelp to see tools to help debug **

Any Help would be appreciated!

Thanks,
Frank


Here is some sample Code
(only relevant code)

Body.h

  
  
public:  
  
Body(int _index);  
~Body();  
void setPoints(vector<ofxPoint2f>& _pts);  
void track(float _ratio);  
  
private:  
vector <ofxPoint2f> silhouette;  
ofxPoint2f leftHand;  
ofxPoint2f rightHand;  
ofxPoint2f leftFoot;  
ofxPoint2f rightFoot;  
  

Body.cpp

  
  
void Body::setPoints(vector<ofxPoint2f>& _pts) {  
	int num = _pts.size();  
	silhouette.clear();  
	silhouette.assign(num, ofxPoint2f() );  
	for(int i = 0; i < num; i++) {  
		silhouette[i] = _pts[i];  
	}  
}  
  
  

testApp.h

  
  
vector<Body*> bodies;  
  

testApp.cpp (OpenCV section)
in update() function… Camera Tracking code

  
  
  
// THIS IS WHERE THE NUMBER OF BODIES (BLOBS) IS FOUND  
// CREATE BODY OBJECTS AND TRACK HANDS AND FEET  
	  
		// 1. Clear Body Vector  
		for (vector<Body*>::iterator bi = bodies.begin(); bi != bodies.end(); bi++) {  
			//Body* b = *bi;  
			bodies.erase(bi);  
			delete *bi;  
		}  
		  
		// Erase Elements in bodies Vector  
		//bodies.erase(bodies.begin(), bodies.end() );  
		bodies.clear();  
		  
		if(numBlobs > 0) {	  
		  
			// 2. For Every Blob found, create a Body object and find silhouette  
			for(int i = 0; i < numBlobs; i++) {  
  
				Body* body = new Body(i);  
				body->setCentroid(contour.blobs[i].centroid);  
				body->setBounds(contour.blobs[i].boundingRect);  
				bodies.push_back(body);  
				  
				//lets get out the contour data  
				int length_of_contour = contour.blobs[i].pts.size();  
  
				//clear the old contours  
				contourReg.clear();  
				contourReg.assign(length_of_contour, ofxPoint2f());  
				contourSmooth.clear();  
				contourSmooth.assign(length_of_contour, ofxPoint2f());  
				  
				//lets make a copy for ourselves  
				for(int j = 0; j < length_of_contour; j++){  
					contourReg[j] = contour.blobs[i].pts[j];  
				}  
				  
				contourSimp.smooth(contourReg, contourSmooth, smoothPct);  
				  
				contourSimple.clear();  
				contourSimp.simplify(contourSmooth, contourSimple, tolerance);  
				contourSimp.convexHull(contourSimple, contourHull);  
				  
				body->setPoints(contourSimple);  
				  
				// TRACK HANDS AND FEET FOR EACH BODY  
				body->track(bodyRatio);  
				  
			}  
		}  
  
  

Not sure if you solved this, but I think it would be better to delete the vector contents before you erase the iterator, otherwise deleting the contents of an invalid, erased, iterator could give you wrong results.

  
  
    delete *bi;  
    bodies.erase(bi);