How to identify where the memory leak is? - c++

I'm trying to rid my code of memory leaks but can't quite narrow down exactly where the leak is coming from.
void insertHead(T value){
if(!duplicateCheck(value)){
length++;
if(head == NULL){
head = new Node(value);
}else{
Node *temp = head;
head = new Node(value);
head->next = temp;
}
}
}
void insertTail(T value){
if(!duplicateCheck(value)){
// create the node
if(head == NULL){// insert at the insertHead
insertHead(value); //head = newNode;
}else{// Iterators through the linked list until a null is found
length++;
Node *fakeIterator = head; // once found sets the null val to newNode
while(fakeIterator->next != NULL){
fakeIterator = fakeIterator->next;
}
fakeIterator->next = new Node(value);
}
}
}
void insertAfter(T value, T insertionNode){ // seg faulting because it cant find the object oadd after
if(!duplicateCheck(value)){
Node *fakeIterator = head;
while (fakeIterator != NULL) {
if (fakeIterator->value == insertionNode) {
Node *newNode = new Node(value);
newNode->next = fakeIterator->next;
fakeIterator->next = newNode;
length++;
break;
}
fakeIterator = fakeIterator->next;
}
}
}
void clear(){
Node *fakeIterator = head;
while(fakeIterator!=NULL){
delete head;
fakeIterator = fakeIterator->next;
head = fakeIterator;
}
head = NULL;
length = 0;
}
The Clear function is called in the deconstructor and main's purpose to delete each node. I used valgrind and it said there is memory lost in all of the insert methods but again im just not sure where. I can post the valgrind output if need be

I'm assuming you've initialized the next field to null in Node constructor.
In your insertTail function, your loop condition involves pointer access, which is illegal if the pointer is null. Also, as #tadman mentioned, you're accessing a deleted element. (Note that while accessing, your head and fakeptr point to the same element. They must point to consecutive elements if you want to delete them)
Generally, compiling with the -g flag (in case of g++ or clang++) and then running valgrind would output the exact line numbers where your memory leak is taking place.
You could also use both gdb and valgrind together. Please refer this link for more info.

Related

Read access violation while deleting multiple nodes from a linked list

I need to implement a doubly linked list in c++ for a small animation running on console. The linkedlist stores clouds and then they move through the console and as each cloud hits the end of screen, it needs to be deleted from linked list. As the cloud hits the end, it has a variable called alive which is set to false so it can be deleted.
I can't upload the full game code, but I have recreated the problem in dummy code by creating sample clouds where some of them have alive = true and alive = false. I have also updated the previous and next nodes of the cloud to be deleted but I still get an error:
Exception thrown: read access violation. temp was 0xFFFFFFFFFFFFFFFF.
Code below (include statements removed for simplicity)
Test.cpp
int main() {
Cloud* a = new Cloud('a');
a->alive = false;
Node* a1 = new Node(a);
Cloud* b = new Cloud('b');
b->alive = false;
Node* b1 = new Node(b);
LinkedList list;
list.Insert(a);
list.Insert(b);
Node* temp = list.head;
while (temp != nullptr) {
if (temp->data->alive == false) list.Delete(temp); // throws exception after deleting a single node.
temp = temp->next;
}
return 0;
}
LinkedList.cpp delete function
void LinkedList::Delete(Node* del) {
if (del == head) {
OutputDebugStringA("Cloud in head");
Node* temp = head;
head = head->next;
head->prev = nullptr;
delete temp;
return;
}
else {
Node* temp = head;
while (temp != tail->next) {
if (temp == del) {
if (temp->next != nullptr) {
OutputDebugStringA("Cloud in mid");
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
break;
}
else {
OutputDebugStringA("cloud at tail");
tail = temp->prev;
tail->next = nullptr;
break;
}
}
temp = temp->next;
}
delete temp;
temp = nullptr;
}
}
Node.cpp
#include "Node.h"
#include <iostream>
using namespace std;
Node::Node() {
this->data = nullptr;
}
Node::Node(Cloud* data) {
this->data = data;
}
Someone please point out where am I going wrong. Thanks
if (temp->data->alive == false) list.Delete(temp); // throws exception after deleting a single node.
temp = temp->next;
Here, temp gets passed into the Delete() method. Afterwards temp gets set to temp->next.
In Delete():
delete temp;
temp = nullptr;
The object referenced by the passed-in temp pointer (here, this temp, by the virtue of the preceding logic, is the same pointer that gets passed in) gets deleted.
After returning, temp->next references a deleted object.
This is at least one confirmed instance of undefined behavior in the shown code. This may or may not be the only bug.
As it's been pointed out to you in comments, this overall Delete() logic is fundamentally flawed. It should not involve any kind of iteration, for a doubly-linked list. You will end up fixing this bug while rewriting Delete() from scratch (which includes rethinking how Delete() itself gets called, because after it returns temp is no longer usable for anything).
As #John Zwinck and #Sam Varshavchik pointed out that the implementation of delete method was flawed and temp became useless after the Delete function returned.
I fixed it by using another temp pointer and fixing the delete method to be O(1).
Delete Method
void LinkedList::Delete(Node* del) {
if (del == head) {
head = head->next;
head->prev = nullptr;
}
else if (del == tail) {
tail = del->prev;
tail->next = nullptr;
}
else {
del->prev->next = del->next;
del->next->prev = del->prev;
}
delete del;
}
Node deletion
Node* temp = cloud_list.head;
Node* next;
while (temp != nullptr) {
next = temp->next;
if (temp->data->alive == false) {
cloud_list.Delete(temp);
}
temp = next;
}
The deletion now works fine.

Circular doubly linked list memory deallocation

I am having serious trouble cleaning up the memory of my circular linked list. I think I understand the issue though. My head pointer has, say, N allocations and my build pointer has N-1 allocations. The addback function can only have an integer and I can't use containers or vectors.
void Queue::addBack(int passed_Val)
{
if (head == nullptr) //takes care of first node when list is empty
{
head = new QueueNode;//head is my building now
cout<<"head is "<<sizeof(head)<<" bytes"<<endl;
head->val = passed_Val;
head->next = head;//this make an infinite loop
head->prev = head;
cout<<"node[0] mem allocated "<<head<<endl;
}
else
{
//build next will always have N-1 address, where N = number of addresses
//head points to. I don't want build to point to head when N=1
// have tempPtr point to old address
QueueNode* tempPrev = head->prev;//this will always point to the last node added!
QueueNode* build = new QueueNode;
build->next=head;
cout<<"build is "<<sizeof(build)<<" bytes"<<endl;
cout<<"node[1] mem allocated "<< build <<endl;
cout<<"node[1] "<< head <<endl;
build->val=passed_Val;
build->next = head;
head->prev = build;
tempPrev->next = build;
build->prev = tempPrev;
}
and my destructor looks like this
Queue::~Queue()
{
if(head !=nullptr)//needed because i don't want to deallocate head if it was never called
{
QueueNode* deletePtr = head->next;
cout<<"deallocating node[0] "<<head<<endl;
delete head;
while(deletePtr !=head)
{
delete deletePtr;
cout<<"deallocating pointer "<<deletePtr<<endl;
deletePtr =deletePtr->next;
}
}
}
I've thought about having the build pointer equal to the head pointer in the (head == nullptr) conditional but I am running into even more problems. Here is the result of my valgrind
I am thinking this line is my primary problem
==30923== Address 0x5a225e0 is 0 bytes inside a block of size 24 free'd
So if I understand correctly, I am trying to deallocate memory that has already been freed? How can I fix this in my destructor? I've tried messing around with my addback function but I end up losing nodes or more memory leaks :(
delete deletePtr;
This destroys whatever object deletePtr was referencing. Immediately afterwards:
deletePtr =deletePtr->next;
This attempts to reference deletePtr->next. As we've just determined, deletePtr's object has been destroyed, and dereferencing it becomes undefined behavior.
This may or may not be the only problem with the shown code. It's not possible to conclusively determine this without a minimum reproducible example, there may be other problems; but this is definitely one of them.
To add to the answer given by #SamVarshavchick, the code is flawed here:
delete head;
while(deletePtr != head) // <-- You are comparing deletePtr to a deleted pointer?
Given all that, the Queue destructor seems to bes more complex than it should be. The following is what I would have expected:
Queue::~Queue()
{
QueueNode* deletePtr = head;
QueueNode* nextPtr = nullptr;
while ( deletePtr )
{
nextPtr = deletePtr->next;
delete deletePtr;
deletePtr = nextPtr;
}
}
Note that there is no need to explicitly check for head being null at the beginning of the function, as the while loop will only be entered on a non-null value.
I was the destructor! Here is how I solved it
Queue::~Queue()
{
int i=1;
if(head !=nullptr)//needed because i don't want to deallocate head if it was never called
{
QueueNode* stop = head->prev;
QueueNode* deletePtr = head->next;//tractor node[1]
QueueNode* temp = head;// cutter node[0]
if(deletePtr ==head)// N=1
{
delete deletePtr;
}
else if(deletePtr == stop)//N=2
{
deletePtr = stop;
delete deletePtr;
delete head;
}
else//N >=3
{
while (temp != stop)//last node
{
delete temp; //delete node[0]
cout<<"temp "<<temp<<endl;
cout<<"deletePtr "<<deletePtr<<endl;
temp= deletePtr;// node[1]
deletePtr = deletePtr->next;//node[2]
cout<<"temp "<<temp<<endl;
cout<<"deletePtr "<<deletePtr<<endl;
}
delete stop;
}//end N>=3
}
}

how to search and delete a specific node in a linked list in c++ [duplicate]

I feel as if I am not actually deleting the node and freeing up memory. I think I am just moving pointers around so when I print the linked list the list doesn't print the element I deleted. So my question is am I actually deleting the node or am I just simply rearranging the pointers so it looks like I am deleting the nodes(Essentially just breaking the links but not deleting the node)? Thank you for any help.
void SLL::deleteNode(int target){
Node *current = new Node;
Node *previous = new Node;
for (current = front->next, previous = front; current != NULL; current = current->next, previous=previous->next){
if (previous->data == target && previous == front){
front = previous->next;
delete[] previous;
return;
//This if statement deletes the element if its the front
}
else {
if (previous->data == target && previous->next == NULL){
previous = NULL;
delete[] current;
return;
//This if statement deletes the node if it is the back
}
else if (current->data==target)
{
previous->next = current->next;
delete[] current;
return;
//This if statement deletes a node if it is in the middle
}
}
}
delete[] current;
delete[] previous;
}
Node *current = new Node;
Node *previous = new Node;
This code causes memory leaks - you are never deleting this memory. You can declare pointers without memory allocation:
Node *current = nullptr;
Node *previous = nullptr;
delete will delete the memory of the pointer so you will actually delete Nodes.
But using delete[] for the Node* is incorrect, it should be used only for arrays - the memory allocated with new[]. Improper use leads to undefined behaviour.
So, to properly delete nodes delete them with operator delete.
Use memory leaks detection tools to know are there memory leaks in you program.
The code to delete a list element: say, we have pHead which points to the head of the list
(but it would give you much more if you write such things yourself):
Node* pCur = pHead;
Node* pPrev = pCur;
while (pCur && pCur->data != target) {
pPrev = pCur;
pCur = pCur->next;
}
if (pCur==nullptr) // not found
return NOT_FOUND;
if (pCur == pHead) { // first element matches
pHead = pCur->next;
} else {
pPrev->next = pCur->next;
}
// pCur now is excluded from the list
delete pCur; // deallocate its memory
Alternative Using Pointer To Pointer (Community Addition)
The above can take on new light when you use the actual pointers in the list to perform the enumeration. The following starts with pp being assigned the address of the head pointer (not the node it points to; the actual pointer itself). We walk the list until pp hold the address of a pointer that is pointing to a node with the target to delete (could be the head pointer, could be a next pointer in some node, makes no difference). The pointer being addressed is set to its own node's next value, then the target node is removed.
This really should be watched in a debugger to see how it works, but the algorithm is remarkably simple given what is really going on:
Node **pp = &pHead;
while (*pp && (*pp)->data != target)
pp = &(*pp)->next;
if (*pp)
{
Node *victim = *pp;
*pp = victim->next;
delete victim;
}
Thats all of it. And you get head-node removal without having to special case it for free. Hope this helps as well.

Deletion in a linked list

Qn) Given only a pointer to a node to be deleted in a singly linked list, how do
you delete it?
I am trying to delete the last element i.e., 1 but the else part goes into an infinite
loop printing garbage values.
Original link.
int main()
{
struct Node* head = NULL;
push(&head, 1);
push(&head, 4);
push(&head, 6);
push(&head, 8);
print(head);
del_p(head->next->next->next);
cout << endl;
print(head);
return 0;
}
void del_p(struct Node* current)
{
struct Node* temp;
if (current->next != NULL)
{
temp = current->next;
current->data = temp->data;
current->next = temp->next;
free(temp);
}
else
{
free(current);
current = NULL;
}
}
The else branch of your function tries to reassign current to NULL. This is problematic because current is a local copy of the pointer passed in. That is, you can't modify the value of the original pointer.
This is why you are receiving garbage, because you're accessing a node whose memory has already been deallocated.
You either need a double pointer, or preferably a reference to the node:
void del_p(struct Node*& current)
If you pass in the node to be deleted and the head node then you can loop until you find the node prior to the node to be deleted. You then need to point the prior node to the node that is pointed to by the node to be deleted and then you can free the node you want to delete.
void delete(struct Node* to_delete, struct Node* head)
{
// check if node to be deleted is the head
if (to_delete == head)
{
head = to_delete->next;
return;
}
// make a local copy of the head just in case as to not alter it
struct Node* tempHead = head;
while(tempHead->next != to_delete)
{
tempHead = tempHead->next;
}
tempHead->next = to_delete->next;
free(to_delete);
}
Just as a disclaimer I haven't tested this code, but conceptually it should work.
The typical algorithm for deleting a node on a linked list would follow the next steps:
Get a temp pointer started in Head.
Move your temp to the node you want to delete (in this case one before the last: temp->next == NULL).
Free the memory for temp2.
Set the pointer of temp->next to NULL.
Return the pointer to head.
Now this is not the only algorithm, there are a lot of ways you can accomplish this. The following code would be my solution to the function del_p (if you would want to delete the last node):
void del_p(struct Node *head)
{
if (head != NULL)
{
struct Node *temp = head;
while (temp->next != NULL) temp = temp->next;
free(temp);
}
}
You can make this code a little more general to make it possible to delete any Node, by passing a pointer to that node (or a value), the code would look as follows:
void del_p(struct Node **head, struct Node *delete_node)
{
if (head != NULL)
{
struct Node *temp = *head;
if (temp == delete_node)
{
*head = (*head)->next;
free(temp);
}
else
{
while (temp->next != NULL && temp->next != delete_node)
temp = temp->next;
if (temp->next != NULL && delete_node != NULL)
{
temp->next = delete_node->next;
free(delete_node);
}
}
}
}
Hope this works for you, this code isn't tested, but tell me if you have troubles!

How do I properly delete nodes of linked list in C++

I feel as if I am not actually deleting the node and freeing up memory. I think I am just moving pointers around so when I print the linked list the list doesn't print the element I deleted. So my question is am I actually deleting the node or am I just simply rearranging the pointers so it looks like I am deleting the nodes(Essentially just breaking the links but not deleting the node)? Thank you for any help.
void SLL::deleteNode(int target){
Node *current = new Node;
Node *previous = new Node;
for (current = front->next, previous = front; current != NULL; current = current->next, previous=previous->next){
if (previous->data == target && previous == front){
front = previous->next;
delete[] previous;
return;
//This if statement deletes the element if its the front
}
else {
if (previous->data == target && previous->next == NULL){
previous = NULL;
delete[] current;
return;
//This if statement deletes the node if it is the back
}
else if (current->data==target)
{
previous->next = current->next;
delete[] current;
return;
//This if statement deletes a node if it is in the middle
}
}
}
delete[] current;
delete[] previous;
}
Node *current = new Node;
Node *previous = new Node;
This code causes memory leaks - you are never deleting this memory. You can declare pointers without memory allocation:
Node *current = nullptr;
Node *previous = nullptr;
delete will delete the memory of the pointer so you will actually delete Nodes.
But using delete[] for the Node* is incorrect, it should be used only for arrays - the memory allocated with new[]. Improper use leads to undefined behaviour.
So, to properly delete nodes delete them with operator delete.
Use memory leaks detection tools to know are there memory leaks in you program.
The code to delete a list element: say, we have pHead which points to the head of the list
(but it would give you much more if you write such things yourself):
Node* pCur = pHead;
Node* pPrev = pCur;
while (pCur && pCur->data != target) {
pPrev = pCur;
pCur = pCur->next;
}
if (pCur==nullptr) // not found
return NOT_FOUND;
if (pCur == pHead) { // first element matches
pHead = pCur->next;
} else {
pPrev->next = pCur->next;
}
// pCur now is excluded from the list
delete pCur; // deallocate its memory
Alternative Using Pointer To Pointer (Community Addition)
The above can take on new light when you use the actual pointers in the list to perform the enumeration. The following starts with pp being assigned the address of the head pointer (not the node it points to; the actual pointer itself). We walk the list until pp hold the address of a pointer that is pointing to a node with the target to delete (could be the head pointer, could be a next pointer in some node, makes no difference). The pointer being addressed is set to its own node's next value, then the target node is removed.
This really should be watched in a debugger to see how it works, but the algorithm is remarkably simple given what is really going on:
Node **pp = &pHead;
while (*pp && (*pp)->data != target)
pp = &(*pp)->next;
if (*pp)
{
Node *victim = *pp;
*pp = victim->next;
delete victim;
}
Thats all of it. And you get head-node removal without having to special case it for free. Hope this helps as well.