Boost C++ Libraries: Ticket #6654: deadline_timer may makes dll can't be freed https://svn.boost.org/trac10/ticket/6654 <p> When I use boost::asio::deadline_timer in a dll, <code>FreeLibrary</code> will be blocked. When I commented the deadline_timer, things become ok. </p> <p> I compile my program on Visual Studio 2010(without sp), and Win Xp sp3. dll was built as Debug configuration, using /MDd </p> <p> below is my code in main(): </p> <pre class="wiki"> HMODULE hmod2 = LoadLibraryA("tDll2.dll"); printf("tDll2.dll loaded\n"); for (;;) { char cmd[100]; scanf("%s", cmd); if (strcmp(cmd, "init") == 0) { void_func _init = (void_func)GetProcAddress(hmod2, "init"); _init(); printf("init ok\n"); } else if (strcmp(cmd, "uninit") == 0) { void_func _uninit = (void_func)GetProcAddress(hmod2, "uninit"); _uninit(); printf("uninit ok\n"); } else if (strcmp(cmd, "exit") == 0) { FreeLibrary(hmod2); printf("tFreeLib.dll free\n"); break; } } </pre><p> code of tDll2.cpp in tDll2.dll </p> <pre class="wiki"> boost::asio::io_service client_ioservice; boost::asio::io_service::work* client_ioservice_virtual_work; boost::thread* th; void run() { boost::asio::deadline_timer timer(client_ioservice); boost::system::error_code error; client_ioservice.run(error); if (error) { printf("error: %d\n", error.value()); } delete th; printf("finish\n"); } TDLL2_API void init(void) { client_ioservice_virtual_work = new boost::asio::io_service::work(client_ioservice); th = new boost::thread(run); } TDLL2_API void uninit(void) { delete client_ioservice_virtual_work; } </pre><p> this statement <code>printf("finish\n"); </code>is successfully executed, the <code>DllMain</code> is also successfully returned, but <code>FreeLibrary(hmod2);</code> blocks. </p> <p> When I commented this line: </p> <pre class="wiki"> boost::asio::deadline_timer timer(client_ioservice); </pre><p> things become ok. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/6654 Trac 1.4.3 weiyuemin@… Tue, 06 Mar 2012 09:00:25 GMT <link>https://svn.boost.org/trac10/ticket/6654#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6654#comment:1</guid> <description> <p> The console input and output: </p> <pre class="wiki">tDll2.dll loaded init init ok uninit uninit ok finish exit </pre><p> It blocks on </p> <pre class="wiki">FreeLibrary(hmod2); </pre> </description> <category>Ticket</category> </item> <item> <author>weiyuemin@…</author> <pubDate>Thu, 22 Mar 2012 08:00:25 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6654#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6654#comment:2</guid> <description> <p> declare client_ioservice as boost::asio::io_service*, and manually release the io_service* would avoid this problem. </p> <p> if I declare io_service as a global variable in dll, or wrap it with shared_ptr, then it(the io_service) can't be properly released. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>chris_kohlhoff</dc:creator> <pubDate>Tue, 29 May 2012 00:25:53 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/6654#comment:3 https://svn.boost.org/trac10/ticket/6654#comment:3 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> <p> To correctly clean up global io_service objects in a DLL, call </p> <pre class="wiki">boost::asio::detail::win_thread::set_terminate_threads(true) </pre><p> at some point prior to the <a class="missing wiki">FreeLibrary</a> call. </p> Ticket