hi
i was having lots of memory leaks due to not being able to delete allocated memory. i worked around and there are no more leaks, – checked with xcode’s MallocDebug, just the standard 64 bytes – but was wondering why this is happening, and if there are proper ways to do this.
my program generates a lot of ‘explosions’ which allocate random numverts and are present for some time and then i free them. the explosions go inside a std::vector, when the explosions life is zero i remove the explosions from the vector.
the problem i faced was not being able to delete the allocated pointer in the destructor. which is really weird… the program crashes and points to the destructor line if i try to deallocate the pointer in the destructor, with crashes like: openFrameworksDebug(5565,0xa06d0720) malloc: *** error for object 0x87d800: double free
*** set a breakpoint in malloc_error_break to debug
it doesnt crash and runs with no leaks it i setup a func to deallocate the memory, leaving an empty destructor.
is this normal vector behaviour? if the parts are not in a vector, calling the destructor seems safe, but if i try to destroy the part from the vector, this kinds of crashes come in…
what should be proper code to handle this kinds of situations?
here is some simplified example code that shows this problem
testApp.h
#ifndef _TEST_APP
#define _TEST_APP
#include "ofMain.h"
#include "ofxVec3f.h"
class Explosion{
public:
ofxVec3f *verts;
int numverts;
int life;
Explosion(){
life = 255;
numverts = ofRandom(10,100);
verts = new ofxVec3f[numverts];
for(int i=0;i<numverts;i++){
float a = 100;
verts[i].set(ofRandomuf()*a,ofRandomuf()*a,ofRandomuf()*a);
}
}
~Explosion(){
//if(verts!=NULL) delete[] verts; // crashes
}
void clear(){
if(verts!=NULL) delete[] verts; // doesnt crash and doesnt leak
}
void update(){
life--;
}
void render(){
glColor4f(1, 1, 1, life/255.f);
for(int i=0;i<numverts-1;i++){
ofLine(verts[i].x, verts[i].y, verts[i+1].x, verts[i+1].y) ;
}
}
};
class ExplosionManager{
public:
vector <Explosion> myparts;
ExplosionManager(){;}
~ExplosionManager(){myparts.clear();}
void update(){
for(int i=0; i<myparts.size(); i++){
myparts[i].update();
if(myparts[i].life<0){
// myparts[i].~Explosion();//also crashes
myparts[i].clear(); //deletes allocated memory and runs smooth
myparts.erase(myparts.begin()+i,myparts.begin()+i+1);
}
}
}
void add(){
Explosion p;
myparts.push_back(p);
}
void render(){
for(int i=0; i< myparts.size(); i++){
myparts[i].render();
}
}
};
class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed (int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
Explosion one;
ExplosionManager manager;
};
#endif
testApp.cpp
#include "testApp.h"
//--------------------------------------------------------------
void testApp::setup(){
ofEnableAlphaBlending();
}
//--------------------------------------------------------------
void testApp::update(){
manager.update();
}
//--------------------------------------------------------------
void testApp::draw(){
one.render();
manager.render();
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
if(key=='a')
manager.add();
}
//--------------------------------------------------------------
void testApp::keyReleased(int key){
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
manager.add();
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::windowResized(int w, int h){
}
thank you!
a