Ok, I'm very new to Valgrind and memory leak profiles in general. And I have to say that it’s a little scary when you start using them, because you can’t stop wondering how many leaks you could have left unsolved before!
Due to the fact that I am not an experienced C ++ programmer, I would like to check if this is a memory leak or that Valgrind is doing a false result?
typedef std::vector<int> Vector;
typedef std::vector<Vector> VectorVector;
typedef std::map<std::string, Vector*> MapVector;
typedef std::pair<std::string, Vector*> PairVector;
typedef std::map<std::string, Vector*>::iterator IteratorVector;
VectorVector vv;
MapVector m1;
MapVector m2;
vv.push_back(Vector());
m1.insert(PairVector("one", &vv.back()));
vv.push_back(Vector());
m2.insert(PairVector("two", &vv.back()));
IteratorVector i = m1.find("one");
i->second->push_back(10);
m2.insert(PairVector("one", i->second));
m2.clear();
m1.clear();
vv.clear();
Why? Should a clear command invoke the destructor of each object and each vector?
Now, after several tests, I have found various leak solutions:
1) Removal:
i->second->push_back(10);
2) Addition:
delete i->second;
3) Removing the second
vv.push_back(Vector());
m2.insert(PairVector("two", &vv.back()));
Using solution 2) does Valgring print: 10 allocs, 11 frees Is this normal?
, ?
, !