gpt4 book ai didi

c++ - 内存泄漏练习使用

转载 作者:行者123 更新时间:2023-11-28 08:22:05 27 4
gpt4 key购买 nike

我的应用程序存在泄漏,我将代码缩减为以下内容,每次迭代泄漏大约 12kb。我看不出这是我的代码有问题还是 xerces 库本身有问题。但是看 Perfmon 中的 Private Bytes 只能看到增长,没有看到收缩,所以很明显是在泄漏。

有人可以建议以下代码可能有什么问题导致它以如此惊人的速度泄漏吗?:

(单线程测试应用)

for (int x = 0; x < 1000000; x++){
DataSerializer* ds = new DataSerializer();
ds->test(request);
ds->releasedocument();
ds->destroy_xml_lib();
delete ds;
}

void DataSerializer::test(std::string& request)
{
impl = initialize_impl();
}
DOMImplementation* DataSerializer::initialize_impl()
{
try
{
boost::mutex::scoped_lock init_lock(impl_mtx);
XMLPlatformUtils::Initialize();
return DOMImplementationRegistry::getDOMImplementation(XConv("Core"));
}
catch(const XMLException& toCatch)
{
char *pMsg = XMLString::transcode(toCatch.getMessage());
std::string msg(pMsg);
XMLString::release(&pMsg);
}

return NULL;
}
void DataSerializer::destroy_xml_lib()
{
boost::mutex::scoped_lock terminate_lock (impl_mtx); //is being used in MT app
XMLPlatformUtils::Terminate();
}
void DataSerializer::releasedocument()
{
if (document){
document->release();
document = NULL;
}
}

我不明白这怎么可能泄漏?我错过了什么?

最佳答案

impl 在哪里被删除?

除了谷歌搜索文档外,我对 API 一无所知,但他们向我建议你不应该调用 Terminate() - 在真实程序中,其他地方的其他代码,可能在其他线程中,可能仍在使用 xerces 库。

DOMImplementation 作为指针返回并具有析构函数 - 清楚地表明您必须管理其生命周期。这似乎是一个非常可能的故事,那就是您的内存泄漏。

此外,DOMImplementationRegistry::getDOMImplementation() 可以返回 NULL,因此您必须提防这种情况。

如果你可以在 Linux 上运行它,请使用 Valgrind (Purify 是 Windows 的商业等价物)

关于c++ - 内存泄漏练习使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5457388/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com