2011-03-28 172 views
0

我在应用程序中有泄漏,我已经将我的代码减少到下面,并且每次迭代泄漏大约12kb。我看不到这是否是我的代码问题或者xerces库自身的问题。但看看Perfmon中的私人字节,我只能看到增长和收缩,所以显然是漏了。内存泄漏xerces使用

有人可以请指教什么可能是错用下面的代码,导致它在这样一个令人难以置信的速度?:

(单线程测试程序)

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; 
    } 
} 

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

+0

也许你可以展示更多的源代码?例如,DataSerializer构造函数是怎样的? – 2013-03-20 17:29:06

回答

2

impl在哪里得到删除?

我对API没有什么比谷歌搜索文档更多,但他们暗示我不应该调用Terminate() - 在一个真正的程序中,别处的其他代码,可能在其他线程中,可能仍然使用xerces库。

DOMImplementation作为指针返回并具有析构函数 - 清除指示必须管理其生存期。这似乎是真的可能的故事,那是你的内存泄漏。

此外,DOMImplementationRegistry::getDOMImplementation()可以返回NULL所以你必须防范。

如果你能在Linux上运行它,使用Valgrind(净化是一个用于Windows的商用当量)

+0

将:afaik这应该删除impl:XMLPlatformUtils :: Terminate(); – 2011-03-28 10:10:52

0

不知道你在哪里分配document。 在ReleaseDocument()函数中,您不会将其删除。清除内容后,您只需将其设置为零即可。 PS:不知道xerces也不知道。

+0

document-> release();删除... – 2011-03-28 11:44:00