Objective c NSMUTABLEARRY在执行块的中途获得dealloc
我有一个Objective c NSMUTABLEARRY在执行块的中途获得dealloc,objective-c,malloc,automatic-ref-counting,Objective C,Malloc,Automatic Ref Counting,我有一个NSMutableArray在enumerateObjects循环中填充值。大约在第四次或第五次调用填充可变数组的函数时,堆栈出现SIGSEGV错误 0 libobjc.A.dylib 0x39e7e626 objc_msgSend + 6 1 CoreFoundation 0x2f663dc9 -[__NSArrayM dealloc] + 154 2 libobjc.A.dylib 0x39e83b6b _ZN11objc_object17sidetable_releas
NSMutableArray
在enumerateObjects循环中填充值。大约在第四次或第五次调用填充可变数组的函数时,堆栈出现SIGSEGV
错误
0 libobjc.A.dylib 0x39e7e626 objc_msgSend + 6
1 CoreFoundation 0x2f663dc9 -[__NSArrayM dealloc] + 154
2 libobjc.A.dylib 0x39e83b6b _ZN11objc_object17sidetable_releaseEb + 172
3 MyAppName 0x0004bdff -[BluetoothManager(DataParse) processResponse:] (BluetoothManager+DataParse.m:231)
设置
appDelegate
,它具有指向BluetoothManager
实例的强指针processResponse:
,每次在外围委托上调用didUpdateValueForCharacteristic:
时都会调用该签名processBleResponse:
获取NSData blob并将其转换为NSNumber数组processresponse:
时,存储所有NSNumber的可变数组在其自己的enumerateObjects循环中被malloc'd
我用一个调用processResponse的重复NSTimer模拟了测试:每隔30-40秒使用一些虚拟数据,但在循环的每几次迭代后,错误是可再现的。应用程序的总内存使用量没有超过45MB
// method on BluetoothManager class
- (void)processBleResponse:(NSData *)myData
{
__block float baseValue = 34.500;
__block float incrementValue = 0.0500;
// convert hex data into array of 10 binary "strings"
NSArray *individualBinaryArray = [self extractBinaryValues:myData];
// Extract temperature fields
NSIndexSet *relevantIndices = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, individualBinaryArray.count - 1)];
__block NSUInteger indexOfFailure = NSNotFound;
NSMutableArray *decimalValues = [[NSMutableArray alloc] init];
[individualBinaryArray enumerateObjectsAtIndexes:relevantIndices options:NSEnumerationConcurrent usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSString *hexString = [self hexString:(NSString *)obj];
if (hexString.length)
{
NSScanner *decimalScanner = [NSScanner scannerWithString:hexString];
unsigned int decimalValue = 0;
if ([decimalScanner scanHexInt:&decimalValue])
{
// insert value
// THIS IS THE LINE WHERE THE CRASH OCCURS
[decimalValues addObject:@(baseValue + incrementValue * decimalValue)];
}
else
{
// throw alert/exception etc.
*stop = YES;
indexOfFailure = idx;
}
}
else
{
*stop = YES;
indexOfFailure = idx;
}
}];
// Insert list of NSNumbers into CoreData
}
我想知道问题是否是这个方法所属的对象(而不是数组)。确保不要过早地解除分配。仅供参考,我无法根据此代码示例重现崩溃。可能是。我意识到我正在使用UIImage imageNamed加载大量一次性图像,这些图像保留在内部imageIO堆栈中,浪费了9.7 MB的空间。我现在已经减少了这一点,不管怎样,我都需要更加优雅地看待失败。谢谢