views:

182

answers:

2

My program has one dialog and two sockets. Both sockets are derived from CAsyncSocket, one is for listening, other is for receiving data from client. My program crashes when client tries to connect to server application and server needs to initialize receiving socket.

This is my MFC dialog class.

class CFileTransferServerDlg : public CDialog
{
   ...
   ListeningSocket ListenSock;
   ReceivingSocket* RecvSock;
   void OnAccept(); // called when ListenSock gets connection attempt
   ...
};

This is my derived socket class for receiving data that calls parent dialogs method when event is signaled.

class ReceivingSocket : public CAsyncSocket
{
   ...
   CFileTransferServerDlg* m_pDlg; // for accessing parent dialogs controls
   virtual void OnReceive(int nErrorCode);
   ...
}

ReceivingSocket::ReceivingSocket()
{
}

This is dialogs function that handles incoming connection attempt when listening socket gets event notification. This is where the crash happens.

void CFileTransferServerDlg::OnAccept()
{   
   RecvSock = new ReceivingSocket;   /* CRASH */
}

OR

void CFileTransferServerDlg::OnAccept()
{   
   ReceivingSocket* tmpSock = new ReceivingSocket;
   tmpSock->SetParentDlg(this);

   CString message;
   if( ListenSock.Accept(*tmpSock) )  /* CRASH */
   {
      message.LoadStringW(IDS_CLIENT_CONNECTED);
      m_txtStatus.SetWindowTextW(message);
      RecvSock = tmpSock;
   }
}

My program crashes when I try to create a socket for receiving file sent from client application. OnAccept starts when Listening socket signals incoming connection attempt, but my application then crashes. What could be wrong? Error in debug mode:

Unhandled exception at 0x009c30e1 in FileTransferServer.exe: 0xC0000005: Access violation reading location 0xccccce58.

UPDATE:

I edited code a little and I've found that inside sockcore.cpp where Accept is defined, program failes on this line of code:

ASSERT(rConnectedSocket.m_hSocket == INVALID_SOCKET);

I don't understand how that can happen. ReceivingSocket class is somehow not getting constructed right. I derive it from CAsyncSock, leave constructor empty, and no matter where I create it, on stack or on heap, it always crashes.

Here is complete project, both client and server, if anyone can take a look at it I would be really grateful. I apologize for the comments, they are in Croatian.

Visual Studio project

A: 

Maybe check to see if you've inherited ReceivingSocket correctly?

Check this out.

John at CashCommons
Yes, I've checked. It is dialog application, socket api is initialized properly and inheritance is valid. Most intriguing thing is that on another computer this application works fine. Exception that is thrown indicates that it's some kind of pointer init error, but I have no clue what could cause it.
usermeister
Correction, it doesn't work fine on other computer, person who was testing it didn't do it right.
usermeister
A: 

I've looked into your code. The issue seems to be that you never call ListeningSocket::SetParentDlg(CFileTransferServerDlg* parent). Since you also do not initialize the m_pDlg pointer in the ListeningSocket constructor, it has random values and the program might crash here and there when you access this pointer. (I had also a crash but slightly at another location than you pointed out.)

I've changed it this way:

In ListeningSocket.h changed the constructor:

ListeningSocket(CFileTransferServerDlg* parent);

Also in ListeningSocket.cpp:

ListeningSocket::ListeningSocket(CFileTransferServerDlg* parent)
   : m_pDlg(parent)
{
}

Constructor of CFileTransferServerDlg changed this way:

CFileTransferServerDlg::CFileTransferServerDlg(CWnd* pParent /*=NULL*/)
   : CDialog(CFileTransferServerDlg::IDD, pParent),
     ListenSock(this)
{
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

Crash disappeared. Other ways are possible of course.

Really nice little programs, by the way :) I'll delete them of course now since I can't probably afford the license fees :)

Slauma
THANK YOU!!! And thanks for the compliments, it's just something I did for practice. I've mainly done C# for the past few months, and came back to this. For a beginner, it's hard to do a context switch easily. Thanks again for saving my nerves! If anything from the code helps anyone, feel free to use it, I'll be glad :)
usermeister