-2

I am implementing star cubing algorithm for data aggregation in c++(I am new to c++). In this algorithm we have big data. In first step we build a base tree using all data, then based on base tree we should build other trees,named star trees, after each star tree is completed, its values are outputted, and the star tree should be destroyed. I did all that process, my algorithm works, but its memory usage is very much, and it seems these star trees are not destroyed. Below is part of my code that I think is useful for this problem:

For each tree which I intend to destroy, I pass its root to destroyTree(node* root) method:

void Cagg::destroyTree(node* root){
   int size = root->child.size();
   for (int i = 0; i < size; i++){
        destroyTree(root->child[i]);
    }
   delete root;
   root = NULL;

}

when debugging, it seems node is deleted, but the memory usage of the program is going up exponentially while the program is creating more star trees, which means they are not destroyed. Moreover, the destructor is empty. I just delete nodes in this method. Thank you in advance for helping me.

Moreover I inherit node struct from another class. Is it possible that's the problem? and both class destructors are empty.

sahar
  • 1
  • 2
  • Nothing truly hideous going on here. I'd look elsewhere for the leak. And I'd also look into storing nodes rather than pointers to nodes. Why have put up with manual memory management at all? If available on your OS, look into giving valgrind a whack at finding the leak. – user4581301 Apr 01 '16 at 00:30
  • I've never heard of it. (I am really new ;) ). I just search it. It seems valgrind is for unix os, am I right? I am using windows. Do you have any suggestion for windows?Thank you – sahar Apr 01 '16 at 00:47
  • Nothing with which I am familiar enough to recommend. Here is a hoary post on Windows substitutes: http://stackoverflow.com/questions/413477/is-there-a-good-valgrind-substitute-for-windows – user4581301 Apr 01 '16 at 00:49
  • do you mean I am deleting pointer to nodes instead of storing nodes? – sahar Apr 01 '16 at 00:51
  • What I'm talking about with the storing nodes vs pointers comment is, do you lose much if you have `vector child` instead of `vector child` where I'm assuming your container is `vector` but could be just about any `std::` container. `vector` makes all of your memory woes go away if your code can support it. – user4581301 Apr 01 '16 at 01:22
  • Moreover I inherit node struct from another class. Is it possible that's the problem? and both class destructors are empty. – sahar Apr 05 '16 at 00:30

1 Answers1

1

Nothing here that will leak memory, but you don't need the explicit recursion. All you need is this:

node::~node()
{
    while (size() > 0) {
        delete child[size()-1];
    }
}

and delete root; somewhere.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • it does not work, I add 2 lines to the end of my question, could you pls read that too. Maybe it is helpful to find the bug. – sahar Apr 05 '16 at 00:31