我的代码使用堆内存分配器。以下代码中引用的leafNode对象是将从分配器获得的堆ptr强制转换为struct:leafNode的结果。 leafNode结构包含一个std :: string数组和另一个RID对象数组,没有其他字段。由于我是在堆上处理这些字符串(在keyArray中),因此我认为在分配时没有正确清理它们。运行valgrind,我看到如下输出。我应该如何避免这些内存泄漏?
11 bytes in 1 blocks are definitely lost in loss record 1 of 20
==5952== at 0x4837DEF: operator new(unsigned long) (vg_replace_malloc.c:334)
==5952== by 0x49D1A1F: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_assign(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (basic_string.tcc:265)
==5952== by 0x49D1D9D: assign (basic_string.h:1366)
==5952== by 0x49D1D9D: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (basic_string.h:695)
==5952== by 0x11DC11: void db::Index::insertIntoLeaf<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, db::LeafNodeString, db::NonLeafNodeString>(db::RIDKeyPair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, db::LeafNodeString*)
leafNode结构:
struct LeafNode {
std::string keyArray[STRINGARRAYLEAFSIZE2];
RecordId ridArray[STRINGARRAYLEAFSIZE2];
};
这是insertIntoLeaf函数的代码:
int index = 0;
for (int i = 0; i < leafOccupancy; i++) {
if (leafNode->ridArray[i].page_number == 0 ||
entry.key <
/*=*/leafNode->keyArray[i]) { // no duplicates so no need for <=
index = i;
if (leafNode->ridArray[index].page_number != 0 &&
index < leafOccupancy - 1) {
std::rotate(&(leafNode->keyArray[index]),
&(leafNode->keyArray[leafOccupancy - 1]),
&(leafNode->keyArray[leafOccupancy]));
std::rotate(&(leafNode->ridArray[index]),
&(leafNode->ridArray[leafOccupancy - 1]),
&(leafNode->ridArray[leafOccupancy]));
}
leafNode->keyArray[index] = entry.key;
leafNode->ridArray[index] = entry.rid;
return;
}