我正在使用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實例持續存在的循環引用?