我正在使用boost :: asio来执行后台任务的一组类。实际上,该程序会持续运行,但我在测试期间添加了信号处理程序进行清理。在使用boost :: asio和boost :: thread时了解内存泄漏
但是,当收到SIGINT后监视代码中的函数调用时,我发现我的对象的私有实现没有像预期的那样被销毁 - 内存泄漏。它使用boost :: shared_ptr进行管理。私有实现类如下所示。
class TestImpl: public boost::enable_shared_from_this<TestImpl>, boost::noncopyable {
TestImpl(): update_timer(io_svc), signals(io_svc, SIGINT, SIGTERM) {
signals.async_wait(boost::bind(&boost::asio::io_service::stop, &io_svc));
};
public:
virtual ~TestImpl() {
std::cout << "Destroyed." << std::endl;
};
static boost::shared_ptr<TestImpl> create() {
boost::shared_ptr<TestImpl> ptr(new TestImpl);
ptr->start();
return ptr;
}
void start() {
update_timer.expires_from_now(boost::posix_time::seconds(1));
update_timer.async_wait(boost::bind(&TestImpl::update, shared_from_this()));
run_thread = boost::thread(boost::bind(&TestImpl::run, shared_from_this()));
};
void cleanup() {
run_thread.join();
};
private:
void run() {
io_svc.run();
};
void update() {
std::cout << "Updating." << std::endl;
update_timer.expires_from_now(boost::posix_time::seconds(1));
update_timer.async_wait(boost::bind(&TestImpl::update, shared_from_this()));
};
boost::asio::io_service io_svc;
boost::asio::deadline_timer update_timer;
boost::thread run_thread;
boost::asio::signal_set signals;
};
下面是使用私有实现的代码。
class Test {
public:
Test(): impl(TestImpl::create()) { };
virtual ~Test() { std::cout << "Destroyed." << std::endl; };
int run() {
boost::asio::signal_set signals(io_svc, SIGINT, SIGTERM);
signals.async_wait(boost::bind(&boost::asio::io_service::stop, &io_svc));
io_svc.run();
impl->cleanup();
return 0;
};
private:
boost::asio::io_service io_svc;
boost::shared_ptr<TestImpl> impl;
};
int main() {
Test test;
test.run();
}
我无法理解为什么TestImpl类被泄漏。通过调试,我可以验证两个io_service实例在SIGINT上停止并且该线程被加入,这导致我相信在销毁时它不会被分离。似乎必须有一个引起TestImpl实例持续存在的循环引用?