What type of things can I try to do with my existing code to fully return back memory to the operating system? Right as it stands there are 8 bytes of memory still in the heap. In addition, I am getting these errors when I run my code through Valgrind:
#ifndef LINKEDLIST
#define LINKEDLIST
class CLinkedlist {
private:
struct node {
node() : mData(0), mNext(nullptr) {}
int mData;
node* mNext;
};
typedef node *nodePtr;
nodePtr mHead;
nodePtr mCurr;
nodePtr mTemp;
public:
CLinkedlist() : mHead(nullptr), mCurr(nullptr), mTemp(nullptr) {}
~CLinkedlist();
void AddNode( int);
void DeleteNode( int );
void Printlist();
};
#endif
CLinkedlist::~CLinkedlist(){
mCurr = mHead;
mTemp = mHead;
while (mCurr)
{
mTemp = mCurr;
mCurr = mCurr->mNext;
cout << mTemp->mData << " was destroyed." <<endl;
delete mTemp;
}
mHead = nullptr;
}
void CLinkedlist::AddNode( int datanode){
nodePtr data = new node;
data->mData = datanode;
if (mHead)
{
mCurr = mHead;
while(mCurr->mNext != nullptr) {
mCurr = mCurr->mNext;
}
mCurr->mNext = data;
}
else
{
mHead = data;
}
}
void CLinkedlist:: DeleteNode(int deldata){
nodePtr delptr = nullptr;
mTemp = mHead;
mCurr = mHead;
while (mCurr != nullptr && mCurr->mData !=deldata ){
mTemp =mCurr;
mCurr = mCurr->mNext;
}
if (!mCurr){
cout << deldata << " was not in the list\n";
delete delptr;
}
else {
if (mHead == mCurr)
mHead = nullptr;
delptr = mCurr;
mCurr = mCurr->mNext;
mTemp ->mNext=mCurr;
delete delptr;
cout << "The value "<< deldata << " was deleted\n";
}
}
void CLinkedlist::Printlist(){
mCurr = mHead;
while (mCurr)
{
cout << mCurr->mData << " -> " << endl;
mCurr = mCurr->mNext;
}
}
#include <cstdlib>
#include "Linklist.h"
using namespace std;
int main (){
CLinkedlist John;
John.AddNode(3);
}
On top of that, I am getting various errors on Valgrind like the ones below. Based on uninitialised values
=24530== Use of uninitialised value of size 4
==24530== at 0x4AFB934: free_mem (in /lib/arm-linux-gnueabihf/libc-2.19.so)
==24530== by 0x4AFB3C3: __libc_freeres (in /lib/arm-linux-gnueabihf/libc-2.19.so)
==24530== by 0x4023633: _vgnU_freeres (vg_preloaded.c:61)
==24530== by 0x48C5ED7: std::ctype<char>::_M_widen_init() const (in /usr/lib/arm-linux-gnueabihf/libstdc++.so.6.0.20)
==24530==
==24530== Conditional jump or move depends on uninitialised value(s)
==24530== at 0x4AFB9B0: free_mem (in /lib/arm-linux-gnueabihf/libc-2.19.so)
==24530== by 0x4AFB3C3: __libc_freeres (in /lib/arm-linux-gnueabihf/libc-2.19.so)
==24530== by 0x4023633: _vgnU_freeres (vg_preloaded.c:61)
==24530== by 0x48C5ED7: std::ctype<char>::_M_widen_init() const (in /usr/lib/arm-linux-gnueabihf/libstdc++.so.6.0.20)
==24530==
==24530==
==24530== HEAP SUMMARY:
==24530== in use at exit: 8 bytes in 1 blocks
==24530== total heap usage: 1 allocs, 0 frees, 8 bytes allocated
==24530==
==24530== LEAK SUMMARY:
==24530== definitely lost: 0 bytes in 0 blocks
==24530== indirectly lost: 0 bytes in 0 blocks
==24530== possibly lost: 0 bytes in 0 blocks
==24530== still reachable: 8 bytes in 1 blocks
==24530== suppressed: 0 bytes in 0 blocks
What type of things can I try to do with my existing code to fully return back memory to the operating system?
For testing purposes, I used a Blueberry Pi on a Linux operating system. In addition, I used a g++ compiler and included -std=c++11 flags if that helps.
What I have tried:
I have done thorough research on my deciphering my memory leaks, but either the things I found was programmed through c or not applicable in my situation. One of the issues I found that I potentially was I didn't initialize my values which are why I was getting those errors. However, after careful inspection, I initialize all my in my class declarations. I was able to trace the problem to my destructor, apparently, it isn't deallocating the memory properly. However, it could be a deeper issue. My DeleteNode () method when called in the main, is able to deallocate the memory, but I don't how or why it can do that but not my destructor.
Update
I tried various methods suggested on this forum, but unfortunately, that didn't work. I connected to various Raspberry Pies, including Blueberry, raspberry, and pennutbutter to test on my virtual Linux machine but whenever I use Valgrind to test my application, in addition to the error message that I already showed I also get an illegal instruction,(not shown). Also, I used Putty, but it had the same effect. However, I have sort of given up, Visual Studio it doesn't report I have any leaks and neither do you guys so I honestly don't know and you guys don't seem to find anything wrong with how I handle the memory allocation and deallocation, so I don't know.