title: Segmentation fault malloc_consolidate
body: <p>I am running this code (simplified for staying on point):</p>
  
  <pre><code>int main(int argc, char** argv) {
  
      char* videoPath = args[1];
  
      DataSource* dataSource;
      std::thread dataSourceThread;
      dataSource = new FileDataSource(0.5f, true);
      dataSourceThread = std::thread(&amp;FileDataSource::start, (FileDataSource*)dataSource, videoPath, 50);
  
  
      logger-&gt;info("FileDataSource started");
      float invScale = 1.f / dataSource-&gt;getScale();
  
      cv::Mat frame;
      unsigned long bufferIndexPrev = 0;
      unsigned long t;
  
      cv::Mat projMat(3, 4, CV_32F);
      cv::Mat rVec, tVec;
  
      while (!dataSource-&gt;isRunning()) std::this_thread::yield();
      logger-&gt;info("Data source running");
  
      Tracker tracker;
      tracker.dataSource = dataSource;
      std::thread trackerThread(&amp;Tracker::start, &amp;tracker);
  
      while (tracker.stateBuffer == nullptr) std::this_thread::yield();
      logger-&gt;info("Tracker started");
  
      while (true) {
          int bufferIndex = tracker.stateBuffer-&gt;bufferIndex;
          if (bufferIndex == bufferIndexPrev) {
              if (dataSource-&gt;isRunning()) {
                  // std::this_thread::sleep_for(std::chrono::milliseconds(1));
                  std::this_thread::yield();
                  continue;
              }
              else {
                  break;
              }
          }
          bufferIndexPrev = bufferIndex;
  
  
          State&amp; state = tracker.stateBuffer-&gt;buffer[bufferIndex];
          Data data = state.data;
  
          int lastProcessedFrameNum = tracker.stateBuffer-&gt;buffer[bufferIndex].dataPacket.frameNumber;
  
          dataSource-&gt;getOriginalFrameByFrameNum(lastProcessedFrameNum, frame);
  
          cv::putText(frame, "Processing", cv::Point(20, 30), CV_FONT_HERSHEY_PLAIN, 2, CV_RGB(255, 0, 0)); 
       // ^^^^^^^^^ this line causes the crash
          cv::putText(frame, std::to_string(lastProcessedFrameNum), cv::Point(200, 30), CV_FONT_HERSHEY_PLAIN, 2, CV_RGB(255, 255, 0));
  
          std::vector&lt;cv::KeyPoint&gt; keypoints = state.keypoints;
          std::vector&lt;int&gt; indices = state.indices;
          for (int i = 0; i &lt; keypoints.size(); i++) {
              cv::Point2f detectedPoint = keypoints[i].pt * invScale;
              if (state.indices[i] &gt;= 0)
                  cv::drawMarker(frame, detectedPoint, CV_RGB(0, 255, 0), cv::MARKER_CROSS);
              else 
                  cv::drawMarker(frame, detectedPoint, CV_RGB(255, 0, 0), cv::MARKER_CROSS);
  
  
          }
  
  
          cv::imshow("Video", frame);
  
          int key = cv::waitKey(10);
          if (key == 32) {
              tracker.nextPhase();
          }
  
      }
  
      logger-&gt;info("Exited main loop");
  
      if (trackerThread.joinable()) trackerThread.join();
      if (dataSourceThread.joinable()) dataSourceThread.join();
  
      return 0;
  }
  </code></pre>
  
  <p>Now, the program <strong>sometimes</strong> crashes with <strong>Segmentation Fault (SIGSEGV)</strong> on </p>
  
  <pre><code>cv::putText(frame, "Processing", cv::Point(20, 30), CV_FONT_HERSHEY_PLAIN, 2, CV_RGB(255, 0, 0));
  </code></pre>
  
  <p>I debug it using GDB. The way I see it, the only thing that can be causing problems is the matrix object <code>frame</code>.
  I can also confirm this, since the code never crashes if I place another instance of <code>cv::Mat</code> instead of <code>frame</code>.</p>
  
  <p>The data source is running its own loop of fetching video frames and putting them inside a circular buffer. The main program takes a video frame from that buffer when it needs one.</p>
  
  <pre><code>void FileDataSource::start(char* fileVideoPath, int fps) {
  
      logger-&gt;info("FileDataSource opening FileVideo {}", fileVideoPath);
      FileVideo video(fileVideoPath);
  
      unsigned long period = 1.e+6 / fps; // microseconds
      std::chrono::microseconds periodDuration(period);
  
      std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
      while (true) {
          int nextBufferIndex = (bufferIndex + 1) % bufferSize;
          cv::Mat&amp; originalFrame = originalFrameBuffer[nextBufferIndex];
          bool success = video.getFrame(originalFrame);
          if (!success) break;
  
          cv::Mat&amp; reducedFrame = reducedFrameBuffer[nextBufferIndex];
          cvtColor(originalFrame, reducedFrame, CV_BGR2GRAY);
          if (!this-&gt;presized) resize(reducedFrame, reducedFrame, cv::Size(), scale, scale);
  
          frameNumBuffer[nextBufferIndex] = frameNumBuffer[bufferIndex] + 1;
          tBuffer[nextBufferIndex] = tBuffer[bufferIndex] + period;
          bufferIndex = nextBufferIndex;
          this-&gt;running = true;
          std::this_thread::sleep_until(now + periodDuration);
          now = std::chrono::system_clock::now();
      }
  
      this-&gt;running = false;
  }
  
  void FileDataSource::getOriginalFrameByFrameNum(unsigned long frameNum, cv::Mat&amp; frame) {
  
      int lBufferIndex = this-&gt;bufferIndex;
      for (int i = 0; i &lt; bufferSize; i++)
      {
          if (frameNumBuffer[lBufferIndex] == frameNum) break;
          lBufferIndex = (lBufferIndex - 1 + bufferSize) % bufferSize;
      }
  
      frame = originalFrameBuffer[lBufferIndex];
  }
  </code></pre>
  
  <p>The <code>tracker</code> neither reads or writes to the <code>originalFrameBuffer</code>, only reads from <code>reducedFrameBuffer</code> but this is not displayed in code since the post is large enough as it is.</p>
  
  <p>From <a href="https://stackoverflow.com/questions/2346806/what-is-a-segmentation-fault">another SO question</a> I learn about the nature of segmentation fault:</p>
  
  <blockquote>
    <p>Segmentation fault is a specific kind of error caused by accessing
    memory that “does not belong to you.” It’s a helper mechanism that
    keeps you from corrupting the memory and introducing hard-to-debug
    memory bugs. Whenever you get a segfault you know you are doing
    something wrong with memory – accessing variable that has already been
    freed, writing to a read-only portion of the memory, etc.</p>
  </blockquote>
  
  <p>I really don't see how this applies to my case. Any ideas?</p>
  
  <p>The GDB backtrace shows:</p>
  
  <pre><code>Thread 1 "videoGTKExample" received signal SIGSEGV, Segmentation fault.
  0x00007fffed095281 in malloc_consolidate () from /lib/libc.so.6
  (gdb) bt
  #0  0x00007fffed095281 in malloc_consolidate () at /lib/libc.so.6
  #1  0x00007fffed096d2a in _int_malloc () at /lib/libc.so.6
  #2  0x00007fffed098d44 in malloc () at /lib/libc.so.6
  #3  0x00007fffed966a78 in operator new(unsigned long) (sz=8192) at /build/gcc-multilib/src/gcc/libstdc++-v3/libsupc++/new_op.cc:50
  #4  0x00007fffeed3d684 in cv::putText(cv::_InputOutputArray const&amp;, cv::String const&amp;, cv::Point_&lt;int&gt;, int, double, cv::Scalar_&lt;double&gt;, int, int, bool) ()
      at /usr/local/lib/libopencv_imgproc.so.3.1
  #5  0x00000000004a920b in main(int, char**) (argc=4, argv=0x7fffffffe0d8) at /home/andro/tracker/examples/videoGTKExample.cpp:180
  </code></pre>
  
  <p>P.S. 
  If I run the code with valgrind, I cannot reproduce the error since it is executing much slower and the concurrency of threads no longer seems to be the problem.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I've managed to reproduce the SIGSEGV crash while running via:</p>
  
  <pre><code>valgrind --tool=exp-sgcheck
  </code></pre>
  
  <p>The resulting log is <a href="http://textuploader.com/d1jzc" rel="nofollow noreferrer">here</a>. I am not sure though that this is the same error since it occurred at a line where it is not occurring regularly. I am new to valgrind and do not know if there is anything useful in this log. All I see is a stacktrace, similar to what I see during normal gdb session.</p>
  
  <p><strong>UPDATE 2:</strong></p>
  
  <p>In <code>gdb</code> I also sometimes (rarely) get the following crash:</p>
  
  <pre><code>Thread 1 "videoGTKExample" received signal SIGABRT, Aborted.
  0x00007fffed05104f in raise () from /lib/libc.so.6
  (gdb) bt
  #0  0x00007fffed05104f in raise () at /lib/libc.so.6
  #1  0x00007fffed05247a in abort () at /lib/libc.so.6
  #2  0x00007fffed08ec50 in __libc_message () at /lib/libc.so.6
  #3  0x00007fffed094fe6 in malloc_printerr () at /lib/libc.so.6
  #4  0x00007fffed09536c in malloc_consolidate () at /lib/libc.so.6
  #5  0x00007fffed096d2a in _int_malloc () at /lib/libc.so.6
  #6  0x00007fffed098d44 in malloc () at /lib/libc.so.6
  #7  0x00007fffed966a78 in operator new(unsigned long) (sz=8192) at /build/gcc-multilib/src/gcc/libstdc++-v3/libsupc++/new_op.cc:50
  #8  0x00007fffeed3d684 in cv::putText(cv::_InputOutputArray const&amp;, cv::String const&amp;, cv::Point_&lt;int&gt;, int, double, cv::Scalar_&lt;double&gt;, int, int, bool) ()
      at /usr/local/lib/libopencv_imgproc.so.3.1
  #9  0x00000000004a951b in main(int, char**) (argc=4, argv=0x7fffffffe0d8) at /home/andro/stypevisualirtracker/examples/videoGTKExample.cpp:180
  </code></pre>
  
  <p>From this <a href="https://stackoverflow.com/questions/3413166/when-does-a-process-get-sigabrt-signal-6">question</a>:</p>
  
  <blockquote>
    <p>abort() sends the calling process the SIGABRT signal, this is how
    abort() basically works.</p>
    
    <p>abort() is usually called by library functions which detect an
    internal error or some seriously broken constraint. For example
    malloc() will call abort() if its internal structures are damaged by a
    heap overflow.</p>
  </blockquote>
  
  <p>So this is most certainly caused by corrupted heap.</p>