views:

937

answers:

7

I'm getting a EXC_BAD _ACCESS after leaving the method below. At that point htmlDocument becomes invalid, which it should since it falls out of scope. But is that why I'm getting the error? By the time the contentView (UIWebView) loads, htmlDocument is gone. But doesn't contentView already have what it needs from loadHTMLString?

- (void)viewDidLoad {
[super viewDidLoad];
//something other processing here

NSString *htmlDocument = [NSString stringWithFormat:@"<html> \n"
"<body>%@</body> \n"
"</html>", aboutContent];
[contentView loadHTMLString:htmlDocument baseURL:[NSURL URLWithString:@"http://www.abc.com"]];

}

Is there a better way to follow this all the way to failure? Leaving this method is the end of the line for my code. SDK stuff from there.

A: 

You say that EXC_BAD_ACCESS occurs after leaving this function - do you mean it occurs after you step out or at sometime after this function exits?

Andrew Grant
The latter. I can step out ok. Any attempt to keep stepping just keeps the debugger on the same line. I continue and splat!
4thSpace
A: 

At a glance I would say there is nothing wrong with this code, assuming 'aboutContent' is a valid pointer to an object.

Chris Lundie
It is a valid pointer as it has an address. Something down the line falls apart though. But I'm not involved at that point. This error is usually related to some memory issue. I definitely somehow caused it.
4thSpace
A: 

You can try running your app with Guard Malloc on the simulator, but that's not guaranteed to turn up anything. I'd suggest you just start commenting out statements until you find the one that's causing the error.

Brent Royal-Gordon
A: 

I've the problem but am not clear on a solution. The following is on RootViewController and loads the next view, which is where the error occurs...I think. Not exactly clear on that either.

-(void)showAbout:(id)sender{
UIBarButtonItem *button = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil action:nil] autorelease];
self.navigationItem.backBarButtonItem = button;
//causes about view to crash.
//[button release];
AboutViewController *aboutView = [[AboutViewController alloc] initWithNibName:@"About" bundle:nil];
[delegate.navigationController pushViewController:aboutView animated:YES];
[aboutView release];

}

Following memory management rules, I release button since I alloc'd it. Why does this cause EXC__BAD_ ACCESS? The scope of button is only within the method. I tried putting its release as the last line above but that didn't help.

4thSpace
For the purpose of the memory management rules, "autorelease" is the same as "release". (It really just means "release later".) So you effectively released the button twice, which will certainly cause a crash.
Brent Royal-Gordon
A: 

It's not clear what's going on with the code snippet you've just provided without more context. That said, it looks like all you want to do is load some HTML locally in the device. Why not just do this?

- (void)viewDidLoad {
    [webView loadRequest:[NSURLRequest 
                          requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] 
                                                pathForResource:@"help/about.html"
                                                ofType:@"html"]isDirectory:NO]]];
}

Using this technique, you can author HTML-based documents right in your phone without the user being wise to the fact that they are actually looking at a web view: this includes Javascript, CSS, the whole ball of a wax. I've seen people actually have their iPhone app go out to the Internet just to render a static page on the Internet which really is not necessary.

hyuan
A: 

From your sample code in the second post, the button you release UIBarButtonItem *button is not retained by anything following the release you have commented out - and so it is deallocated.

You need to add the button to another view (using adSubview) in order for it to display and then you can release it. The parent view will retain the button.

Of course if you are going to refer to the button again, your view controller should keep the retain and release the button in its dealloc.

Roger Nolan
+1  A: 

From your second post, the line you have commented out ([button release]) releases an object already marked to be released automatically.

Either remove the "autorelease" where you are doing an alloc for the button or remove the [button release] statement for the code to compile without errors and exceptions.

If an object is marked for autorelease, calling a release on it will be the same as calling a release on a deallocated instance, hence the error.

lostInTransit