Invalid Address specified to RtlFreeHeap on Destructor
I am trying to preserve the original value of an object for use at each iteration of a loop, however on when the elements are deleted which are only in scope of the loop, I get an error which says: "Invalid Address specified to RtlFreeHeap." I can't figure out what I am doing wrong.
Here is a sample of my code:
Code:
void mainTest (Graph_ptr _dataGraph)
{
Graph originalDataGraph = (*_dataGraph);
for(int i=0; i<10; i++)
{
Graph dataGraphCopy = originalDataGraph;
Graph_ptr dataGraph = Graph_ptr(&dataGraphCopy);
// do some other things
}
}
Note this code works properly if I change the dataGraph definition to:
Code:
Graph_ptr dataGraph = _dataGraph;
However, by doing this, I lose the original value of the dataGraph which I need at the start of each loop iteration. Also, Graph_ptr is just a boost pointer of type Graph.
What could be the problem?
Thanks.
Re: Invalid Address specified to RtlFreeHeap on Destructor
Quote:
Originally Posted by
NTL
Graph_ptr is just a boost pointer of type Graph.
do you mean a boost::shared_ptr ? if yes, in the line "Graph_ptr dataGraph = Graph_ptr(&dataGraphCopy);" you are constructing a shared pointer from a pointer to an object on the stack.
instead of
Code:
Graph dataGraphCopy = originalDataGraph;
Graph_ptr dataGraph = Graph_ptr(&dataGraphCopy);
you should write simply
Code:
Graph_ptr dataGraph( new Graph( originalDataGraph ) );
or
Code:
Graph_ptr dataGraph = boost::make_shared<Graph>( originalDataGraph );
anyway, the way you code makes me think you might not have a proper understanding of C++ basics ( and boost smart pointer library for what matters ). I say this because coding this way in C++ is dangerous and it inevitably leads to unstable/buggy programs even when they appear working as expected. Just a friendly warning ... :)