Run-Time Check Failure #2 - Stack around the variable 'm_osWrite' was corrupted

thundersung 2014-02-25 02:38:49
以下是菜鸟的向串口写字符串的函数

void ProcessSndBuffer(BYTE *buffer, DWORD size, int comIndex)
{
try
{
OVERLAPPED m_osWrite;
memset(&m_osWrite, 0, sizeof(m_osWrite));
m_osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
COMSTAT ComStat;
DWORD dwErrorFlags, dwBytesWritten, dwRes;
BOOL bWriteStat;
ClearCommError(hComArray[comIndex], &dwErrorFlags, &ComStat);
bWriteStat = WriteFile(hComArray[comIndex], buffer, size, &dwBytesWritten, &m_osWrite);
if(!bWriteStat)
{
DWORD dwLastError = GetLastError();
if(GetLastError() == ERROR_IO_PENDING)
{
dwRes = WaitForSingleObject(m_osWrite.hEvent, 1000);
if (dwRes == WAIT_OBJECT_0)
{
while(!GetOverlappedResult(hComArray[comIndex], &m_osWrite, &dwBytesWritten, TRUE))
{
DWORD dwError = GetLastError();
if (dwError == ERROR_IO_INCOMPLETE)
continue;
}
}
}
}
CloseHandle(m_osWrite.hEvent);
}
catch (...)
{
}
}


然后运行一会儿就报错退出:Run-Time Check Failure #2 - Stack around the variable 'm_osWrite' was corrupted
好吧,引起这个错误的可能是别的地方内存操作越界引起的,但是为虾米每次俺运行都是这里错呢?
另外俺把memset(&m_osWrite, 0, sizeof(m_osWrite));这句注释了它貌似就不报错了。。。只不过这样WriteFile会返回LastError参数错误。。。
...全文
192 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaohuh421 2014-02-26
  • 打赏
  • 举报
回复
我觉得你的问题可能根本不在这个函数中. 你把下面这些代码先注释起来. 再一步一步打开排除. ClearCommError(hComArray[comIndex], &dwErrorFlags, &ComStat); bWriteStat = WriteFile(hComArray[comIndex], buffer, size, &dwBytesWritten, &m_osWrite); if(!bWriteStat) { DWORD dwLastError = GetLastError(); if(GetLastError() == ERROR_IO_PENDING) { dwRes = WaitForSingleObject(m_osWrite.hEvent, 1000); if (dwRes == WAIT_OBJECT_0) { while(!GetOverlappedResult(hComArray[comIndex], &m_osWrite, &dwBytesWritten, TRUE)) { DWORD dwError = GetLastError(); if (dwError == ERROR_IO_INCOMPLETE) continue; } } } } CloseHandle(m_osWrite.hEvent);
thundersung 2014-02-26
  • 打赏
  • 举报
回复
问题大概找到了,另外一处定期写日志的函数,没有SeekToEnd就WriteString,把这句加上,目前程序还没挂。
thundersung 2014-02-26
  • 打赏
  • 举报
回复
别沉啊!!!
thundersung 2014-02-26
  • 打赏
  • 举报
回复
在所有尝试之前,今天关机重启后没修改啥代码就不死了。 但是。。。exe拿到另外一台机器上就死了。。。艹!!!
oyljerry 2014-02-25
  • 打赏
  • 举报
回复
ClearCommError(hComArray[comIndex], &dwErrorFlags, &ComStat); 会不会有越界等操作
thundersung 2014-02-25
  • 打赏
  • 举报
回复

void ProcessSndBuffer(BYTE *buffer, DWORD size, int comIndex)
{
	try
	{
		OVERLAPPED m_osWrite;
#if 1
		m_osWrite.hEvent = 0;
		m_osWrite.Internal = 0;
		m_osWrite.InternalHigh = 0;
		m_osWrite.Offset = 0;
		m_osWrite.OffsetHigh = 0;
#endif
		COMSTAT ComStat;
		DWORD dwErrorFlags, dwBytesWritten, dwRes;
		BOOL bWriteStat;
		//memset(&m_osWrite, 0, sizeof(m_osWrite));
		m_osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
		ClearCommError(hComArray[comIndex], &dwErrorFlags, &ComStat);
		bWriteStat = WriteFile(hComArray[comIndex], buffer, size, &dwBytesWritten, &m_osWrite);
		if(!bWriteStat)
		{
			DWORD dwLastError = GetLastError();
			if(GetLastError() == ERROR_IO_PENDING)
			{
				dwRes = WaitForSingleObject(m_osWrite.hEvent, 1000);
				if (dwRes == WAIT_OBJECT_0)
				{
					while(!GetOverlappedResult(hComArray[comIndex], &m_osWrite, &dwBytesWritten, TRUE))
					{
						DWORD dwError = GetLastError();
						if (dwError == ERROR_IO_INCOMPLETE)
							continue;
					}
				}
			}
		}
		CloseHandle(m_osWrite.hEvent);
	}
	catch (...)
	{
	}
}
不用memset,俺纯粹一个一个成员赋0,效果应该和memset一样了吧? 然后。。。就悲剧了!还是这个错误。。。
thundersung 2014-02-25
  • 打赏
  • 举报
回复
引用 1 楼 VisualEleven 的回复:
只不过这样WriteFile会返回LastError参数错误。。。 ----------------------------------------------- ??? 你的意思是这个 bWriteStat = WriteFile(hComArray[comIndex], buffer, size, &dwBytesWritten, &m_osWrite); 调用失败?GetLastError返回的错误是参数错误??
嗯,我用VS自带的错误查找工具是这么说的。 当时DEBUG发现如果不memset的话里面除了hEvent全都是0xcccccccc
Eleven 2014-02-25
  • 打赏
  • 举报
回复
只不过这样WriteFile会返回LastError参数错误。。。 ----------------------------------------------- ??? 你的意思是这个 bWriteStat = WriteFile(hComArray[comIndex], buffer, size, &dwBytesWritten, &m_osWrite); 调用失败?GetLastError返回的错误是参数错误??

2,644

社区成员

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

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