title: BAD_ACCES iterator on map c++
body: <p>I'm trying to implement a function using a map in a map like that :  </p>
  
  <pre><code>typedef std::map&lt;int, float&gt; inner_map;
  typedef std::map&lt;int, inner_map&gt; outer_map;
  </code></pre>
  
  <p>I'm filling the map this way. The idea is the first map contains a int key and an other map which contains another key and a float. If the pair I obtain is known, I increment the value otherwise I fill the map. </p>
  
  <pre><code>for (int y = 2; y &lt; imgSize.y - 2; y++){
      for (int x = 2; x &lt; imgSize.x - 2; x++) {
          thisPos = x + y*imgSize.x ;
  
          {SOME CODE}
  
          lj = labelMap[thisPos] ;
  
          if (condition) {
              // get the 2 label 
              li = pro_label_mod[thisPos] ; 
  
              // look if it lj is in the map 
              is_lj = pi_matrix.count(lj) ;
  
              // this lj has already been observed 
              if (is_lj == 1) {
                  is_li = pi_matrix[lj].count(li);
  
                  // this pair is known -&gt; I increment 
                  if (is_li==1) {
                      pi_matrix[lj][li] +=  1.0f ;
                  }
                  else {
                       pi_matrix.at(lj).insert(pi_matrix[lj].end(), std::make_pair(li, 1.0f)) ;
                  }
              }
              else {
                  inner_map inter ; inter.insert(std::make_pair(li, 1.0f)) ;
                  pi_matrix.emplace(lj, inter) ;
              }
          }
          numb_lj[lj] += 1.0f ;
      }
  }
  </code></pre>
  
  <p>And then, I would like to iterate throught the map in my code. I did that with iterators such as : </p>
  
  <pre><code>std::vector&lt;std::pair&lt;int,float&gt; &gt; max_pi(size_lj) ;
  
  float maxValue, thisValue ; int label_max ; 
  
  for (outer_map::iterator it_lj = pi_matrix.begin(), it_lj_end = pi_matrix.end(); it_lj != it_lj_end; ++it_lj) {
      maxValue = 0.0f ;
      label_max = 0 ;
  
      inner_map &amp;innerMap = it_lj-&gt;second;
  
      for(inner_map::iterator it_li = innerMap.begin(), it_li_end = innerMap.end(); it_li != it_li_end; ++it_li) {
          thisValue = it_li-&gt;second / numb_lj[it_lj-&gt;first] ;
  
          if (thisValue &gt;= maxValue) {
              maxValue = thisValue ;
              label_max = it_li-&gt;first ;
          }
      }
  
      max_pi[it_lj-&gt;first] = std::make_pair(label_max, maxValue) ;
      i+=1;
  }
  </code></pre>
  
  <p>However, i'm getting a segmentation fault at the lines of the for loop (either the first one or the second one). BUT ! Not every time. I'm calling this function at each frame and I can have 5 calls without a BAD_ACCESS and suddenly, it crashes.. Sometimes after the first call and then the 10th.</p>
  
  <p>I really can't see why and how to solve it..</p>
  
  <p>Thank you in advance for any clues/comments which could be helpful ! </p>