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
implget 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
DOMImplementationis 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 returnNULLso you have to guard against that.If you can run this on Linux,use Valgrind (Purify is a commercial equivalent for windows)