Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy am I having memory leaks?
    text
    copied!<p>I'm using VLD to detect memory leaks in a game I'm writing in C++. It reported no leaks at all until very recently. I have a SettingsManager class (all static methods) which loads settings and can save them (file I/O). Here's how I would load a list of "key=value" settings into a vector:</p> <pre><code>std::vector&lt;Setting*&gt; settings; SettingsManager::loadFromFile(settingsLocation + "display" + settingsExtension, settings); </code></pre> <p>This vector is correctly filled and I can further process the data. The loadFromFile() method is implemented as follows:</p> <pre><code>std::ifstream file; file.open(filename); if(file.is_open()) { std::string line; unsigned pos; while(file.good()) { Setting* s = new Setting; getline(file, line); if(line.empty()) { // do not read empty lines continue; } // parse to Setting pos = line.find('='); s-&gt;key = line.substr(0, pos); s-&gt;value = line.substr(pos + 1); // add to vector settings.push_back(s); } file.close(); return true; } else { return false; } </code></pre> <p>So, this allocates Settings (which is a simple struct with two std::string variables). I delete them from where I call the loadFromFile method with the following method call:</p> <pre><code>SettingsManager::deleteSettings(settings); </code></pre> <p>which is implemented as follows:</p> <pre><code>void SettingsManager::deleteSettings(std::vector&lt;Setting*&gt;&amp; settings) { for(std::vector&lt;Setting*&gt;::iterator it = settings.begin(); it != settings.end(); ++it) { delete (*it); } } </code></pre> <p>When I debug, after the deleteSettings call all the elements in the vector are Bad Ptr's (Visual Studio 2010 Express). If I assign NULL to *it after the delete statement they are all NULL. So I really see no reason why this is giving me memory leaks.</p> <p>Anyone have an idea? Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload