Re: [Boost-bugs] [Boost C++ Libraries] #3605: Boost.Asio: Static Initialization causes Deadlock during WSAStartup and other problems (was: Boost.Asio: Deadlock during WSAStartup)

Subject: Re: [Boost-bugs] [Boost C++ Libraries] #3605: Boost.Asio: Static Initialization causes Deadlock during WSAStartup and other problems (was: Boost.Asio: Deadlock during WSAStartup)
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2010-12-07 22:55:05


#3605: Boost.Asio: Static Initialization causes Deadlock during WSAStartup and
other problems
---------------------------------------+------------------------------------
  Reporter: ulrich.kroemer@… | Owner: chris_kohlhoff
      Type: Bugs | Status: reopened
 Milestone: Boost 1.41.0 | Component: asio
   Version: Boost 1.45.0 | Severity: Problem
Resolution: | Keywords: deadlock asio WSAStartup
---------------------------------------+------------------------------------
Changes (by jlcastillo@…):

  * status: closed => reopened
  * version: Boost 1.40.0 => Boost 1.45.0
  * resolution: wontfix =>

Comment:

 The static initialization might cause other problems appart from locking.
 If you divide your project into DLLs, and at least one of them uses Boost
 Asio, the initialization of static objects and data inside DllMain(),
 before main() is called. As you can read from Microsoft documentation:

 "Calling functions that require DLLs other than Kernel32.dll may result in
 problems that are difficult to diagnose. For example, calling User, Shell,
 and COM functions can cause access violation errors, because some
 functions load other system components. Conversely, calling functions such
 as these during termination can cause access violation errors because the
 corresponding component may already have been unloaded or uninitialized.
 If your DLL is linked with the C run-time library (CRT), the entry point
 provided by the CRT calls the constructors and destructors for global and
 static C++ objects. Therefore, these restrictions for DllMain also apply
 to constructors and destructors and any code that is called from them."

 Full article:
 http://msdn.microsoft.com/en-us/library/ms682583(VS.85).aspx

 In our case, we experienced odd behaviour in a program that could run
 without problems from the command line or as a service. In Windows XP it
 worked in all cases, while in Windows Vista
 it only worked from the command line, but not as a service. Unfortunately
 we were not able to reproduce it with a small fragment of code.

 The solution was to comment out the following line in winsock_init.hpp:

 {{{
 //static const winsock_init<>& winsock_init_instance =
 winsock_init<>(false);
 }}}
 and include the following in our own source code:

 {{{
 #include <boost/asio/detail/winsock_init.hpp>
 using namespace boost::asio::detail;
 winsock_init<> * winsock_init_instance = NULL;

 void BoostAsioInit()
 {
         if(winsock_init_instance == NULL)
                 winsock_init_instance = new winsock_init<>(false);
 }

 void BoostAsioFinish()
 {
         delete winsock_init_instance;
         winsock_init_instance = NULL;
 }
 }}}

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/3605#comment:2>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:05 UTC