umar-qureshi2 / lru-cache-cpp

Automatically exported from code.google.com/p/lru-cache-cpp
0 stars 0 forks source link

1) using std::string as a key. 2) contact email is not responding #1

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. use <std::string> as a Key type and <MyClass> as a Value type
<int> for MyClass will work here.

2. To make hash_map work with std::string I followed the tips from 
 http://the-lost-beauty.blogspot.com/2008/10/cc-gnucxx-hashmap-under-ubuntu-804.html and http://forums.devshed.com/c-programming-42/tip-about-stl-hash-map-and-string-55093.html

3. Declare LRUCache<std::string, MyClass> with 100 max_size elements.
I call LRUCache insert() method  few hundreds  of times.
Then cache is full the _erase() method is called - hence the key and value 
should be available at this moment. 
But I think what currently LRUCache does not keep std::string internally - it 
just keeps string.c_str() which is not enough, because the original string from 
main apps may be already destroyed.
And of course we do not want to keep in main application the millions of 
std::string and MyClass objects for the application lifetime - it should be 
business of LRUCache to keep the keys (not just the pointers to keys).

Currently I have the memory corruption in _erase() - it is reported by 
valgrind. 
Thanks for quick reply!
vantive98@yahoo.com

Original issue reported on code.google.com by mlubin...@gmail.com on 26 Sep 2011 at 11:27

GoogleCodeExporter commented 9 years ago
How to use LRUCache for hash_map<char*,int> ?
Problem is: then key is char* then LRUCache does not keep internally the key 
value, only the pointer, as a result then original key is destroyed then the 
hash_map key pointer is not valid anymore and the memory corruption happens.
Is it any way to update the current code to support std::unordered_map or boost 
unordered_map ?

Original comment by mlubin...@gmail.com on 29 Sep 2011 at 3:57