Several Objective-C objects become Invalid for no reason, sometimes.

Posted by farnsworth on Stack Overflow See other posts from Stack Overflow or by farnsworth
Published on 2010-04-06T20:24:37Z Indexed on 2010/04/07 3:23 UTC
Read the original article Hit count: 279

Filed under:
|
- (void)loadLocations {
    NSString *url = @"<URL to a text file>";
    NSStringEncoding enc = NSUTF8StringEncoding; 
    NSString *locationString = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:url] usedEncoding:&enc error:nil];
    NSArray *lines = [locationString componentsSeparatedByString:@"\n"];

    for (int i=0; i<[lines count]; i++) {
        NSString *line = [lines objectAtIndex:i];
        NSArray *components = [line componentsSeparatedByString:@", "];
        Restaurant *res = [byID objectForKey:[components objectAtIndex:0]];

        if (res) {
            NSString *resAddress = [components objectAtIndex:3];
            NSArray *loc = [NSArray arrayWithObjects:[components objectAtIndex:1], [components objectAtIndex:2]];
            [res.locationCoords setObject:loc forKey:resAddress];   
        }
        else {
            NSLog([[components objectAtIndex:0] stringByAppendingString:@" res id not found."]);
        }
    }
}

There are a few weird things happening here. First, at the two lines where the NSArray lines is used, this message is printed to the console-

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFDictionary count]: method sent to an uninitialized mutable dictionary object'

which is strange since lines is definitely not an NSMutableDictionary, definitely is initialized, and because the app doesn't crash.

Also, at random points in the loop, all of the variables that the debugger can see will become Invalid. Local variables, property variables, everything. Then after a couple lines they will go back to their original values. setObject:forKey never has an effect on res.locationCoords, which is an NSMutableDictionary. I'm sure that res, res.locationCoords, and byId are initialized.

I also tried adding a retain or copy to lines, same thing. I'm sure there's a basic memory management principle I'm missing here but I'm at a loss.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about iphone-sdk