Home > Software engineering >  The new memory for help
The new memory for help

Time:10-11

MFC to write a message-based asynchronous socket communication, the problem of the code is at the time of receiving the file, the sender sends in the form of the while loop, the receiver in OnSocket message received, upon receiving the second with the file content of datagram happened out of memory, I through the way of writing log to track is a New unsuccessful quote error, posted the code now, and attach run log, please help to analysis the reason,
 
Void CCallDlg: : OnSock (WPARAM WPARAM, LPARAM LPARAM)
{
DWORD dwMaxNum=0;
Int curPackSize=0;//the current datagram size
Cstrings sExtra=", "mQSerial=" ", mQueues="", mFilePath=" ", sCurLog="";
The switch (LOWORD (lParam))
{
Case FD_READ:
{
SCurLog="1. Check the socket word register of the number of bytes can be read! The \ r \ n ";
OnWriteLog (sCurLog);

: : ioctlsocket (mCallSocket FIONREAD, & amp; DwMaxNum);//

SCurLog. Format (" 2. Register port found % d bytes can be read data, and begin to apply for heap memory space! \ r \ n ", dwMaxNum);
OnWriteLog (sCurLog);

Char * pBuffer=NULL;
PBuffer=new char [dwMaxNum];//define a buffer
if(! PBuffer)
{
SCurLog="2-1. The heap memory array pBuffer application failed! The \ r \ n ";
OnWriteLog (sCurLog);
}
Char * m_pchBuffer=NULL;
Memset (pBuffer, 0, dwMaxNum);

SCurLog="3. Data test case in the heap memory array pBuffer successfully! The \ r \ n ";
OnWriteLog (sCurLog);

CPackage mPacMsg;
WSABUF WSABUF;

Wsabuf. Buf=pBuffer;
DwMaxNum wsabuf. Len=(unsigned long);

DWORD dwRead=0;
DWORD dwFlag=0;
SOCKADDR_IN addrFrom;//receive arrival address information
Int len=sizeof (SOCKADDR);

SCurLog="4. Begin to port to read data from the socket word! The \ r \ n ";
OnWriteLog (sCurLog);

If (SOCKET_ERROR==WSARecvFrom (mCallSocket, & amp; Wsabuf, 1, & amp; DwRead, & amp; DwFlag,
(SOCKADDR *) & amp; AddrFrom, & amp; Len, NULL, NULL))
{
//DWORD error=WSAGetLastError ();
//WSASendError (error);
MessageBox (" failed to get the data from the server, please restart or contact related department to solve!" );
return;
}//receive the data first and then classify the
//MessageBox (" receive a package!" );
SCurLog. Format (" 5. Port to read data from the socket word! Total to read % d bytes! \ r \ n ", dwRead);
OnWriteLog (sCurLog);

Memcpy (& amp; MPacMsg, wsabuf buf, sizeof (mPacMsg));//start from pBuffer test for sizeof (Msg) bytes to & amp; Msg address
CurPackSize=mPacMsg. M_dwDatalen;//record The Times of the datagram size

SCurLog="6. Packets get to complete! The \ r \ n ";
OnWriteLog (sCurLog);

If (mPacMsg m_nMsgType==MT_SENDDATA)
{
SCurLog="7. Began to obtain complete data report! The \ r \ n ";
OnWriteLog (sCurLog);
M_pchBuffer=new char [mPacMsg m_dwDatalen];
Memset (m_pchBuffer, 0, mPacMsg m_dwDatalen);
Memcpy (m_pchBuffer, wsabuf buf, dwMaxNum);//dwMaxNum consistent with dwDatalen size should be
SCurLog="8. A datagram to get finished, and begins to analyze! The \ r \ n ";
OnWriteLog (sCurLog);
The switch (mPacMsg m_nSubType)
{
Case ST_FILE://audio files
{
If (m_bFirst | | mPacMsg. M_nFlag==1)//for the first time to receive a file
{
M_bFirst=false;
MFilePath=". \ \ voice \ \ test. TXT ".
//mFilePath. The Format (". \ \ s \ \ % s. av ", CPublicData: : mDeptName);
M_File. Open (mFilePath, CFile: : modeCreate | CFile: : modeReadWrite);
Char * pchTmp=m_pchBuffer + sizeof (CPackage);
M_File. WriteHuge (pchTmp curPackSize - sizeof (CPackage));
PchTmp=NULL;

If (mPacMsg m_dwFileLen==curPackSize - sizeof (CPackage))
{
M_File. Close ();
M_bFirst=TRUE;
}
}
Else if (mPacMsg m_nFlag==0)//in the process of file transfer
{
Char * pTmp=m_pchBuffer + sizeof (CPackage);
M_File. WriteHuge (pTmp curPackSize - sizeof (CPackage));
PTmp=NULL;
}
Else if (mPacMsg m_nFlag==1)//end of file transfer
{
Char * pTmp=m_pchBuffer + sizeof (CPackage);
M_File. WriteHuge (pTmp curPackSize - sizeof (CPackage));
M_bFirst=TRUE;
M_File. Close ();
PTmp=NULL;
}
}
break;
Case ST_IMAGE://picture file
{
}
break;
}
SCurLog="9. Release the heap memory: pBuffer and m_pchBuffer! The \ r \ n ";
OnWriteLog (sCurLog);
The delete [] pBuffer;
PBuffer=NULL;

The delete [] m_pchBuffer;
M_pchBuffer=NULL;

return;
}
Else//MT_SENDDATA message should be the first test, pBuffer don't need to be used in other news, the first after the judgment, if the datagram is not MT_SENDDATA news, which can timely release pBuffer heap memory
{
SCurLog="10. Release the heap memory: pBuffer! The \ r \ n ";
OnWriteLog (sCurLog);
The delete [] pBuffer;
PBuffer=NULL;
}//EndIF

.
Omit part of the code
.
}
}
}


OnWriteLog (sCurLog); For a running log function, now stick run log out
1. Check the socket word register can be read number of bytes!
2. Register port found 8192 bytes can be read data, and begin to apply for heap memory space!
3. The data test case in the heap memory array pBuffer successfully!
4. Start port to read data from the socket word!
5. Port to read data from the socket word! Read 6280 bytes!
6. Packets get out!
7. Began to obtain complete data report!
8. A datagram to get finished, and begins to analyze!
9. The release of heap memory: pBuffer and m_pchBuffer!
1. Check the socket word register can be read number of bytes!
2. Register port found 6280 bytes can be read data, and begin to apply for heap memory space!

As can be seen from the log, receiving a second time, the data in the log 2 "insufficient memory" collapsed, seen from a program is in pBuffer=new char [dwMaxNum];//define a buffer, without success,
Don't understand, the first execution time, I have a normal after new release, why the second time when receiving application again went out of memory?

CodePudding user response:

Among task manager to see memory growth situation, the program has multiple return may cause memory leaks,
Recommend put buffer pointer in the class, convenient management, an application used multiple times

CodePudding user response:

reference 1st floor zgl7903 response:
see memory growth situation, the task manager program have multiple return may cause memory leaks,
Recommend put buffer pointer in the class, convenient management, an application used multiple times

Strange look from the task manager, what memory did not increase, because didn't receive the second package is out of memory, an application used multiple times not afraid oh, because every time I receive datagrams may not be the same size,

CodePudding user response:

See dwMaxNum when new application memory how much, is over

CodePudding user response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related