Solved

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

Posted on 2000-03-16
6
508 Views
Last Modified: 2013-12-14
Hi,
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 ?

Thanks,
Corneliu

Corneliu I. Tusnea
Infopulse Romania

0
Comment
Question by:CorneliuTusnea
  • 3
  • 2
6 Comments
 

Expert Comment

by:wardkd
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.
0
 

Author Comment

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

Thanks anyway,
Corneliu.
0
 
LVL 9

Accepted Solution

by:
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.

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Expert Comment

by:ShaunWilde
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
0
 

Author Comment

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

Thanks.
0
 
LVL 9

Expert Comment

by:ShaunWilde
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.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
debug as  junit test 4 73
Eclipse. How to move up/down source folders 4 78
Arduino EDI - Programming language 3 80
show out valin of json for debugging in visaul c++ 1 17
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now