> This is what I use as the main lists, but some of my other lists will contain pointers to allocated
> memory contained in the main lists: ... Geometry is the class the holds all the allocated blocks.
> All other pointers are simply used to reference into Geometry's list of triangles and vertices.
I presume that: The Geometry object is a long-lived object, and it has a destructor that takes care of destroying the vertices and triangles. The list of Span_blocks is a short lived object which hold objects of type Span_block by value; with each Span_block containing non-owning pointers to objects held in the Geometry object.
The temporary lists of Span_block objects are of the type
std::list<Span_block>
- that is the Span_block objects are not dynamically allocated. In this case, there is no issue - you can just let the list pass out of scope and the underlying list container will take care of the clean up.
If these temporary lists are of type
std::list<Span_block*>
, with each Span_block being allocated dynamically, the list destructor of the list will not destroy the Span_block objects. In this case, you will have to write java-like spaghett:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
{
std::list< Span_block* > seq ;
try
{
// ...
seq.push_back( new Span_block( /* ... */ ) ) ;
// ...
// do thing with list
// ...
}
catch( ... ) // simulated finally
{
for( Span_block* p : lst ) delete p ;
throw ;
}
for( Span_block* p : lst ) delete p ;
}
|
Writing error handling code in places where you do not know how to handle errors is an anti-pattern in C++. C++ has first class language support for RAII; it is by design that C++ does not have a try-finally construct.
The idiomatic way to do this would be:
1 2 3 4 5 6 7 8
|
{
std::list< std::unique_ptr<Span_block> > seq ;
// ...
seq.emplace_back( new Span_block( /* ... */ ) ) ;
// ...
// do thing with list
// ...
}
|