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. 2 1086
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Maurice |
last post by:
Hi there,
I'm experiencing big memory problems on my webserver. First on an old
RedHat 7.2 system, now on an other fresh installed Suse 8.2 system:
Linux version 2.4.20-4GB...
|
by: Mark D. Anderson |
last post by:
About a month ago Richard Cornford did an interesting analysis of a
memory leak
in jscript (internet explorer) when there are "circular" references
between
DOM objects and (real) jscript objects:...
|
by: Steve Binney |
last post by:
My code makes synchronous HttpWebRequest and HttpRebResponse calls. In VS
2003, I am getting memory leaks and event handle leaks. I am closing all
streams and using "using"statements. I have...
|
by: Generic Usenet Account |
last post by:
I have been using STL for a long time now, without any problems.
Recently we generated a purification report on our software using
Rational Purify, and we found some memory leaks. My colleague...
|
by: s.subbarayan |
last post by:
Dear all,
I happen to come across this exciting inspiring article regarding
memory leaks in this website:
http://www.embedded.com/story/OEG20020222S0026
In this article the author mentions:...
|
by: ranjeet.gupta |
last post by:
Dear All
Is the Root Cause of the Memory corruption is the Memory leak, ??
suppose If in the code there is Memory leak, Do this may lead to the
Memory Corruption while executing the program ?
...
|
by: ali.jan |
last post by:
Hi,
It is trivial to load an assembly in a new Application Domain. Is there
any way of loading an assembly in a new process?
I tried using the Process class like this:
Process p = new...
|
by: Jim Land |
last post by:
Jack Slocum claims here
http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript-
memory-leaks/
that "almost every site you visit that uses JavaScript is leaking memory".
...
|
by: Ragnar Agustsson |
last post by:
Hi all
I have been wandering about the best way to sandbox memory leaks in 3rd
party libraries when using them from the .Net framework.
I have a 3rd party library, written in C++, that leaks a...
|
by: graham.keellings |
last post by:
hi,
I'm looking for an open source memory pool. It's for use on an
embedded system, if that makes any difference. Something with garbage
collection/defragmentation would be nice. It should have...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |