Fasid
Fasid

Reputation: 255

Strange Objective-C problem

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.

Upvotes: 2

Views: 517

Answers (2)

mvds
mvds

Reputation: 47034

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.

Upvotes: 2

vodkhang
vodkhang

Reputation: 18741

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

Upvotes: 0

Related Questions