Click to See Complete Forum and Search --> : Why does this error occur???


scorrpeio
January 2nd, 2009, 01:13 AM
I am coding to make UDP communication among computers.
I just take the local port no from the user & create the socket. But, I get the screen with
this message............
TODO: <File description> has encountered a problem and needs to close. We are sorry for the inconvenience.

The debugger does not enter in the if loop also..it goes to if statement execute it & shows that screen.

The code I used is...........

void CUDPTry2Dlg::OnCreateSocket()
{
UpdateData(TRUE);

m_sendBuffer = ""; //for async send
m_nBytesSent = 0;
m_nBytesBufferSize = 0;

if ( !UDPptr -> Create( m_LocalPort, SOCK_DGRAM ) )
{
wsprintf(m_szError, "Failed to create UDP socket: %d! Close and restart app.",
UDPptr->GetLastError());
delete UDPptr;
UDPptr = NULL;
AfxMessageBox ( m_szError );
return;
}
}

Krishnaa
January 2nd, 2009, 01:38 AM
The variable, UDPptr where is it created ???

scorrpeio
January 2nd, 2009, 02:35 AM
CAsynSocket* UDPptr;

It is created in the header file of that cpp file.
The application is dialogue based. so <name>dlg.cpp file contains that code & header file contains the declaration of that ptr.

Krishnaa
January 2nd, 2009, 02:37 AM
Are you creating it using new ? Show me the code where it is created.

scorrpeio
January 2nd, 2009, 04:17 AM
I have uploaded the corresponding files along with this reply.


public:
UINT m_LocalPort;
CAsyncSocket* UDPptr;
CString m_sendBuffer;
int m_nBytesSent;
int m_nBytesBufferSize;
TCHAR m_szError[255];
public:
afx_msg void OnCreateSocket();

void CUDPTry2Dlg::OnCreateSocket()
{
UpdateData(TRUE);

if ( UDPptr )
{
MessageBox( "Socket Already exists" );
}

else
{

/*if( (UDPptr = new CAsyncSocket(this)) == NULL )
{
MessageBox( " I can reach here too " );

}*/

m_sendBuffer = ""; //for async send
m_nBytesSent = 0;
m_nBytesBufferSize = 0;

if ( !UDPptr -> Create( m_LocalPort, SOCK_DGRAM ))
{
wsprintf(m_szError, "Failed to create UDP socket: %d! Close and restart app.", UDPptr->GetLastError());

delete UDPptr;
UDPptr = NULL;
AfxMessageBox ( m_szError );
return;
}

}

scorrpeio
January 2nd, 2009, 04:18 AM
Are you creating it using new ? Show me the code where it is created.

I have sent the header file and cpp file

scorrpeio
January 2nd, 2009, 04:34 AM
If I uncomment this......

/*if( (UDPptr = new CAsyncSocket(this)) == NULL )
{
MessageBox( " I can reach here too " );

}*/

Then I will get following error.......

error C2664: 'CAsyncSocket::CAsyncSocket(const CAsyncSocket &)' : cannot convert parameter 1 from 'CUDPTry2Dlg *const ' to 'const CAsyncSocket &'

Krishnaa
January 2nd, 2009, 06:03 AM
Do you understand what that line is ? It is creation of the OBJECT, if you dont do that, how can you expect it to work ???

So you need to create the CAsyncSocket object there, now about the error, CAsyncSocket class does not take input arguments, you should not pass in the dialog class's pointer there.

Following should work.


if( (UDPptr = new CAsyncSocket()) == NULL )
{
AfxMessageBox( " Error creating the socket object.." );

}

Ajay Vijay
January 2nd, 2009, 06:14 AM
f ( UDPptr )
{
MessageBox( "Socket Already exists" );
}

else
{

/*if( (UDPptr = new CAsyncSocket(this)) == NULL )
{
MessageBox( " I can reach here too " );

}*/This logic is incorrect. You should first assign UDPptr to null (in constructor, or somewhere else), then check if it is null and then create a new object and assign to it.
What you are doing is: checking if pointer is non-null, if so display error, else create new one.

scorrpeio
January 4th, 2009, 11:12 PM
Thank you very much guys for help.

It is now working