When I compile in release mode, I have heap corruption on the deallocation of a std::string.
In fact, in a DLL named Atc.dll
, I call a function which is in another DLL named Utilies.dll
. At the end of my function in Atc.dll
, I have the heap corruption.
This my function in Atc.dll
:
void CoreController::readConfigXMLFile()
{
ConfigFileManager configFileManager;
std::string pathXmlFilesTemp = configFileManager.getPathXMLFiles();
}
Then, this is the function getPathXMLFiles
declared in Utilies.dll
:
std::string ConfigFileManager::getPathXMLFiles()
{
bool err = false;
std::string ret = "";
if (!mParserXml.getNodeTextValue(sPathXmlFilesTag, ret))
{
err = true;
}
if (err)
{
ret = sPathXMLFilesDefault;
}
return ret;
}
Note: If here I don't call the function getNodeTextValue
, the heap corruption doesn't occur. So, there is the function getNodeTextValue
which is in the same DLL than getPathXMLFiles
:
bool ParseXml::getNodeTextValue(const string& path, string& nodeValue)
{
DOMNode* child = XmlNode::getChildNodeByPath(xmlNode, path);
//If path is valid.
if(XmlNode::isValid(child))
{
char* str = XmlNode::getTextValue(child);
//If node contains text.
if(str != NULL)
{
nodeValue = str;
XmlNode::freeXMLString(str);
return true;
}
}
//Either path is not valid or node does not contain text
nodeValue = "";
return false;
}
And this is where the heap corruption occurs (STL):
void _Tidy(bool _Built = false,
size_type _Newsize = 0)
{ // initialize buffer, deallocating any storage
if (!_Built)
;
else if (this->_BUF_SIZE <= this->_Myres)
{ // copy any leftovers to small buffer and deallocate
_Elem *_Ptr = this->_Bx._Ptr;
if (0 < _Newsize)
_Traits::copy(this->_Bx._Buf, _Ptr, _Newsize);
this->_Alval.deallocate(_Ptr, this->_Myres + 1); // <-- HEAP CORRUPTION
}
this->_Myres = this->_BUF_SIZE - 1;
_Eos(_Newsize);
}
Can someone tell me why this heap corruption occurs and how to avoit it ?
Thanks for your help.
The usual reason (on Windows) is that you allocate memory in 1 dll, pass it to another when then frees is. Often this is fine, but if each dll is built with a different CRT (C runtime library) then you get issues like this. The reason there is that the debug CRT does memory allocation slightly differently than the release CRT (mainly padding all allocs with guard blocks to show up buffer overruns etc).
So make sure both dlls (and your app) all are built with the same CRT.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With