【发布时间】:2015-05-20 09:41:44
【问题描述】:
我是否应该使用带有 boost::object_pool 的共享指针来防止内存泄漏(以防 malloc-destroy 块内出现异常)?
如果是,初始化 shared_ptr 的正确方法是什么?
之后如何清理内存?
#include <boost/pool/object_pool.hpp>
#include <boost/shared_ptr.hpp>
int main() {
boost::object_pool<int> pool;
// Which is the correct way of initializing the shared pointer:
// 1)
int *i = pool.malloc();
boost::shared_ptr<int> sh(i);
int *j = pool.construct(2);
boost::shared_ptr<int> sh2(j);
// or 2)
boost::shared_ptr<int> sh(pool.malloc());
// Now, how should i clean up the memory?
// without shared_ptr I'd call here pool.destroy(i) and pool.destroy(j)
}
【问题讨论】:
标签: boost shared-ptr pool