15,471
社区成员
发帖
与我相关
我的任务
分享
CRITICAL_SECTION mutex;//全局
...
某个地方启动线程AThreadProc
...
AThreadProc(BYTE *buffer, DWORD size, HWND hDlg)
{
EnterCriticalSection(&mutex);//崩溃的时候都是卡在这的,Debug监视,mutex等于0x0000了!
memcpy(&bufferGlobal[iBufferGlobalEndIndex+1], buffer, size);
iBufferGlobalEndIndex += size;
int iLoop = 0;
BOOL fHasHead = FALSE, fHasTail = FALSE;
int iHeadIndex = 0, iTailIndex = 0;
for (iLoop = iBufferGlobalStartIndex; iLoop < iBufferGlobalEndIndex - 1; iLoop++)
{
if (bufferGlobal[iLoop] == 0x47 && bufferGlobal[iLoop+1] == 0x41)
{
if (fHasHead) /* Before this one is a bad BYTE array. Ignore it. */
{
iBufferGlobalStartIndex = iLoop;
}
fHasHead = TRUE;
iHeadIndex = iLoop;
}
if (bufferGlobal[iLoop] == 0x0A && bufferGlobal[iLoop+1] == 0x0D)
{
fHasTail = TRUE;
iTailIndex = iLoop;
if (fHasHead)
{
CQSCDlg::AnalyseRcvCmd(bufferGlobal + iHeadIndex, iTailIndex - iHeadIndex + 2, NULL, hDlg);
fHasHead = FALSE;
fHasTail = FALSE;
}
else
{
/* Bad BYTE array, ignore it */
fHasHead = FALSE;
fHasTail = FALSE;
iHeadIndex = 0;
iTailIndex = 0;
}
iBufferGlobalStartIndex = iLoop + 2;
if (iBufferGlobalStartIndex >= iBufferGlobalEndIndex)
{
iBufferGlobalStartIndex = 0;
iBufferGlobalEndIndex = -1;
break;
}
}
}
if (iBufferGlobalEndIndex - iBufferGlobalStartIndex > 0)
{
memcpy(bufferGlobalBackup, bufferGlobal + iBufferGlobalStartIndex, iBufferGlobalEndIndex - iBufferGlobalStartIndex + 1);
memset(bufferGlobal, 0, 10240);
memcpy(bufferGlobal, bufferGlobalBackup, iBufferGlobalEndIndex - iBufferGlobalStartIndex + 1);
iBufferGlobalStartIndex = 0;
iBufferGlobalEndIndex = iBufferGlobalEndIndex - iBufferGlobalStartIndex;
}
LeaveCriticalSection(&mutex);
return TRUE;
}