title: iOS & OpenCV : debug malloc_error_break on device
body: <p>I got this error when using my app (under ARC)</p>
  
  <pre><code>(816,0x2ffe0000) malloc: *** error for object 0xb185010: pointer being freed was not allocated
  *** set a breakpoint in malloc_error_break to debug
  </code></pre>
  
  <p>So I set a malloc_error_break breakpoint and did not get any additional information to the console.
  I add <strong>bt</strong> to the console and get</p>
  
  <pre><code>(gdb) bt
  #0  0x3635117c in malloc_error_break ()
  #1  0x362dd924 in free ()
  #2  0x31d64588 in -[NSConcreteData dealloc] ()
  #3  0x319a50c4 in _objc_rootRelease ()
  #4  0x319a6db6 in objc_release ()
  #5  0x319a5e0c in (anonymous namespace)::AutoreleasePoolPage::pop ()
  #6  0x319a5d28 in _objc_autoreleasePoolPop ()
  #7  0x37e13e8e in _CFAutoreleasePoolPop ()
  #8  0x37b818e6 in _dispatch_worker_thread2 ()
  #9  0x362e61ce in _pthread_wqthread ()
  #10 0x362e60a4 in start_wqthread ()
  </code></pre>
  
  <p>The error appears at different time, different usage and I cannot find the code involve.
  Any idea how to debug (only on device, I use AVCaptureSession).</p>
  
  <h2>Edit</h2>
  
  <p>I think it comes from</p>
  
  <pre><code>NSData *data = [NSData dataWithBytesNoCopy:cvMat.data
                                      length:(cvMat.elemSize() * cvMat.total())
                                freeWhenDone:YES];
  
  CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
  </code></pre>
  
  <p>With this code, I constraint memory allocation</p>
  
  <p><a href="http://s14.postimage.org/wq2zr6j0x/Capture_d_cran_2012_03_16_18_30_53.png" rel="nofollow">Instruments With No Copy</a></p>
  
  <p>Whereas if I change it to</p>
  
  <pre><code>NSData *data = [NSData dataWithBytes:cvMat.data
                                    length:(cvMat.elemSize() * cvMat.total())];
      CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
  </code></pre>
  
  <p>I got something strange which looks like an AutoReleasePool</p>
  
  <p><a href="http://s18.postimage.org/93o2x0yrd/Capture_d_cran_2012_03_16_18_31_11.png" rel="nofollow">Instruments With Copy</a></p>
  
  <p>Could the error be due to autoreleasing of one of the object in the code ?</p>
  
  <p>*method taken from Robin Summerhill</p>