c++ - unordered_map iterator iterates to null pointer, defying possibility -


i storing bunch of dummy objects in unordered_map, , iterating through delete them. somehow, comes out key value pairing value null pointer, though i'm pretty sure not possible.

each of objects make houses part of large string construct randomly. make sure hand constructor of object @ least 5 characters of string.

here code:

delete function

bool delete_objects(){     serial.println("deleting objects in 'store'");     for(auto iter = store.begin(); iter != store.end(); iter++)     {         serial.println("\ndeleting following element: \n");         serial.print("key: ");         serial.println(iter->first);         delay(1000);         serial.print("value: ");         serial.println(iter->second->showmewhatyougot());         delay(1000);          delete iter->second;         iter->second = nullptr;         store.erase(iter);     }       if(store.empty())        return true;     else        return false; } 

block objects allocated (delete_objects() called right before this)

unsigned int index = 0; unsigned int key = 0; while(index < buff_string.length()){     int amount = (rand() % 51) + 5;     dummyclass* dc = new dummyclass(buff_string.substr(index, amount));     store[key] = dc;     index += amount;     key++;     serial.println("created new dummyclass object");     serial.print("free memory: ");     serial.println(system.freememory()); } 

class definition

using namespace std;  class dummyclass {     private:         char* _container;      public:         dummyclass(){         }          dummyclass(string input){             _container = new char[input.length()];             strcpy(_container, input.c_str());         }          ~dummyclass(){             delete _container;             _container = nullptr;         }          char* showmewhatyougot(){             return _container;         } }; 

serial output, sos's (crashes, particle chip) when key 0

key: 194 value: q->@b{s?tx/min3k?[~2[mvtoskpe34#5[@q8*d8bzwh`

deleting following element:

key: 193 value: 9hb^7-j(#z3h#0uqg

deleting following element:

key: 192 value: v$xz>c$u{mjxzxl?{kqqvqp*mn3~ce&yzbmop1$9xlkjm)jgja~p{my

deleting following element:

key: 191 value: yo*cvze~2

deleting following element:

key: 190 value: [&pqaktv3{^aq?(ffv/*24xaxej]~t1^sfwim3atpk#{coq

deleting following element:

key: 0 value

share|improve question
    
up vote 2 down vote accepted

from http://en.cppreference.com/w/cpp/container/unordered_map/erase

references , iterators erased elements invalidated. other iterators , references not invalidated.

after

    store.erase(iter); 

iter invalid. expression ++iter after cause undefined behavior.

one way solve problem:

for(auto iter = store.begin(); iter != store.end(); /* iter++ */)                                                     // not needed {     // code     // ...      iter = store.erase(iter); }  

another way solve problem:

for(auto iter = store.begin(); iter != store.end(); iter++) {     // code     // ...      // dont' call erase.     // store.erase(iter); }   // clear map. store.clear(); 
share|improve answer

your answer

 
discard

posting answer, agree privacy policy , terms of service.

not answer you're looking for? browse other questions tagged or ask own question.

Comments