使用delete []出现的堆栈调试错误

废话不说,贴代码段:VS2010+C++


EnterCriticalSection(&m_crit);
m_InitImport = TRUE;
if(m_fileList.Open(exePath+"\\playMenu.txt",CFile::modeRead))
{
pfile = new char[m_fileList.GetLength()+10];


memset(pfile,0,m_fileList.GetLength());

int a  = m_fileList.Read(pfile,m_fileList.GetLength());
pfile[a] = '\0';
strFind.Format("%s",pfile);

while(true)
{
pos2 = strFind.Find(",",pos1+1);
if (pos2 <= 0)
{

if (BW_AddAudioFileName(strFind.Mid(pos1,strFind.GetLength()-pos1)) == 0)
{
importnum ++;
}

break;
}
name = strFind.Mid(pos1,pos2-pos1);
if (BW_AddAudioFileName(name) == 0)
{
importnum ++;
}

pos1 = pos2+1;
}
delete []pfile;
m_fileList.Close();
}
m_InitImport = FALSE;
LeaveCriticalSection(&m_crit);



这段代码,从playMenu.txt中读取逗号分隔的音频文件名称,之前一直在delte []pfile处,出现错误,报堆栈异常,后来网上查找资料,发现new的内存和实际delete的内存大小不符合,仔细检查,想明白了,是new的小了,所以改成pfile = new char[m_fileList.GetLength()+10];然后一切OK ,仅以警示和启发,new和delete使用要尽量小心点。

发布了61 篇原创文章 · 获赞 6 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/shayueqing/article/details/50060721
今日推荐