Home > Back-end >  Memory leak while destroying a BST
Memory leak while destroying a BST

Time:12-01

I'm trying to code a destructor for my BST so that I don't have to delete everything manually. I've tried multiple destructors for my BST but I keep getting a memory leak when running with valgrind. Here is my code (destructor at the end of code).

#include <fstream>
#include <iostream>
#include <string>
using namespace std;
//struct for names
struct User
{
    string firstname;
    string lastname;
};


//BST struct where the data in the BST are the names in struct User
struct BST
{
    User* value;
    int key=0;
    BST* leftTree;
    BST* rightTree;
};
// creates a BST ( I do it like this incase i want to run a new function on a BST, but with a field to keep track of the size and initialize to 0)
BST* createBST(){
    BST* n = nullptr;
    return n;
}
// checks if empty
bool isEmpty(BST* tree){
    if(tree == nullptr){
        return true;
    }
    return false;
}
// destroy the BST completely.
void destroy(BST* tree){
    if (tree != nullptr){
        delete tree->value;
        destroy(tree->leftTree);
        destroy(tree->rightTree);
        delete tree;
    }
}

Main to see if code works:

// TEST to see if code works correctly.
int main() {
    BST* bst = createBST();
    // I don't have an insert function yet so I do it manually
    bst = new BST;
    bst->key = 15;
    bst->value = new User{"John","15"};
    bst->leftTree = new BST;
    bst->leftTree->key = 5;
    bst->leftTree->value = new User{"John","15"};
    destroy(bst);
    return 0;
}

edit: added delete tree->value; but I still get this error:

==34== Memcheck, a memory error detector
==34== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==34== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info
==34== Command: student/labo12
==34==
==34== Conditional jump or move depends on uninitialised value(s)
==34==    at 0x400923: destroy(BST*) (in /task/student/labo12)
==34==    by 0x400950: destroy(BST*) (in /task/student/labo12)
==34==    by 0x400950: destroy(BST*) (in /task/student/labo12)
==34==    by 0x400ABD: main (in /task/student/labo12)
==34==
==34== Conditional jump or move depends on uninitialised value(s)
==34==    at 0x400923: destroy(BST*) (in /task/student/labo12)
==34==    by 0x400960: destroy(BST*) (in /task/student/labo12)
==34==    by 0x400950: destroy(BST*) (in /task/student/labo12)
==34==    by 0x400ABD: main (in /task/student/labo12)
==34==
==34== Conditional jump or move depends on uninitialised value(s)
==34==    at 0x400923: destroy(BST*) (in /task/student/labo12)
==34==    by 0x400960: destroy(BST*) (in /task/student/labo12)
==34==    by 0x400ABD: main (in /task/student/labo12)
==34==
==34==
==34== HEAP SUMMARY:
==34==     in use at exit: 0 bytes in 0 blocks
==34==   total heap usage: 8 allocs, 8 frees, 208 bytes allocated
==34==
==34== All heap blocks were freed -- no leaks are possible
==34==
==34== For counts of detected and suppressed errors, rerun with: -v
==34== Use --track-origins=yes to see where uninitialised values come from
==34== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 0 from 0)

CodePudding user response:

Count the number of new operators and delete operators in your code. They should match. But it your code you care only of the BST objects, and never delete the User.

Why don't you implement the destructor of BST that correctly deletes all the fields?

Update Ok, after deleting the User you have a problem of incorrectly initialized fields. You have no explicit constructor for BST, so the pointers to left, right and user are initialized with junk. That is what valgrind points you to now.

  • Related