4

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.

2
  • 1
    How many CRTs do you have in the process? Are you allocating in one and deallocating in another? Aug 10, 2011 at 15:47
  • The place in the code where the corruption is detected is very rarely the place where the corruption occurred. Use a good debug allocator to troubleshoot this, like crtdbg.h Aug 10, 2011 at 15:58

1 Answer 1

10

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.

2
  • 1
    "So make sure both dlls (and your app) all are built with the same CRT." Well, if you can make sure that you allocate and deallocate in the same module. Aug 10, 2011 at 15:53
  • I have checked CRTs. The two dlls were in /MT and my app in /MD. So, I changed the CRT of my dlls to /MD and it's work better. Thank you both ;-)
    – Cedekasme
    Aug 10, 2011 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.