issue regarding deleting an object in qt with delete and free
-
I am trying to delete an object that has been created. When i gave delete p it did not work, but when i set p=NULL and then delete p it worked. I don't see why ? however when i give free(p) it also works.
it does not work without p=NULL
stack *p = new stack(); for (int i=0; i<4; i++) { p->push(i); } for (int i=0; i<4; i++) { p->pull(); } p=NULL; delete p;error message deleting an object that does not exist if i don't use p=NULL. If i use free (p) instead of p=NULL and delete it works. But what is the explaination?
but when i set p=NULL and then delete p it worked
It "worked", in the sense that in this case it does not delete anything!
You should never
free()an object created bynew, onlydelete.error message deleting an object that does not exist if i don't use p=NULL
Sounds more like an error in the destructor for
stackreferring to something else (e.g. there's something wrong in your push-me-pull-you code)!As the others say: post whole message, and show what is going on inside
stack? -
This post is deleted!
-
This is the whole error message when i remove the null
linkelist(34449,0x7fffa95e1380) malloc: *** error for object 0x3: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug -
I found the mistake
it was inClassList::~ClassList() { //destructor //delete temp; / delete head; }it was delete temp
-
I found the mistake
it was inClassList::~ClassList() { //destructor //delete temp; / delete head; }it was delete temp
it was delete temp
I was just about to tell you that! Your
tempwasn't even initialized! As you can see, as predicted the error was indeed inside the destructor forstack, which is only called if youdelete stack. -
@SherifOmran said in issue regarding deleting an object in qt with delete and free:
ClassList::ClassList()
{//main head = new Node; head=NULL;Huh? Looks like an instant memory leak.
(btw. you should use
nullptr)Frankly, I don't have time to analyse your whole code, sorry. But perhaps try running it through valgrind and address sanitizer - it should pick all the leaks for you.
-
This is the whole error message when i remove the null
linkelist(34449,0x7fffa95e1380) malloc: *** error for object 0x3: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug@SherifOmran As far as I can see you did not initialise temp and try to delete it! No wonder it does not work.
ClassList::ClassList() { head = new Node; head=NULL; // Why do you assign NULL after creating instance?! } ClassList::~ClassList() { delete temp; //Check whether temp points to anything before deleting delete head; }Change
private: Node* head = nullptr; //number 1 Node* current = nullptr; //current node Node* temp = nullptr; Node* temp2 = nullptr; -
what caused confusion is that i delete L at line 51 and it was accepted
-
@jsulm
because i want to check that the head it Null at the initialization -
what caused confusion is that i delete L at line 51 and it was accepted
delete L; // L became a null pointerThe comment is wrong: the value of the pointer does not change when you call delete on it.
delete L; L = nullptr; // Now it is null pointer -
yes thank you i corrected this comment
-
@jsulm
because i want to check that the head it Null at the initialization@SherifOmran said in issue regarding deleting an object in qt with delete and free:
@jsulm
because i want to check that the head it Null at the initializationAs @sierdzio already told you: you created a memory leak this way.
Just removehead = new Node;Else you create an instance and loose the pointer to it, so you never delete this instance -> memory leak.
-
yes i see.
how i delete temp that was initialized in ClassList::InsertNode ? -
yes i see.
how i delete temp that was initialized in ClassList::InsertNode ?@SherifOmran said in issue regarding deleting an object in qt with delete and free:
how i delete temp that was initialized in ClassList::InsertNode ?
delete temp; -
yes i see.
how i delete temp that was initialized in ClassList::InsertNode ?how i delete temp that was initialized in ClassList::InsertNode ?
As @jsulm says it's still
delete temp, but make sure you initializetemp = nullptrin either the declaration or the constructor,ClassList::InsertNodemight never get called.... -
thank you guys for suggested corrections