memory leak xerces use
I have a leak in my application and I've come to reduce my code to the following and it's leaking about 12kb per iteration. I cannot see if this is a problem with my code or a problem with the xerces library itself. But looking at the Private Bytes in Perfmon I can only see growth and no shrinkage, so it's obviously leaking.
Can someone please advice what could be wrong with the following code that causes it to leak at such an incredible rate?:
(single threaded test app)
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;
}
}
I don't understand how this could possibly leak? What have I missed?
Where does impl
get deleted?
I know nothing more about the API than googling the docs, but they suggest to me that you should not be calling Terminate()
- in a real program, other code elsewhere, possibly in other threads, may still be using the xerces library.
The DOMImplementation
is returned as a pointer and has a destructor - clear indications you have to manage its lifetime. It seems a really likely story that that is your memory leak.
Furthermore, that the DOMImplementationRegistry::getDOMImplementation()
can return NULL
so you have to guard against that.
If you can run this on Linux,use Valgrind (Purify is a commercial equivalent for windows)
Not sure where you allocate document
.
In the ReleaseDocument() function you don't delete it. All you do is set it to zero after clearing its content.
PS: don't know xerces either.
精彩评论