Solved issue regarding deleting an object in qt with delete and free
-
it was delete temp
I was just about to tell you that! Your
temp
wasn'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.
-
@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 -
delete L; // L became a null pointer
The 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
-
@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 ? -
@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;
-
how i delete temp that was initialized in ClassList::InsertNode ?
As @jsulm says it's still
delete temp
, but make sure you initializetemp = nullptr
in either the declaration or the constructor,ClassList::InsertNode
might never get called.... -
thank you guys for suggested corrections