title: c++ Floodfill algorithm crashes in Xcode
body: <p>I have a simple floodfill function, except it crashes with too much deallocating while running.</p>
  
  <pre><code>#include &lt;vector&gt;
  cv::Mat fillLayer(cv::Mat filledEdge, int y, int x, int oldColor, float newColor){
      cv::Size shape = filledEdge.size();
      int h = shape.height;
      int w = shape.width;
  
      std::vector&lt;int&gt; theStackx = {x};
      std::vector&lt;int&gt; theStacky = {y};
  
  
      while (theStacky.size() &gt; 0){
          y = theStacky.back();
          x = theStackx.back();
          theStacky.pop_back();
          theStackx.pop_back();
  
          if (x == w){
              continue;
          }
          if (x == -1){
              continue;
          }
          if (y == -1){
              continue;
          }
          if (y == h){
              continue;
          }
  
          if (filledEdge.at&lt;float&gt;(y, x) != oldColor){
              continue;
          }
  
          filledEdge.at&lt;float&gt;(y, x) = newColor;
  
          //up
          theStacky.push_back(y + 1);
          theStackx.push_back(x);
          //down
          theStacky.push_back(y - 1);
          theStackx.push_back(x);
          //right
          theStacky.push_back(y);
          theStackx.push_back(x + 1);
          //left
          theStacky.push_back(y);
          theStackx.push_back(x - 1);
      }
      return filledEdge;
  
  }
  </code></pre>
  
  <p>The function that runs through floodfill is <code>fillSurface</code>. which runs through all the pixels in a Mat and fills them with a different color per floodfill</p>
  
  <pre><code>fillSurface(cv::Mat filledEdge, int oldColor) {
      std::vector&lt;float&gt; layers; //list all the different colors in mat
      cv::Size shape = filledEdge.size();
      int h = shape.height;
      int w = shape.width;
      float newColor;
      // run through all the pixels in Mat
      for(int y = 0; y!= h; y++){
          for(int x = 0; x!= w; x++){
              // only run floodfill if current pixel is oldColor
              if (filledEdge.at&lt;float&gt;(y, x) == oldColor){
                  //newColor is random float to fill in to floodfill
                  newColor = static_cast &lt;float&gt; ((rand()) / (static_cast &lt;float&gt; (RAND_MAX/253)) + 1); 
                  // add newColor to list of layers
                  layers.push_back(newColor);
                  //run flood fill replacing old color with new color
                  filledEdge = fillLayer(filledEdge, y, x, oldColor, newColor);
  
              }
          }
      }
  }
  </code></pre>
  
  <p>This is the error I receive:</p>
  
  <pre><code>Incorrect checksum for freed object 0x7fea0d89dc00: probably modified after being freed.
  </code></pre>
  
  <p>The debugging I've done is far is set a breakpoint on malloc_error_break() to see where exactly I'm getting the break. It does lead to the floodfill function.</p>
  
  <p><a href="https://i.stack.imgur.com/TLuLZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TLuLZ.png" alt="enter image description here"></a></p>
  
  <p>I'm wondering if there's anyway to fix this. If not, what would be the best alternative?</p>