小弟自定义了一个类,继承了CEdit类,可以显示输出文本及轮动功能。申明一个此类的对象名为m_Info.然后通过消息队列的方式,在m_Info上输出文本。
下面是消息实现文本显示的函数:
LRESULT CMyView::OnInfo(WPARAM w, LPARAM l)
{
static int i = 1;
try{
CString strNum;
strNum.Format("[%d]",i++);
CString str;
char *info = (char *)w;
CString strInfo(info);
delete []info;
m_Info.GetWindowText(str);
int lenMax = m_Info.GetLimitText();
int lenNow = str.GetLength();
int lenAppend = strInfo.GetLength() + 2;
int lenNum = strNum.GetLength();
if( (lenNow+lenAppend+lenNum) > lenMax )
m_Info.SetSel(0,-1);
else
m_Info.SetSel(lenNow,lenNow);
try{strInfo = strNum + strInfo + "\x0d\x0a"; <---这一句出错
}catch(...){
::AfxMessageBox("加串出错!");
}
try{m_Info.ReplaceSel(strInfo);}catch(...){
::AfxMessageBox("ReplaceSel出错!");
}
}
catch(...){
::AfxMessageBox("输出到文本框出错!");
}
return 0;
}
现在的问题是:运行一段很短的时间后,大概是输出了50行左右。就会报错。请问是什么原因造成的?
char *info = (char *)w;
CString strInfo(info);
delete []info;
是怎么了?,为什么要删除info,你保证w申请那边没有再次释放?
strInfo = strNum + strInfo + _TEXT("\x0d\x0a")
多线程之间是否有什么关系,没关系的话,就没什么影响
看了很久,觉得不会错啊。是不是你在OnInfo(WPARAM w, LPARAM l)中传入的参数w有问题啊,本来即使有问题也不应该出错的,晕了。