views:

83

answers:

2

My app has a navigation controller and two views, firstController and secondController. firstController has a webView that displays an html page with links, and clicking any link will take the user to secondController. This is where the program stops by stepping through the debugger.

See code below.

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        secondController *nextController = [[secondController alloc] init];

        [self.navigationController pushViewController:nextController animated:YES];
        [nextController release];

        return NO;
    }
    return YES;
}

This works fine except for when I navigate from firstController to secondController by clicking any link on firstController the third time, the application just exits.(firstController link click, secondController back button, firstController link click, secondController back button, firstController link click and the application crashes)

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSCFSet length]: unrecognized selector sent to instance 0x251f100'

This is so strange. I've tried everything but still couldn't figure out what went wrong.

A: 

Use NSSet count if you want to know how many elements your set has

vodkhang
+2  A: 

You have a memory problem, where some object is sent the length message, but that object is long gone and has it's memory occupied by a NSCFSet object. There's the explanation for the error. Now for the bug.

You might want to try not to release nextController so quickly, but wait a little longer; use autorelease so nextController stays alive at least until the moment your app is returning to some idle mode. So:

secondController *nextController = [[[secondController alloc] init] autorelease];

Otherwise, delve into the inner workings of secondController.

mvds
Fixed. It turns out that I released a NSString in secondController. NSStrings don't need to be released right?
Fasid
They do, but only if *you* alloc or retain them! (so, no, normally they don't)
mvds
(Or `copy` them.)
Wevah
when would you want to alloc them? I don't see the difference to just = @"something"
Fasid
For example, if you insist on using NSStrings in a loop while adding 1 character a gazillion times, you don't want autoreleased NSStrings for *every* iteration - they will not be released until after your loop! In such a case, you'd might want to alloc and release the NSStrings yourself. (or, create your own local autoreleasepool)
mvds
i see. thank you.
Fasid