这是我的第一篇文章,所以要温柔:D
我刚开始从事这项工作,我的第一个任务是找到我做过的泄漏,评论指出。 我控制点击了gettext(),然后我得到了
virtual System::WideChar * __fastcall GetText(void);
我增加了5个额外的pwide并且泄漏增加了(任务主机中的内存使用量增加了1000)。
我尝试删除并免费但我得到例外。任何人都有关于如何堵塞泄漏的任何建议?
这是我的代码:
UnicodeString CReportManager::GetFinalHTML()
{
UnicodeString ret = "";
ret = "<Reports>";
if( ErrorHTML->Count )
{
ret += "<Errors>" + UnicodeString(ErrorHTML->GetText()) + "</Errors>";
}
//System::WideChar *pWide= ReportHTML->GetText();
//System::WideChar *pWide4= ReportHTML->GetText();
//System::WideChar *pWide1= ReportHTML->GetText();
//System::WideChar *pWide2= ReportHTML->GetText();
//System::WideChar *pWide3= ReportHTML->GetText();
//delete pWide;
// free(pWide);
//ret +=pWide;
ret += ReportHTML->GetText(); //leak here
ret += "</Reports>";
#ifdef _DEBUG
ReportHTML->SaveToFile("C:\\debug.xml");
#endif
delete ReportHTML;
delete ErrorHTML;
ReportHTML = NULL;
ErrorHTML = NULL;
return ret;
}
答案 0 :(得分:1)
好的我找到了答案,如果有人遇到同样的问题,解决方案是StrDispose(pWide);
我尝试了所有4个额外的pWide
并且我的内存泄漏消失了:
UnicodeString CReportManager::GetFinalHTML()
{
UnicodeString ret = "";
ret = "<Reports>";
if( ErrorHTML->Count )
{
ret += "<Errors>" + UnicodeString(ErrorHTML->GetText()) + "</Errors>";
}
System::WideChar *pWide= ReportHTML->GetText();
//delete pWide;
//free(pWide);
ret +=pWide;
StrDispose(pWide);
//ret += ReportHTML->GetText(); //leak here
ret += "</Reports>";
#ifdef _DEBUG
ReportHTML->SaveToFile("C:\\debug.xml");
#endif
delete ReportHTML;
delete ErrorHTML;
ReportHTML = NULL;
ErrorHTML = NULL;
return ret;
}