CRITICAL_SECTION用着用着就自己变NULL是咋回事儿?

thundersung 2013-11-14 04:19:24

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;
}


除了mutex外,所有带有global字样的变量都是全局的。
整个程序是个读写串口的程序,其中这个AThreadProc就是把每次ReadFile进来的BYTE串拼到这个bufferGlobal里然后截取特定数据头数据尾之间的内容处理。
把程序放着运行一会儿(五分钟左右,有时会更久)就崩溃来了个“不能读取0x0005”,Debug时卡死在函数第一行进入临界区那句,mutex这个时候是NULL(0x0000)。
唯一调用DeleteCriticalSection的地方就是程序退出前。而且打断点了崩溃之前也没断上。
这是怎么回事儿?
...全文
216 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
thundersung 2013-11-18
  • 打赏
  • 举报
回复
引用 5 楼 91program 的回复:
自己变,是不太可能。难道是传说中的越界操作引起的?
呃~~~貌似就是这个原因,TRACE发现我的globalBuffer已经越出去好几光年了~~~
91program 2013-11-14
  • 打赏
  • 举报
回复
自己变,是不太可能。难道是传说中的越界操作引起的?
woshi_hujunjun 2013-11-14
  • 打赏
  • 举报
回复
会不会是哪个地方没有匹配啊,enter 和 leavel ,会不会在其它地方中间执行的时候就跳出来了。
yaozhiyong110 2013-11-14
  • 打赏
  • 举报
回复
引用 2 楼 thundersung 的回复:
[quote=引用 1 楼 yaozhiyong110 的回复:] 你没有InitializeCriticalSection(&mutex);吧...
有的。 关于错误描述的不太详细,严格来说是mutex里第一个字段debug什么的是NULL。另外有个blockcount等于-4.[/quote]
引用 2 楼 thundersung 的回复:
[quote=引用 1 楼 yaozhiyong110 的回复:] 你没有InitializeCriticalSection(&mutex);吧...
有的。 关于错误描述的不太详细,严格来说是mutex里第一个字段debug什么的是NULL。另外有个blockcount等于-4.[/quote] 估计还是线程里面的逻辑处理有问题 或者是某个逻辑导致mutex被DeleteCriticalSection了...
thundersung 2013-11-14
  • 打赏
  • 举报
回复
引用 1 楼 yaozhiyong110 的回复:
你没有InitializeCriticalSection(&mutex);吧...
有的。 关于错误描述的不太详细,严格来说是mutex里第一个字段debug什么的是NULL。另外有个blockcount等于-4.
yaozhiyong110 2013-11-14
  • 打赏
  • 举报
回复
你没有InitializeCriticalSection(&mutex);吧...

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧