我假设ptr_set
如果未手动删除从 a 释放的对象,则会泄漏该对象。但是,下面的测试程序仅显示 valgrind 中有 2 个泄漏(从第 9/13 行开始),第 12 行没有泄漏。我误解了release
,还是ptr_container
设法以某种方式进行清理?
#include <iostream>
#include <boost/ptr_container/ptr_set.hpp>
typedef boost::ptr_set<int> SetInt;
int main() {
SetInt s;
new int(1); // leak, reported by valgrind
s.insert(new int(2));
s.insert(new int(3)); // leak? not reported by valgrind
s.insert(new int(4)); // leak, reported by valgrind
s.erase(s.begin());
s.release(s.begin()); // release '3'
SetInt::auto_type old_int1 = s.release(s.begin());
int *old_int2 = old_int1.release();
std::cout << '\'' << *old_int2 << "' has been released\n";
}
输出:
'4' has been released