CSocket creation failure 
Author Message
 CSocket creation failure

I have a managed C++ project, the main project file is
very simple. I tried to create a socket object then
delete it twice. But the second time creation always
thrown an exception as:

An unhandled exception of
type 'System.Runtime.InteropServices.SEHException'
occurred in SocketTest.exe
Additional information: External component has thrown an
exception.

And the break point is in the MFC file "wingdi.cpp",
function:

// resource failure
void AFXAPI AfxThrowResourceException()
{
        THROW((CResourceException*)
&_simpleResourceException);

Quote:
}

My main function is as simple as:

// This is the entry point for this application
int _tmain(void)
{
  // TODO: Please replace the sample code below with your
own.
    Console::WriteLine(S"Hello World");

  // TEST SOCKET
  if(!AfxSocketInit())
    AfxMessageBox("Failed to Initialize Sockets",MB_OK|
MB_ICONSTOP);

    CSocket* socketObj = new CSocket();
    if (!socketObj->Create())
    {
        MessageBox(NULL, "Create failed.........", NULL,
MB_OK);
    }
    if (!socketObj->Connect(ipAddress, port))
    {
        MessageBox(NULL, "Connection failed.........",
NULL, MB_OK);
    }
    delete socketObj;
    socketObj = NULL;

    CSocket* socketObj1 = new CSocket();
    if (!socketObj1->Create())       // THIS ONE THROWN AN
EXCEPTION
    {
        MessageBox(NULL, "Create failed.........", NULL,
MB_OK);
    }
    if (!socketObj1->Connect(ipAddress, port))
    {
        MessageBox(NULL, "Connection failed.........",
NULL, MB_OK);
    }
    delete socketObj1;

    return 0;

Quote:
}



Sat, 20 Aug 2005 22:15:14 GMT  
 CSocket creation failure
| Content-Class: urn:content-classes:message

| Subject: CSocket creation failure
| Date: Tue, 4 Mar 2003 06:15:14 -0800
|
| I have a managed C++ project, the main project file is
| very simple. I tried to create a socket object then
| delete it twice. But the second time creation always
| thrown an exception as:

Hi,

I was able to take the sample code, build it with minimal modification
(declaring and initializing variables 'ipAddress' and 'port) and reproduce
the behavior you describe.

I believe this is occuring because your console app lacks a call to
AfxWinInit.  For a console application, which does not use the MFC-supplied
WinMain function, you must call AfxWinInit directly to initialize MFC.

For more information, see :
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcli...
_mfc_afxwininit.asp

BTW -- a debug version of the original sample code (without AfxWinInit)
will assert from AfxGetInstanceHandle.  These asserts can be extremely
helpful in determining that there is a problem in an application.

--
Andrew Brown, Visual C++ Team
This posting is provided AS IS with no warranties, and confers no rights.

--------------------
|
| An unhandled exception of
| type 'System.Runtime.InteropServices.SEHException'
| occurred in SocketTest.exe
| Additional information: External component has thrown an
| exception.
|
| And the break point is in the MFC file "wingdi.cpp",
| function:
|
| // resource failure
| void AFXAPI AfxThrowResourceException()
| {
|       THROW((CResourceException*)
| &_simpleResourceException);
| }
|
|
| My main function is as simple as:
|
| // This is the entry point for this application
| int _tmain(void)
| {
|   // TODO: Please replace the sample code below with your
| own.
|     Console::WriteLine(S"Hello World");
|
|   // TEST SOCKET
|   if(!AfxSocketInit())
|     AfxMessageBox("Failed to Initialize Sockets",MB_OK|
| MB_ICONSTOP);
|
|     CSocket* socketObj = new CSocket();
|     if (!socketObj->Create())
|     {
|       MessageBox(NULL, "Create failed.........", NULL,
| MB_OK);
|     }
|     if (!socketObj->Connect(ipAddress, port))
|     {
|       MessageBox(NULL, "Connection failed.........",
| NULL, MB_OK);
|     }
|     delete socketObj;
|     socketObj = NULL;
|
|     CSocket* socketObj1 = new CSocket();
|     if (!socketObj1->Create())     // THIS ONE THROWN AN
| EXCEPTION
|     {
|       MessageBox(NULL, "Create failed.........", NULL,
| MB_OK);
|     }
|     if (!socketObj1->Connect(ipAddress, port))
|     {
|       MessageBox(NULL, "Connection failed.........",
| NULL, MB_OK);
|     }
|     delete socketObj1;
|
|     return 0;
| }
|
|



Mon, 22 Aug 2005 02:59:36 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Document creation failure.

2. Palette creation failure....................

3. Creation failure with CImageList.....What the...???

4. CBitmap creation failure

5. Document creation failure.

6. Palette creation failure....................

7. WebBrowser Creation Failure!

8. Failure in Creating CSocket

9. Debug Assertion Failure using CSocket

10. Failure in Creating CSocket

11. Socket NOtification Sink Failure with CSocket

12. CSocket in Dll causes an assertion failure

 

 
Powered by phpBB® Forum Software