I am using C++ hash_map to store some C-style string pairs. And all keys should be unique for this case...
My problem is a serious memory leak when stress testing this over mutliple runs.
When none of these keys in the test are not identical, there is no memory leak. But with identical keys its a different story...
The hash_map (this is Google's sparsehash but it implements the SGI functions entirely)
sparse_hash_map<const char*, char *, hash<const char*>, eqstr> values;
I searched and could not find a function to replace a key/val pair that has an identical key.
values[k]=v;
will only add a new pair, even if the key is the same. (a behavior I think should be toggleable) - this is nothing but a hash_map.insert()
So I have a function to check if the key exists, and if it does replace the val, and if not just add a new pair:
char *confFile::replaceOrStoreVal( char *k, char *v ) {
char *ret = NULL;
values.set_deleted_key(_DIST_KEY);
sparse_hash_map<const char*, char *, hash<const char*>, eqstr>::iterator it =
values.find(k);
if(it == values.end())
values[k] = v;
else {
// ret = it->second; // option 1
//it->second = v; // end option 1
//option 2
char *t = (char *) it->first;
ret = it->second;
values.erase( iter ); <--- seg fault here
free(t);
values[k] = v; // end option 2
}
return ret;
} ... and ret is later free()ed
initally pairs are added like this:
old = replaceOrStoreVal(recordname, value);
It crashes on the first duplicate key.
2 ways I have tried this. Option 1 results in a segfault on erase (something that also puzzles me). Option 2 just doesnt fix the problem, still have a memory leak. Maybe I am just doing this all wrong.
Yes, I know I could use C++ strings, but I dont want to. Trying to keep this real light, for an embedded system. Any help is appreciated...