iphone - objective-c 'double free' error in ARC mode -
i'm trying practice block , gcd/nsoperation, wrote project async-objc, when i'm trying test unit test, encountered 'double free' error, thought there shouldn't such error in arc mode.
here code
- (void)each:(nsarray *)items iterator:(callbackeach)iterator complete:(callbackwitherror)complete { nsblockoperation *blockop = [nsblockoperation blockoperationwithblock:^{ if (!items || items.count == 0) { complete(nil); return; } callbackwitherror __block completeonce = complete; nsoperationqueue *queue = [[nsoperationqueue alloc] init]; nslog(@"start operations %@", queue); [queue setmaxconcurrentoperationcount:kmaxconcurrentoperationcount]; nsinteger __block count = 0; (id item in items) { nsoperation *op = [nsblockoperation blockoperationwithblock:^{ iterator(item, ^(nserror *error) { if (error) { completeonce(error); completeonce = nil; } else { count++; if (count >= items.count) { completeonce(nil); completeonce = nil; } } }); }]; [queue addoperation:op]; } [queue waituntilalloperationsarefinished]; nslog(@"all operations down %@", queue); }]; [_mainqueue addoperation:blockop]; }
this test code:
- (void)testeachwithbigdata { bool __block completed = false; nsmutablearray *items = [nsmutablearray arraywithcapacity:100]; (int = 1; < 10000; ++i) { [items addobject:[nsstring stringwithformat:@"%d", i]]; } nsmutablearray *result = [nsmutablearray arraywithcapacity:100]; [_async each:items iterator:^(id item, callbackwitherror callback) { //nslog(@"======item %@", item); nsnumber *num = [nsnumber numberwithint:[item integervalue]]; //nslog(@"number %@", num); [result addobject:num]; callback(nil); } complete:^(nserror *error) { dispatch_async(dispatch_get_main_queue(), ^{ stassertnil(error, @"there should no error"); [result sortusingcomparator:^nscomparisonresult(id obj1, id obj2) { nsnumber *num1 = (nsnumber *)obj1; nsnumber *num2 = (nsnumber *)obj2; if ([num1 integervalue] > [num2 integervalue]) { return nsordereddescending; } else if ([num1 integervalue] < [num2 integervalue]) { return nsorderedascending; } return nsorderedsame; }]; nsinteger index = 1; (nsnumber *num in result) { stassertequals(index, [num integervalue], @"the value should ordered"); index++; } completed = yes; }); } ]; nsdate *until = [nsdate datewithtimeintervalsincenow:10]; while (!completed && [until timeintervalsincenow] > 0) { [[nsrunloop currentrunloop] runmode:nsdefaultrunloopmode beforedate:until]; } }
if couldn't reproduce error, increase test item count 10000.
(int = 1; < 10000; ++i) { [items addobject:[nsstring stringwithformat:@"%d", i]]; }
and comment nslog() command + u run unit test
here error message: otest(11105,0xb039f000) malloc: * error object 0xa0f8000: pointer being freed not allocated
otest(11209,0xb0115000) malloc: * error object 0x41dcc00: pointer being freed not allocated * set breakpoint in malloc_error_break debug
i cannot reproduce 'double free' error time
Comments
Post a Comment