Using the Networking Package in Global Objects
The technique described in
Initializing and Cleaning Up does not work if you use the Networking package in global objects. For example, if the program contains a global connection to a server, as shown below:
RWSocketPortal globalPortal(RWInetAddr(7932,"net.roguewave.com"));
then the constructor for this global object is executed before the constructor for the
RWWinSockInfo in
main() or
WinMain(). One solution is to add a static global
RWWinSockInfo object before the definition of
globalPortal, as shown below:
static RWWinSockInfo info; //1
RWSocketPortal globalPortal(RWInetAddr(7932,"net.roguewave.com"));
The
RWWinSockInfo object defined in line
//1 is guaranteed to be constructed before the portal is defined in the next line. It is not destroyed until after the portal is destroyed.
NOTE: The
RWWinSockInfo object is not guaranteed to be constructed first if the globals are in different modules.
Your application can nest
RWWinSockInfo objects. The first call to the constructor initializes the Winsock DLL, and the last call to the destructor cleans up the Winsock DLL.