2 years ago
#62131

Snow_Ball
Destructor can not find member variable, can not delete memory, leading to memory leaks
I implement a class RedBlackTree like this. My questions are at the end.
template < class T,
class Node = ft::treeNode<T>,
class Type_Alloc = std::allocator<T>,
class Node_Alloc = std::allocator<Node>
>
class RedBlackTree
{
public:
typedef T value_type;
typedef size_t size_type;
RedBlackTree()
: _tree_size(0),
_alloc_node(Node_Alloc()),
_root(NULL)
{}
~RedBlackTree()
{
deleteTree(_root);
}
void deleteTree(Node* node)
{
if(node == NULL)
return ;
deleteTree(node->left);
deleteTree(node->right);
_alloc_node.destroy(node);
_alloc_node.deallocate(node, 1);
}
void insertValue(const value_type &val)
{
Node *pt = _alloc_node.allocate(1);
_alloc_node.construct(pt, Node(val));
_root = RBT_Insert(_root, pt);
}
Node* RBT_Insert(Node* root, Node* pt)
{...}
private:
size_type _tree_size;
std::allocator<Node> _alloc_node;
Node* _root;
};
With a struct treeNode like this:
template <typename T>
struct treeNode
{
public:
typedef T value_type;
value_type value;
treeNode* parent;
treeNode* left;
treeNode* right;
bool color;
treeNode(treeNode* parent = NULL, treeNode* left = NULL, treeNode* right = NULL)
: value(),
parent(parent),
left(left),
right(right),
color(BLACK)
{
}
treeNode(const value_type& val, treeNode* parent = NULL, treeNode* left = NULL, treeNode* right = NULL)
: value(val),
parent(parent),
left(left),
right(right),
color(RED)
{}
virtual ~treeNode()
{
}
};
I have problem with the destructor of my class RedBlackTree. It has a member method called insertValue which allocates memory to a new treeNode. So I thought at the destruction I need to deallocate all the memory, if not it will cause memory leaks. However it can not compile because of this error:
./map/red_black_tree.hpp: In instantiation of ‘void ft::RedBlackTree<T, Node, Type_Alloc, Node_Alloc>::deleteTree(Node*) [with T = ft::pair<const char, int>; Node = std::less<char>; Type_Alloc = std::allocator<ft::pair<const char, int> >; Node_Alloc = std::allocator<std::less<char> >]’: ./map/red_black_tree.hpp:59:27: required from ‘ft::RedBlackTree<T, Node, Type_Alloc, Node_Alloc>::~RedBlackTree() [with T = ft::pair<const char, int>; Node
= std::less<char>; Type_Alloc = std::allocator<ft::pair<const char, int> >; Node_Alloc = std::allocator<std::less<char> >]’ ./map/map.hpp:69:22: required from ‘ft::map<Key, T, Compare, Alloc>::map(const key_compare&, const allocator_type&) [with Key = char; T = int; Compare = std::less<char>; Alloc = std::allocator<ft::pair<const char, int> >; ft::map<Key, T, Compare, Alloc>::key_compare = std::less<char>; ft::map<Key, T, Compare, Alloc>::allocator_type = std::allocator<ft::pair<const char, int> >]’ main_map.cpp:122:24: required from here ./map/red_black_tree.hpp:66:34: error: ‘struct std::less<char>’ has no member named ‘left’; did you mean ‘less’?
deleteTree(node->left);
~~~~~~^~~~
less ./map/red_black_tree.hpp:67:34: error: ‘struct std::less<char>’ has no member named ‘right’
deleteTree(node->right);
~~~~~~^~~~~ <builtin>: recipe for target 'main_map.o' failed
I don't know why it cannot get the right class of Node? It thinks that class Node is std::less instead of my ft::treeNode.
Is there a better way to do destruction?
Answer: I declared my variable _rbt in my class map wrongly. It was like that:
template < class Key, class T, class Compare = std::less<Key> >
class map
{
private:
Compare _compare;
ft::RedBlackTree<T, Compare> _rbt;
};
the last line of code should be like this:
ft::RedBlackTree<T> _rbt;
c++
memory-leaks
destructor
0 Answers
Your Answer