Help! release the memory after using malloc?

Hello everyone

would it be makes sense if I’m not release the memory after using malloc/new) in mouseDragged function?

void ofApp::mouseDragged( int x, int y, int button){

myClass* ob = new myClass(ofRandom(0.5,3),ofRandom(0.5,3), ofRandom(ofGetWidth()), ofRandom(ofGetHeight()));


delete ob;// it cause shut program down


If I delete the “ob” then the program return the message

malloc: Heap corruption detected, free list is damaged at0x…

so I decide not to delete it. would it be great if you give me nice tip or guide line in this case.
thank you!

Hi I guess that objects is a vector< myClass*> or some kind of container.
as such, if you delete the pointer after you push it into objects you will also delete it from objects, and because of it you are getting this error.
So, you should not delete it there.
I would recommend you to avoid pointer as much as possible, specially if you are creating these.
If you can not manage the myClass instances as simple instantiations and not as pointers use an std::shared_ptr instead of the pointer. Your code should look like this.

void  ofApp::mouseDragged( int x, int y, int button){
auto ob = make_shared<myClass>(ofRandom(0.5,3),ofRandom(0.5,3), ofRandom(ofGetWidth()),ofRandom(ofGetHeight()));

objects must be declared as vector<shared_ptr<myClass>> objects; (where I assume it is a vector)

Hope this helps.


thanks you for your nice explain and I learned a lot from you.
and your are correct object is a container for myClass.
of course I would love to avoid pointer as much as I can and I never use smart pointer before.

after change to std::vector<shared_ptr<myClass>> objects;

another issue is comes up at draw function.

it returns me the message

Non-const lvalue reference to type ‘vector<myClass *>’ cannot bind to a value of unrelated type ‘vector<shared_ptr<myClass>>’

here is draw function ofApp.cpp

void ofApp::draw(){

std::vector<shared_ptr<myClass>>::iterator it = objects.begin();

for (it = objects.begin(); it != objects.end(); ) {

ofVec2f leftForce;

ofVec2f rightForce;

ofVec2f crossForce;


leftForce = (*it)->left_force(objects);

rightForce = (*it)->right_force(objects);



I think I can not put objects to the función left_force(objects); :frowning:
since I define std::vector<shared_ptr<myClass>> objects;
the function defined originally as ofVec2f left_force(vector<myClass*>&objects);

well then you need to change those functions to be left_force(vector<shared_ptr<myClass>>& objects) and right_force(vector<shared_ptr<myClass>>& objects) also you can completely avoid iterators.

void ofApp::draw(){
for (auto& obj: objects) {

ofVec2f leftForce;

ofVec2f rightForce;

ofVec2f crossForce;


leftForce = obj->left_force(objects);

rightForce = obj->right_force(objects);


1 Like

Now The code it self has no error but some where in memory part causing error…

:Static_assert failed due to requirement 'is_constructible…

since I am not a programmer, just enjoying programming with OF.This stuffs is too much for me.I better leave this issue. otherwise I don’t enjoy it coding with OF. Maybe I shouldn’t step into pointer area :slight_smile:
I will ask another time with another question.

Thanks a lot being gentle and spend your time, to share useful tips for my question!!


don’t be discouraged just hang in there and eventually you will start understanding things
it’s not rocket science.

I mean the most simple way to do this , go back to your original code

and just don’t delete ob; because as roy pointed out you still need it.

delete everything only once you are done when your app quits inside the exit() function in a for loop…

for (int i=0; i<objects.size(); i++)
delete objects[i];

also, to quote Bjarne Stroustrup…
if you are using pointers inside a scope you messed up.