程序连续运转三天过后,在debug版本下出现如下提示:
Debug Assertion Failed
Progtam:c:\pepoleorientation\pd.exe
File:e:\Program files\microsoft viusal studio.net 2003\vc7\atlmfc\include\atlsimpstr.h
Line:778
Expression:pOldData->nAllocLength<nLength
For information on how you program can cause an assertion
failue,see the visual c++ documentation on asserts
(Press Retry to debug the application)
终止 重试 忽略
--------------------------------------------
我看了提示文件的源码
ATL_NOINLINE void Reallocate( int nLength )
{
CStringData* pOldData = GetData();
ATLASSERT( pOldData->nAllocLength < nLength );////这里是778行
IAtlStringMgr* pStringMgr = pOldData->pStringMgr;
CStringData* pNewData = pStringMgr->Reallocate( pOldData, nLength, sizeof( XCHAR ) );
if( pNewData == NULL )
{
ThrowMemoryException();
}
Attach( pNewData );
}
---------------------------------------
ATLASSERT( pOldData->nAllocLength < nLength );////这里是778行
请问大家可能造成这个错误的常见操作是那些?举个示例.推荐个标准写法.
Debug Assertion Failed
Progtam:c:\pepoleorientation\pd.exe
File:e:\Program files\microsoft viusal studio.net 2003\vc7\atlmfc\include\atlsimpstr.h
Line:778
Expression:pOldData->nAllocLength<nLength
For information on how you program can cause an assertion
failue,see the visual c++ documentation on asserts
(Press Retry to debug the application)
终止 重试 忽略
--------------------------------------------
我看了提示文件的源码
ATL_NOINLINE void Reallocate( int nLength )
{
CStringData* pOldData = GetData();
ATLASSERT( pOldData->nAllocLength < nLength );////这里是778行
IAtlStringMgr* pStringMgr = pOldData->pStringMgr;
CStringData* pNewData = pStringMgr->Reallocate( pOldData, nLength, sizeof( XCHAR ) );
if( pNewData == NULL )
{
ThrowMemoryException();
}
Attach( pNewData );
}
---------------------------------------
ATLASSERT( pOldData->nAllocLength < nLength );////这里是778行
请问大家可能造成这个错误的常见操作是那些?举个示例.推荐个标准写法.
CString也用了很多
根据我的经验,CString最好少用,尤其是在一小段程序循环创建一个CString局部变量容易出问题,如果有这种情况最好是将其提出来定义一个变量,或者用其他类型代替。
--------------------------------------------------------------------------
你说的着情况在我程序中存在,我在循环中有循环创建的这种
还有对CString的指针操作问题
::GetPrivateProfileString("Connect","UserName","smilefox",m_strUserName.GetBuffer(50),50,".\\InitConfig.Ini");
::GetPrivateProfileString("Connect","PassWord","xqw8089",m_strPassWord.GetBuffer(50),50,".\\InitConfig.Ini");
::GetPrivateProfileString("Connect","Database","PepoleOrientation",m_strDBName.GetBuffer(50),50,".\\InitConfig.Ini");
::GetPrivateProfileString("Connect","BKDataBase","BKPepoleOrientation",m_strBKDB.GetBuffer(50),50,".\\InitConfig.Ini");
::GetPrivateProfileString("Connect","BKHostIP","",m_strBKHostIP.GetBuffer(50),50,".\\InitConfig.Ini");
m_iDelayTime=::GetPrivateProfileInt("Connect","DelayTime",10,".\\InitConfig.Ini");
m_iConnFlag=::GetPrivateProfileInt("Connect","ConnFlag",0,".\\InitConfig.Ini");
if(m_iDelayTime<0||m_iDelayTime>30)
m_iDelayTime=10; m_strBKHostIP.ReleaseBuffer();
m_strBKDB.ReleaseBuffer();
m_strPassWord.ReleaseBuffer();
m_strBKHostIP.Trim();
m_strServerName.ReleaseBuffer();
m_strUserName.ReleaseBuffer();
m_strDBName.ReleaseBuffer();
m_strBKDB.Trim();
m_strPassWord.Trim();
m_strServerName.Trim();
m_strUserName.Trim();
m_strDBName.Trim();
只做了简单的操作赋值
比如
CString cc;
cc="";
和Mid,Right,Left,Format等操作
不知道你代码用没用格式化函数.
如果格式不对的话,很可能造成内存块分配错误.
-----------------------------------------------------------------------------
使用了大量的格式化函数
比如
m_strSelect.Format("INSERT INTO deviceinformation\
(Minenumber, StationNumber, StationType, StationAddress,IPAddress,CuriseNumber,RecordRunLog,ExecptionAlarm,DeviceGroup,SecQuee,UpDownWellStation)\
values(%d,%d,%d,'%s','%s',%d,%d,%d,%d,%d,%d)",atoi(m_csNet.GetMineNumber()),m_iPointNumber,m_iStationType,m_strSetupAddress,m_strIP,m_iCuriseNumber,m_iRunLogReport,m_iExecptionAlarm,m_iGroupNumber,m_iSQ,m_UDWStation);
重载CString类,然后在这个新类里面添加一个写文件的函数,将原先所有调用CString类的地方改成调用你自己的CMyString类,同时记录下调用的位置写到文件里去。只有找到了出问题的地方(可能是多个,但是肯定有相同的问题),就可以对症下药了。
或者想法加输出语句
watch the value of all local and mamber variables
用了GetBuffer就ReleaseBuffer
但是我不知道这样有没有潜在问题,理论上好象没有
while(true)
{
CString m_str;
m_str=" ";
......}