PRB: Issue Specific to Using Windows Sockets from a DLL
ID: Q237572
|
This article discusses a Beta release of a Microsoft product. The
information in this article is provided as-is and is subject to change
without notice.
No formal product support is available from Microsoft for this Beta
product. For information about obtaining support for a Beta release,
please see the documentation included with the Beta product files, or
check the Web location from which you downloaded the release.
The information in this article applies to:
-
Microsoft Windows 95
-
Microsoft Windows 98
-
Microsoft Windows NT, versions 4.0, 4.0 SP4
SYMPTOMS
When WSAStartup name-resolution functions such as gethostbyname and WSACleanup are called in a loop on Windows 9x or Windows NT 4.0, there is a small memory leak in each iteration.
CAUSE
The Hostent structure that is allocated by the Winsock name-space provider is not freed after WSACleanup is called, because the memory is allocated on a per-thread basis and is freed only when the thread exits. If WSAStartup is called again before the thread exits, another Hostent structure is allocated.
RESOLUTION
You rarely need to call WSAStartup and WSACleanup more than once in a application; however, it may be necessary to call WSAStartup and WSACleanup multiple times when writing a DLL where the developer chooses to wrap the entire Winsock call sequence within a DLL.
The most straight-forward way to trigger calling WSAStartup and WSACleanup in the intermediate DLL is to include procedures in the intermediate DLL interface to initiate and conclude use of the DLL.
If it is not possible to determine the first call from the application to the intermediate DLL, then calling WSAStartup and WSACleanup is more difficult. The scenario becomes much more complex if there are multiple threads in the DLL and each thread is loading a Winsock DLL by calling WSAStartup.
One possible workaround is to use the synchronization objects in a manner similar to mutex objects to synchronize the calls between the DLLs.
Please note that calling WSAStartup and WSACleanup in DLL_PROCESS_ATTACH and DLL_PROCESS_DETACH is not recommended, because this method can have an adverse affect on the application, giving rise to thread deadlocking.
Please refer to the Platform SDK documentation on DllMain. For additional information, please click the article number below
to view the article in the Microsoft Knowledge Base:
Q214784 PRB: FreeLibrary() Fails When Called within DLL_PROCESS_DETACH
STATUS
This behavior is by design.
The problem has been fixed in Windows 2000. The memory for Winsock name resolution is allocated on a per-thread basis. It is freed when the thread exits. In the case where WSAStartup and WSACleanup are called repeatedly without exiting the thread, new memory won't be allocated at additional WSAStartup calls.
MORE INFORMATION
Steps to Reproduce Behavior
- While (TRUE).
- Call WSAStartup.
- Call gethostbyname.
- Call WSACleanup.
- Use System monitor and monitor the memory count.
REFERENCES
Additional query words:
Keywords : kbDLL kbNTOS400 kbWinOS98 kbWinOS98bug kbWinsock kbDSupport
Version : WINDOWS:95; winnt:4.0,4.0 SP4
Platform : WINDOWS winnt
Issue type : kbprb