1

I got this error when using my app (under ARC)

(816,0x2ffe0000) malloc: *** error for object 0xb185010: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

So I set a malloc_error_break breakpoint and did not get any additional information to the console. I add bt to the console and get

(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 ()

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).

Edit

I think it comes from

NSData *data = [NSData dataWithBytesNoCopy:cvMat.data
                                    length:(cvMat.elemSize() * cvMat.total())
                              freeWhenDone:YES];

CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);

With this code, I constraint memory allocation

Instruments With No Copy

Whereas if I change it to

NSData *data = [NSData dataWithBytes:cvMat.data
                                  length:(cvMat.elemSize() * cvMat.total())];
    CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);

I got something strange which looks like an AutoReleasePool

Instruments With Copy

Could the error be due to autoreleasing of one of the object in the code ?

*method taken from Robin Summerhill

  • I think it is similar to http://stackoverflow.com/questions/2257435/how-to-resolve-malloc-error-break-xcode-objective-c and http://www.iphonedevsdk.com/forum/iphone-sdk-development/40661-set-breakpoint-malloc_error_break-debug.html – Vinh Mar 16 '12 at 16:56
  • Already look at these links. malloc_error_break and NSZombies do not work on device unfortunately ! – cyberserker Mar 16 '12 at 17:27

1 Answers1

0

cyberserker,

Your data is being deallocated by the autorelease pool drain. CGDataProviderCreateWithCFData() does not appear to retain your data item. The easiest way to change this behavior is to use the init form to create data. As in:

int *dataBytes = malloc(20);
NSData *data = [[NSData alloc] initWithBytesNoCopy:dataBytes
                                            length:20
                                      freeWhenDone:YES];

CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge_retained CFDataRef)data);

When you finally release provider, you may also have to release the CFData (but I doubt it).

Andrew

adonoho
  • 4,339
  • 1
  • 18
  • 22
  • @adonho. I tried your solution and it seems it does not call autorelease pool anymore. What I do not understand is why under ARC this factory methods return an object that goes inside an autoreleasepool ? I do not release CFData (complaint from Analyser if I do it) but the app continue to crash. Always under investigation... – cyberserker Mar 17 '12 at 15:08
  • cyberserker, ARC does not change the retention rules for returned objects. Most objects in Cocoa are still autoreleased and not ARC released. W.r.t. your crash, I assume the autorelease trace above is now invalid. Where is it exactly crashing? Where exactly do you release `provider`? Andrew – adonoho Mar 18 '12 at 12:45
  • @adonho. Thank you for these informations about ARC. I have to update a lot of my code since I was using mainly factories since ARC! I have updated Xcode to 4.3.1 and now use LLDB. Now, the analyzer says that I have to release data ? It is what I did: `CFDataRef dataRef = (__bridge_retained CFDataRef)data; CGDataProviderRef provider = CGDataProviderCreateWithCFData(dataRef); CFRelease(dataRef);` And now all seems to work (need to do a long run to be sure). I will probably never understand what was the problem (Xcode 4.2, GCD, me ?).I'd like to add a vote to you but I cannot currently, sorry. – cyberserker Mar 18 '12 at 13:34