Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C++
#ifndef LINKEDLIST
    #define LINKEDLIST  
    /** LinkedList Class Interface
    */
  //begining update  
 class CLinkedlist {
    private:
    struct node {
        node() : mData(0), mNext(nullptr) {}
        int mData;
        node* mNext;
	};
    typedef node *nodePtr;
        
    nodePtr mHead;
    // As already suggested these should be local variables
    nodePtr mCurr;
    nodePtr mTemp;
	public:
    CLinkedlist() : mHead(nullptr), mCurr(nullptr), mTemp(nullptr) {}
    /// end of update
    ~CLinkedlist();
    
        void AddNode( int);
        void DeleteNode( int );
        void Printlist();
    
    };
    
    #endif

    /** \file Linkedlist.cpp
    */

   
    /** Linkedlist Deconstructor
    */
    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)
      {
        /// starting from the head, walking down the list
        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;
      /// The program either walked the entire list and found
      /// nothing or we found our data
      while (mCurr != nullptr && mCurr->mData !=deldata ){
        mTemp =mCurr;
        mCurr = mCurr->mNext;
      }
      if (!mCurr){
        cout << deldata << " was not in the list\n";
        delete delptr;
      }
      else {
        /// new code
        if (mHead == mCurr)
           mHead = nullptr;
        /// old code
        delptr = mCurr;
        mCurr = mCurr->mNext;
        mTemp ->mNext=mCurr;
        delete delptr;
        cout << "The value "<< deldata << " was deleted\n";
      }
    }
    /** Displays information about our current list
    */
    void CLinkedlist::Printlist(){
      mCurr = mHead;
      while (mCurr)
      {
        cout << mCurr->mData << " -> " << endl;
        mCurr = mCurr->mNext;
      }
    }

    /** 
     * \file main.cpp
     */
    #include <cstdlib>
    #include "Linklist.h"
    
    using namespace std;
    
    int main (){
      CLinkedlist John;
    
      John.AddNode(3);
      //John.DeleteNode(3);
      //John.AddNode(5);
      //John.AddNode(7);
      //John.Printlist();
    
    }


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.
Posted
Updated 21-Jul-17 6:55am
v5
Comments
Jochen Arndt 17-Jul-17 4:29am    
I have tested your code with Ubuntu and it does not report memory leaks.

However, it seems that you are using a Raspberry Pi or another ARM based embedded system. You might add this information and the used G++ command line to your question so that others can try to reproduce the error. Use the green "Improve question" link to edit your question.
Mohibur Rashid 18-Jul-17 3:05am    
couple of issues
if your mHead get deleted, you also need to set mHead=nullptr as well as mCurr=nullptr
otherwise, it will throw error after deleting first item. to solve this, just add a verification at the top to check if your search item match with the mHead and follow accordingly.

if you add a new item make sure next item of your new item is set to nullptr, otherwise many thing will get error prone.

Why your mTemp is global?
John Last 18-Jul-17 15:54pm    
Okay yes mTemp is a global variable. I see your point, I added a check so whenever I deleted a new node I checked if it was the head node and set it to nullptr before I began deleting (in fact I just updated the code to reflect that). Additionally, by default, any new node that I add it's mNext will be a nullptr and its data is 0. I tried this same code on visual studio 2015, and it reports no memory leaks, but when I tried on a linux virtual machine via a terminal it says I still memory leakage.
Mohibur Rashid 19-Jul-17 3:21am    
I should not have called the member variable a global variable. Anyway
valgrind show no memory leak in ubuntu

==19837== Memcheck, a memory error detector
==19837== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==19837== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==19837== Command: ./output
==19837==
The value 3 was deleted
5 ->
7 ->
5 was destroyed.
7 was destroyed.
==19837==
==19837== HEAP SUMMARY:
==19837== in use at exit: 0 bytes in 0 blocks
==19837== total heap usage: 5 allocs, 5 frees, 73,776 bytes allocated
==19837==
==19837== All heap blocks were freed -- no leaks are possible
==19837==
==19837== For counts of detected and suppressed errors, rerun with: -v
==19837== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

checked also with -v option. So, I am guessing nothing

1 solution

It looks like this is a compiler / platform / LIBC dependant problem. As already noted I have tested the code (Ubuntu 14.04.05 LTS 32 bit, GCC 4.8.4) and had no memory leak.

The message "Use of uninitialised value of size 4" may indicate that there is an unitialised value (a pointer passed to free_mem).

To be on the safe side I suggest to use the constructors to initialise member variables. I'm not sure if your method ensures that the members are always initialised.
C++
class CLinkedlist {
private:
    struct node {
        node() : mData(0), mNext(nullptr) {}
        int mData;
        node* mNext;
    };
    typedef node *nodePtr;
        
    nodePtr mHead;
    // As already suggested these should be local variables
    nodePtr mCurr;
    nodePtr mTemp;
public:
    CLinkedlist() : mHead(nullptr), mCurr(nullptr), mTemp(nullptr) {}
    // ...
};
 
Share this answer
 
Comments
John Last 21-Jul-17 12:47pm    
I tried your method, 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, like I said on Visual Studio 2015, it doesn't report I have any leaks, so I honestly don't know and you guys don't seem to find anything wrong with how I handle memory, so I don't know.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900