I am writing some code that loads the contents of an XML file into a string, and parsing it using RapidXML. I can successfully do this, but I have came back to clean my code up and came across a problem when making sure I have no memory leaks.
The RapidXML parse function wants a char*, so I cannot pass string.c_str(), so I am copying the contents of the string into a char* array. If I delete the array using delete[], and then pass the array into the parsing function I can access all the data inside the string as if it was never deleted. I can even print the array out, surely this isn't possible? Am I not seeing something glaringly obvious?
    std::string file = loadFile(filePath);
    char* buffer = new char[file.size() + 1];
    std::copy(file.begin(), file.end(), buffer);
    buffer[file.size()] = '\0';
    delete[] buffer;
    rapidxml::xml_document<char> m_currentFile;
    m_currentMap.parse<0>(buffer);
 
    