By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

memory leaks question

P: n/a
Hi,

I have a class representing a tree-like structure called Node. Each Node
contains a vector of pointers to other nodes ie stl::vector<Node*>. The
Node class itself contains a destructor method to delete all the nodes
within its vector as below:

Node::~Node() {
for(vector<Node*>::iterator it = outedges.begin(); it != outedges.end();
++it)
delete *it;
}

All the nodes were taken from dynamic memory using 'Node* newnode = new
Node; node->add(newnode);' etc, except the initial Root node, which is just
an attribute of another class. My question is, if I call the destructor
method on just the root node, what will happen to all the nodes pointed to
by its sub-nodes amd the sub-nodes sub-nodes etc. Will the entire tree be
deleted or will it leave pointers with no owner, meaning that I should
traverse the entire tree, deleting all leaf nodes, then moving back up to
finally delete the root? I'm hoping that I can just destroy the root and
then everything else will be ok.

Thanks for your help
Jason.
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

Sheesh, I just read you dont call destructors automatically, therefore, now
the destructor is defined, is it all taken care of?

"Jason" <ja***********@btinternet.com> wrote in message
news:br**********@hercules.btinternet.com...
Hi,

I have a class representing a tree-like structure called Node. Each Node
contains a vector of pointers to other nodes ie stl::vector<Node*>. The
Node class itself contains a destructor method to delete all the nodes
within its vector as below:

Node::~Node() {
for(vector<Node*>::iterator it = outedges.begin(); it != outedges.end();
++it)
delete *it;
}

All the nodes were taken from dynamic memory using 'Node* newnode = new
Node; node->add(newnode);' etc, except the initial Root node, which is just an attribute of another class. My question is, if I call the destructor
method on just the root node, what will happen to all the nodes pointed to
by its sub-nodes amd the sub-nodes sub-nodes etc. Will the entire tree be
deleted or will it leave pointers with no owner, meaning that I should
traverse the entire tree, deleting all leaf nodes, then moving back up to
finally delete the root? I'm hoping that I can just destroy the root and
then everything else will be ok.

Thanks for your help
Jason.

Jul 22 '05 #2

P: n/a
Jason wrote:

Hi,

I have a class representing a tree-like structure called Node. Each Node
contains a vector of pointers to other nodes ie stl::vector<Node*>. The
Node class itself contains a destructor method to delete all the nodes
within its vector as below:

Node::~Node() {
for(vector<Node*>::iterator it = outedges.begin(); it != outedges.end();
++it)
delete *it;
}

All the nodes were taken from dynamic memory using 'Node* newnode = new
Node; node->add(newnode);' etc, except the initial Root node, which is just
an attribute of another class. My question is, if I call the destructor
method on just the root node, what will happen to all the nodes pointed to
by its sub-nodes amd the sub-nodes sub-nodes etc. Will the entire tree be
deleted or will it leave pointers with no owner, meaning that I should
traverse the entire tree, deleting all leaf nodes, then moving back up to
finally delete the root? I'm hoping that I can just destroy the root and
then everything else will be ok.


You don't need to destroy the root node, it will be done automatically
when the object that contains the root node gets destroyed.
But then the dtor kicks in and destroys all of the roots children
which by themself will destroy all there children etc. until the
whole structure is deleted.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.