高分求救,程序出现错误,wincore.cpp debug Assertion Failed 991,问题找不到在哪?
我做了GPS校时程序。需要用到线程来监听GPS时间。通过this将类成员传递给线程ThreadProc,ThreadProc通过CGPSadjustDlg *p=(CGPSadjustDlg*)lparam来接收;在ThreadProc中访问类成员函数Analyze(LPCSTR m_strReceived)对GPS数据格式进行解析。问题出来了,程序运行一段后就崩溃。出现ASSERTION DEBUG 提示是wincore.cpp 991出现错误,。初步判定是出现内存泄露。可我在程序中,调用指针,就在ThreadProc中声明一个指针来进行对类成员的调用,并没有分配内存。为什么会出现这样的错误呢?在线程中对类成员函数Analyze(LPCSTR m_strreceived)调用时,有对全局变量,数组等操作。并没有指针,分配内存等。如果有指针,分配内存,我可以释放。可我找几天了,并没有找到原因所在。
请教高手。
void CGPSadjustDlg::OnOpen()
{
AfxBeginThread(ThreadProc2,(LPVOID)this); //调用线程
}
static UINT ThreadProc2(LPVOID lparam)
{
CGPSadjustDlg *p=(CGPSadjustDlg*)lparam;
LPCSTR m_lpcstr_strReceived=NULL;
int nLength;
nLength=0;
CString m_UpdataReceived;
while (TRUE)
{
m_UpdataReceived=p->m_strReceived;
nLength=m_UpdataReceived.GetLength();
m_lpcstr_strReceived=m_UpdataReceived.GetBuffer(nLength);
m_UpdataReceived.ReleaseBuffer();
p->m_GPSDISPFlag=TRUE;
p->m_TimeMonitor=TRUE;
p->Analyze_Data(m_lpcstr_strReceived);
p->m_strReceived="";
p->m_ReceiveGPStimeFlag=TRUE;
Sleep(1000);
}
delete p;
p=NULL;
return 0;
}
//分析GPS数据
void CGPSadjustDlg::Analyze_Data(LPCSTR m_lpcstr_ReceiveGPS)
{
int iw=0, pos=0;
CString szArr[12],pszstring;
int i=0;
CString m_ReceiveGPS;
m_ReceiveGPS.Format("%s",m_lpcstr_ReceiveGPS);
//TRACE(m_ReceiveGPS);
//先判断是否接收到数据
if (m_ReceiveGPS.IsEmpty())
{ TRACE("the m_ReceiveGPS is empty");
return;
}
// AfxMessageBox(m_ReceiveGPS);
//若字符串不是以'$'开头的,必须删掉这部分不完整的
if (m_ReceiveGPS[0] != '$')
{
i = m_ReceiveGPS.Find('\n', 0);
//尾部未接收完整,必须等接收完后才能删除
if(i == -1)
return;
//尾部已接收完整(尾部为\r\n结束),删除不完整的部分
m_ReceiveGPS.Delete(0, i+1);
}
//判断GPS接收缓冲区是否存在$GPRMC"
if ( strstr(m_ReceiveGPS,"$GPRMC") && strstr(m_ReceiveGPS,"\r\n"))
{
//如果找到$GPRMC",则进行截位操作
pos=m_ReceiveGPS.Find( "$GPRMC" );
m_ReceiveGPS=m_ReceiveGPS.Right(m_ReceiveGPS.GetLength()-(pos));
if (m_ReceiveGPS.GetLength()<= 60)
return;
//将POS置为回车换行符
pos=m_ReceiveGPS.Find( "\r\n" );
//取最右边的值
m_ReceiveGPS=m_ReceiveGPS.Left( pos );
}
else
return;
pszstring=m_ReceiveGPS;
//断言,提示GPS接收缓冲区状态
TRACE( "m_ReceiveGPS State = %s\n",m_ReceiveGPS );
//判断是否存在“,”
while ( pos = pszstring.Find(",") )
{
//将字符串pszstring“,”后的值传递
szArr[iw] = pszstring.Left( pos );
//截取pszstring
pszstring = pszstring.Right( pszstring.GetLength() - (pos+1) );
iw++;
}
//判定是否定位
CString m_status;
CString m_Ostatus;
m_Ostatus=szArr[2];
m_Ostatus.TrimLeft();
m_Ostatus.TrimRight();
m_status="定位状态:";
if (m_Ostatus=="V")
{
m_status+="未定位";
}
else
if (m_Ostatus=="A")
{
m_status+="定位";
m_AVStatus=TRUE;
//AfxMessageBox("定位");
}
m_OriennationStatus.SetWindowText((LPCTSTR)m_status);
//分析数组中的数据
/*-analyze date------------------------------------------*/
lpIDatetime.xday = atoi(szArr[9].Mid(0,2));
lpIDatetime.xmonth = atoi(szArr[9].Mid(2,2));
lpIDatetime.xyear = atoi(szArr[9].Mid(4,2));
/*-analyze time------------------------------------------*/
lpIDatetime.xhour = atoi(szArr[1].Mid(0,2))+8;
lpIDatetime.xminute = atoi(szArr[1].Mid(2,2));
lpIDatetime.xsecond = atoi(szArr[1].Mid(4,2));
lpIDatetime.GPSyear.Format("%.2d",lpIDatetime.xyear);
lpIDatetime.GPSmonth.Format("%.2d",lpIDatetime.xmonth);
lpIDatetime.GPSday.Format("%.2d",lpIDatetime.xday);
lpIDatetime.GPShour.Format("%.2d",lpIDatetime.xhour);
lpIDatetime.GPSminute.Format("%.2d",lpIDatetime.xminute);
lpIDatetime.GPSsecond.Format("%.2d",lpIDatetime.xsecond);
//数据格式转换
m_pszDT.Format( "20%.2d-%.2d-%.2d %.2d:%.2d:%.2d\r\n",
lpIDatetime.xyear,
lpIDatetime.xmonth,
lpIDatetime.xday,
lpIDatetime.xhour,
lpIDatetime.xminute,
lpIDatetime.xsecond );
m_pszDT="GPS时间:"+m_pszDT;
/*m_LDateTime.SetWindowText( pszDT );
m_LEW.SetWindowText( szArr[five] );
m_LNS.SetWindowText( szArr[three] ); */
// AfxMessageBox(m_pszDT);
//将指定的GPS数据时间显示在编辑框中
int nLen=m_CtrDisplay.GetWindowTextLength();
if (!m_TimeMonitor)
{
m_CtrDisplay.SetSel(nLen, nLen);
m_CtrDisplay.ReplaceSel(m_pszDT);
nLen+=m_str.GetLength();
m_Display+=m_str;
}
m_TimeMonitor=TRUE;
if (m_GPSDISPFlag)
{
m_displaygps=m_pszDT;
m_ctrDisplagGPS.SetWindowText((LPCTSTR)m_displaygps);
}
m_GPSDISPFlag=FALSE;
//获取当前系统时间
SYSTEMTIME lptime;
GetSystemTime( &lptime );
lptime.wYear = (lpIDatetime.xyear+2000);
lptime.wMonth = lpIDatetime.xmonth;
lptime.wDay = lpIDatetime.xday;
lptime.wHour = lpIDatetime.xhour-8;
lptime.wMinute = lpIDatetime.xminute;
lptime.wSecond = lpIDatetime.xsecond;
SetSystemTime( &lptime );
m_strReceived="";
m_ReceiveGPS="";
m_ReceiveGPStimeFlag=TRUE;
PurgeComm(m_Port.m_hComm,PURGE_TXCLEAR|PURGE_TXABORT);
//判断是否存在“$GPRMC"
if ( szArr[0].CollateNoCase( "$GPRMC" ) )
return;
//判断是否存在V
if ( szArr[2].CollateNoCase( "V" ) )
return;
}