{"action":"created","issue":{"url":"https://api.github.com/repos/greg7mdp/sparsepp/issues/19","repository_url":"https://api.github.com/repos/greg7mdp/sparsepp","labels_url":"https://api.github.com/repos/greg7mdp/sparsepp/issues/19/labels{/name}","comments_url":"https://api.github.com/repos/greg7mdp/sparsepp/issues/19/comments","events_url":"https://api.github.com/repos/greg7mdp/sparsepp/issues/19/events","html_url":"https://github.com/greg7mdp/sparsepp/issues/19","id":195055999,"number":19,"title":"Possible iterator bug?","user":{"login":"indigox4","id":19512882,"avatar_url":"https://avatars.githubusercontent.com/u/19512882?v=3","gravatar_id":"","url":"https://api.github.com/users/indigox4","html_url":"https://github.com/indigox4","followers_url":"https://api.github.com/users/indigox4/followers","following_url":"https://api.github.com/users/indigox4/following{/other_user}","gists_url":"https://api.github.com/users/indigox4/gists{/gist_id}","starred_url":"https://api.github.com/users/indigox4/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/indigox4/subscriptions","organizations_url":"https://api.github.com/users/indigox4/orgs","repos_url":"https://api.github.com/users/indigox4/repos","events_url":"https://api.github.com/users/indigox4/events{/privacy}","received_events_url":"https://api.github.com/users/indigox4/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-12-12T18:58:46Z","updated_at":"2016-12-13T02:47:33Z","closed_at":null,"body":"I'm replacing some usage of `std::unordered_map` with `sparse_hash_map` and I've encountered an occasional issue with iterators. \r\n\r\n```\r\nmap_t::const_iterator iter = map.find(keyA); // after this line iter contains a good value\r\nmap[keyB] = (*iter).second; // after this line iter contains a garbage value\r\n```\r\n\r\nWith this code, keyB will occasionally map to some garbage values (0xdddddddd, indicating deleted memory), and not the same value as keyA, which is what I would expect. The same code works fine with `std::unordered_map`. \r\n\r\n\r\nIf I store keyA's value in a temp variable everything works as expected:\r\n```\r\nmap_t::const_iterator iter = map.find(keyA);\r\ntempVal = (*iter).second;\r\nmap[keyB] = tempVal;\r\n```\r\n\r\n(Unfortunately, I can't reproduce this issue with a simple test case, only our internal code.)\r\n\r\nUsing the temp variable is fine for my usage, just curious if what I'm seeing is a bug, or expected behaviour. I saw in the readme that calls to `erase()` can invalidate iterators. Is the same true with insertions as well?\r\n"},"comment":{"url":"https://api.github.com/repos/greg7mdp/sparsepp/issues/comments/266620489","html_url":"https://github.com/greg7mdp/sparsepp/issues/19#issuecomment-266620489","issue_url":"https://api.github.com/repos/greg7mdp/sparsepp/issues/19","id":266620489,"user":{"login":"greg7mdp","id":1496907,"avatar_url":"https://avatars.githubusercontent.com/u/1496907?v=3","gravatar_id":"","url":"https://api.github.com/users/greg7mdp","html_url":"https://github.com/greg7mdp","followers_url":"https://api.github.com/users/greg7mdp/followers","following_url":"https://api.github.com/users/greg7mdp/following{/other_user}","gists_url":"https://api.github.com/users/greg7mdp/gists{/gist_id}","starred_url":"https://api.github.com/users/greg7mdp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/greg7mdp/subscriptions","organizations_url":"https://api.github.com/users/greg7mdp/orgs","repos_url":"https://api.github.com/users/greg7mdp/repos","events_url":"https://api.github.com/users/greg7mdp/events{/privacy}","received_events_url":"https://api.github.com/users/greg7mdp/received_events","type":"User","site_admin":false},"created_at":"2016-12-13T02:47:33Z","updated_at":"2016-12-13T02:47:33Z","body":"Hi there,\r\n\r\nThank you using sparsepp, and for for taking the time to report this issue. Indeed iterators may be invalidated when an item is inserted into sparsepp, and that is expected behavior, but I should have stated it clearly in the readme.md (which I will update).\r\n\r\nPlease note that inserting an item into a std::unordered_map may also invalidate iterators, albeit only if this causes a resize. As stated in the doc for [std::unordered_map](http://en.cppreference.com/w/cpp/container/unordered_map/insert):\r\n\r\n```\"If rehashing occurs due to the insertion, all iterators are invalidated. Otherwise iterators are not affected. References are not invalidated. Rehashing occurs only if the new number of elements is greater than max_load_factor()*bucket_count().\"```\r\n\r\nUsing the temp variable is a good solution. I think insert would also work as the parameters would be evaluated first:\r\n\r\n`map.insert({keyB, (*iter).second});`"}}
{"actor":{"display_login":"greg7mdp"}}