AfxSocketInit() Mem Leak? Pls. Help...

Posted on 2000-03-16
Last Modified: 2013-12-14
I have a MFC (no GUI) application. It's a NT service and it's multithreaded by using the SDK not the MFC heavy stuff.
The application is statically linked to MFC.

I have the followings for initializing:

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
      int      nRetCode = 0;
      // initialize MFC and print and error on failure
      if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
            // TODO: change error code to suit your needs
            cerr << _T("Fatal Error: MFC initialization failed") << endl;
            nRetCode = 1;

      WSADATA lpwsaData;
      if ( !AfxSocketInit( &lpwsaData ) )
            cerr << _T("Fatal Error: Could NOT Initialize Windows Sockets!") << endl;
            nRetCode = 1;

When I stop the application I get few lines of memory leaks:

[!!! Warning !!!]( 307): Detected memory leaks!
[!!! Warning !!!]( 307): Dumping objects ->
[!!! Warning !!!]( 307): sockcore.cpp(88) :
[!!! Warning !!!]( 307): {45}
[!!! Warning !!!]( 307): client block at 0x00C103A0, subtype 0, 28 bytes long.
[!!! Warning !!!]( 307): a CPtrList object at $00C103A0, 28 bytes long
[!!! Warning !!!]( 307): sockcore.cpp(86) :
[!!! Warning !!!]( 307): {44}
[!!! Warning !!!]( 307): client block at 0x00C103F0, subtype 0, 28 bytes long.
[!!! Warning !!!]( 307): a CMapPtrToPtr object at $00C103F0, 28 bytes long
[!!! Warning !!!]( 307): sockcore.cpp(84) :
[!!! Warning !!!]( 307): {43}
[!!! Warning !!!]( 307): client block at 0x00C10D40, subtype 0, 28 bytes long.
[!!! Warning !!!]( 307): a CMapPtrToPtr object at $00C10D40, 28 bytes long
[!!! Warning !!!]( 307): Object dump complete.

If I simply remove the AfxSocketInit() call all works perfect.
Why? As I see the AfxSocketInit() makes some memory leaks.
There is a question in the MSDN: Q193101 that says that the problem with multithreaded statically linked MFC applications and AfxSocketInit() has been solved in VS6SP3.
I do have VS6SP3.
What is wrong and how can I solve this ?


Corneliu I. Tusnea
Infopulse Romania

Question by:CorneliuTusnea
  • 3
  • 2

Expert Comment

ID: 2627200
I have written winsock applications as NT services in the past with no problems. However, I didn't use any mfc extensions at all -use the straight winsock api and you'll have no problem.

Author Comment

ID: 2627264
I understand you, buy I want to use CSockets :-)

Thanks anyway,

Accepted Solution

ShaunWilde earned 200 total points
ID: 2627400
you need to have a CWinApp object as this will destroy the structures for you when it terminates.

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.


Expert Comment

ID: 2627414
also have you tried a call to AfxSocketTerm() at the end - it isn't documentated but it does sit side by side with AfxSocketInit in the MFC source

Author Comment

ID: 2627422
The answer was no good, I already knew that, but the comment was excellent :-))


Expert Comment

ID: 2627425
(I have just looked deeper into the code and...)
forget AfxSocketTerm you'll have to go the CWinApp route for it to tidy up correctly.


Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question