Tread 9:EXC_BAD_ACCESS

时间:2013-02-01 08:42:53

标签: iphone ios objective-c multithreading

我在此方法中收到此消息“Tread 9:EXC_BAD_ACCESS(code = 1,address = 0x70000010)”(但只有在下载另一个线程文件时才会创建此错误):

- (NSMutableDictionary *) getDictionaryAllStatin:(sqlite3*)database
{

    if (self._streetsArrey == nil || self._streetsArrey.count <= 0) {
        [self getArrayAllStatin:database];
    }

    /*--------------------------------------------------------------*/
    NSMutableDictionary *result1 = [[NSMutableDictionary alloc] init];

    for (StreetData *street in _streetsArrey) {
        NSString * name = [self deleteContractionWithText:street._name];
        NSArray * arr = [name componentsSeparatedByString:@" "];
        NSMutableArray *arrm = [[NSMutableArray alloc] initWithArray:arr];
        arr = nil;
        [arrm addObject:name];

            for (NSString *txt in arrm) {
                int lengthText = txt.length;
                for (int i = 2 ; i <= lengthText; i++) {
                    NSString * key = [txt substringToIndex:i];
                    key = [key lowercaseString];
                    NSMutableDictionary *isSet = [result1 objectForKey:[NSNumber numberWithInt:[key hash]]];
                    if (isSet == nil) {
                        isSet = [[NSMutableDictionary alloc]init];
                    }
                    [isSet setObject:street forKey:[NSNumber numberWithInt:street._streetId]];
                    [result1 setObject:isSet forKey:[NSNumber numberWithInt:[key hash]]];
                    isSet = nil;
                    key = nil;
                }
            }

    }

    NSMutableDictionary *result = [[NSMutableDictionary alloc] init];
    for (id key in result1) {
        NSMutableDictionary *dictionary = [result1 objectForKey:key];
        NSArray *arr = [dictionary allValues];
        [result setObject:arr forKey:key];
        arr = nil;
        [dictionary removeAllObjects];
        dictionary = nil;

    }
    [result1 removeAllObjects];
    result1 = nil;
    /*--------------------------------------------------------------*/
    if (result.count > 0) {
        _streetsDictionary = result;
        result = nil;
        return _streetsDictionary;
    }else {
        _streetsDictionary = nil;
        return nil;
    }
}

enter image description here

enter image description here

为什么我收到此消息?

我该如何解决?

1 个答案:

答案 0 :(得分:2)

崩溃的最可能原因是尝试访问已经解除分配的对象。

因为似乎失败就出现了:

  NSMutableDictionary *isSet = [result1 objectForKey:[NSNumber numberWithInt:[key hash]]];

我建议将语句拆分为其组件,以尝试追踪哪个对象实际上可能是罪魁祸首:

 NSInteger h = [key hash];
 NSNumber n = [NSNumber numberWithInt:h];
 ...
  

但是只有在下载另一个线程文件时才会创建此错误

另外,检查下载代码和崩溃代码是否有任何共同之处。前者可能导致第二个中使用的对象的释放。

希望它有所帮助。