Hello everyone,
I have some troubles with memory handling of std::map.
Here is an example of code.
using valgrind :Code:#include <map> #include <iostream> struct MonDouble{ MonDouble(double d): v(d) {} ~MonDouble(){} double v; }; int main(int /*argc*/, char **/*argv*/) { std::map<std::string,MonDouble*> mymap; std::map<std::string,MonDouble*>::iterator it; //insert one value (Part 1) it = mymap.find("VAL"); if(it != mymap.end()){ delete it->second; } mymap.insert(std::make_pair("VAL",new MonDouble(1.5))); //modify the value (Part 2) it = mymap.find("VAL"); if(it != mymap.end()){ delete it->second; } mymap.insert(std::make_pair("VAL",new MonDouble(1.5))); //delete the value (Part 3) it = mymap.find("VAL"); if(it != mymap.end()){ delete it->second; } }
I have "Invalid free() / delete / delete[]" errors on deletes of Part 2 and 3.Code:valgrind --leak-check=yes main
If I remove Part 2 (the modification), I have no error.
I don't see the error I made. Do you have the same problem ?
In advance, thank you for any answer




Reply With Quote