Reputation: 10137
I have a template class (Node
is an inner class within a BST). It's now time to free up the memory; Given that either the key or the value (or both) may be pointers, I must figure out how to free them if they are.
See an example:
~Node( void )
{
if ( is_pointer< TValue >( Value ) )
{
delete Value;
Value = NULL;
}
if ( is_pointer< TComparable >( Key ) )
{
delete Key;
Key= NULL;
}
}
The implementation behind the is_pointer< T >
function works (Taken from here), however as soon as I press delete on either Key or Value, I get the following:
Error 13 error C2440: 'delete' : cannot convert from 'esc::Shader' to 'void *' c:\programming\c++\git\escalator\engine\engine\searchtree.hpp 131
Error 14 error C2440: 'delete' : cannot convert from 'esc::ShaderComparable' to 'void *' c:\programming\c++\git\escalator\engine\engine\searchtree.hpp 137
Error 12 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion) c:\programming\c++\git\escalator\engine\engine\searchtree.hpp 130
I've tried static_cast
, dynamic_cast
, reinterpret_cast
, etc, but neither of these appear to work.
What's a good solution?
Upvotes: 1
Views: 1258
Reputation: 470
This really depends on what kind of software you're working on here. Unless it's some quick test and you're not going to reuse the code, then yes, don't bother distinguishing pointers from objects or arrays. But if you're writing library code and you think your component will be reused by other people, then you should take care about cleaning after yourself. STL vector has been doing this successfully since the dawn of times. Last I saw the code they've been calling a Destroy function for (First,Last) elements of the vector with an value tag passed into the function as the third argument. And if elements are just plane scalar pointers (meaning int* for instance), then destructors need not be called. Obviously the real data has been allocated by the user and only addressed of the data has been stored in a vector, so it should not be deallocated. But if it's objects that are stored in the vector (objects of a user-defined class for instance, class A, lets say), then destructors need to be called for each element in the vector ~A() and after all of them ran to the end, the contiguous memory chunk that used to store the elements should be deallocated through the use of vectors allocator. For more information you can easily open vectors implementation, it's all in the header file, look at ~vector() implementation and let that guide you.
Upvotes: 0
Reputation: 146910
I must figure out how to free them if they are.
Don't. Really- don't. That's the user's problem- and he can supply a smart pointer if he wants this behaviour. After all, what if I want to map non-owning pointers? Or need a custom deleter?
Also, your code does not work because you compile the dead code if
branches anyway, because you did not use a specialization.
Upvotes: 4
Reputation: 100527
It looks like you are storing copies of elements of type T instead of pointers as commonly done. If your declaration is T Value;
than your Node class is normally not responsible for deleting Value object.
Upvotes: 2