NSMutableArray在块中途获得dealloc

时间:2015-01-17 01:02:55

标签: objective-c malloc automatic-ref-counting

我有一个NSMutableArray在enumerateObjects循环中填充了值。大约第4或第5次填充MutableArray的函数被调用,我的堆栈出现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)    

设置

  1. 一个appDelegate,它有一个指向BluetoothManager实例的强指针。
  2. BluetoothManager类上的函数调用,BluetoothManager上的签名为processResponse:,每次在外围代理上调用didUpdateValueForCharacteristic:时都会调用该函数。
  3. processBleResponse:获取NSData blob并将其转换为NSNumbers数组
  4. 我注意到第4次左右调用processReponse:,我存储所有NSNumber的可变数组在其自己的enumerateObjects循环中得到了malloc。

    我使用重复的NSTimer来模拟测试,该NSTimer调用processResponse:每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
    }    
    

0 个答案:

没有答案