我的应用程序中有一个漏洞,我已经将我的代码减少到以下代码,并且每次迭代都会泄漏大约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;
}
}我不明白这怎么会泄露出去?我错过了什么?
发布于 2011-03-28 18:08:02
impl在哪里被删除?
我只知道谷歌文档,但他们建议我不应该调用Terminate() -在一个真正的程序中,其他地方的代码,可能在其他线程中,可能仍然在使用xerces库。
DOMImplementation作为指针返回,并且有一个析构函数清除的指示,您必须管理它的生命周期。这似乎是一个非常有可能的故事,那是你的内存泄漏。
此外,DOMImplementationRegistry::getDOMImplementation()可能会返回NULL,因此您必须对此进行防范。
如果您可以在Linux上运行它,那么使用Valgrind (Purify是windows的商业等价物)
发布于 2011-03-28 19:40:57
不确定您将document分配到何处。在ReleaseDocument()函数中,您不能删除它。您所要做的就是在清除其内容后将其设置为零。
PS:我也不知道xerces。
https://stackoverflow.com/questions/5457388
复制相似问题