Hi all,
I write server application (in fact it is FTP server) using MFC CSocket-derived classes (I know it is not the best approach but I didn't know it when I started).
When user wants to put file on server (upload, STOR command), my receive loop looks like this:
Of course I have to use Attach() and Detach() functions to be able to use data socket in worker thread, I just didn't write it due to simplicity.
The problem is this: sometimes, after transfer is completed (all bytes have been read) and I delete data socket object in receiveThread(), OnReceive() callback of this socket is called. Of course, at this moment, the data socket object is deleted, and thus invalid (hence it leads to some Access violations further in the code).
Why is system trying to send next data to my socket, which has already been closed and which already received all pending data?
I don't know how to get rid of it, maybe I'm not correctly finishing the transfer, but if it's true, I don't know how it should be done...
Thank you for any advice.
I write server application (in fact it is FTP server) using MFC CSocket-derived classes (I know it is not the best approach but I didn't know it when I started).
When user wants to put file on server (upload, STOR command), my receive loop looks like this:
Code:
[b]BOOL CMyDataSocket::receiveNextDataChunk()[/b]
{
INT iLen = Receive(mReceiveBuffer, mReceiveBufferSize);
if (iLen > 0)
{
try
{
pFile->Write(mReceiveBuffer, iLen);
}
catch (CFileException, e)
{
Close();
return FALSE;
}
return TRUE;
}
else
{
// We have read 0 bytes, it means no data is waiting anymore
Close();
return FALSE;
}
}
[b]void CMyDataSocket::OnReceive(INT nErrorCode)[/b]
{
AfxBeginThread(receiveThread);
}
[b]UINT receiveThread(LPVOID lParam)[/b]
{
do {
// some internal stuff here
} while (dataSocket->receiveNextDataChunk());
// transfer completed, delete socket object
delete dataSocket;
dataSocket = NULL;
}
Of course I have to use Attach() and Detach() functions to be able to use data socket in worker thread, I just didn't write it due to simplicity.
The problem is this: sometimes, after transfer is completed (all bytes have been read) and I delete data socket object in receiveThread(), OnReceive() callback of this socket is called. Of course, at this moment, the data socket object is deleted, and thus invalid (hence it leads to some Access violations further in the code).
Why is system trying to send next data to my socket, which has already been closed and which already received all pending data?
I don't know how to get rid of it, maybe I'm not correctly finishing the transfer, but if it's true, I don't know how it should be done...
Thank you for any advice.