views:

362

answers:

1

Hello,

I have pushed view controller and load WebView and Custom rectangular rounded button on right down left corner into view using programmatic way.

-(void)loadView {


        CGRect frame = CGRectMake(0.0, 0.0, 480, 320);
    WebView = [[[UIWebView alloc] initWithFrame:frame] autorelease];
    WebView.backgroundColor = [UIColor whiteColor];
    WebView.scalesPageToFit = YES;
    WebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin);
    WebView.autoresizesSubviews = YES; 
    WebView.exclusiveTouch = YES;

    WebView.clearsContextBeforeDrawing = YES;

 self.roundedButtonType = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
 self.roundedButtonType.frame = CGRectMake(416.0, 270.0, 44, 19);
 [self.roundedButtonType setTitle:@"Back" forState:UIControlStateNormal];
 self.roundedButtonType.backgroundColor = [UIColor grayColor];
 [self.roundedButtonType addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];

 self.view = WebView;

 [self.view addSubview: self.roundedButtonType ]; 
 [WebView release]; 


}

This is action that I have added as back button of navigation.

-(void)back:(id)sender{
 [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewDidUnload{
 self.WebView = nil;
 self.roundedButtonType = nil;
}

-(void)dealloc{
 [roundedButtonType release];
 [super dealloc];
}

Here, When Back button click then it is showing previous view but application got stuck in that view and GDB shows Program received signal :EXC_BAD_ACCESS message.

how resolve this issue?

Thanks,

A: 

You have -autorelease'd WebView here:

WebView = [[[UIWebView alloc] initWithFrame:frame] autorelease];

but then you -release it again!

 [self.view addSubview: self.roundedButtonType ]; 
 [WebView release]; 

Try to remove one of the releases.


Also,

 self.roundedButtonType = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];

unless roundedButtonType is declared as @property(assign,...), you don't need to send the -retain message. And it's better to setup the frame, title, etc. before assigning to self.roundedButtonType, because every call to self.roundedButtonType is not free.

KennyTM
Thanks, Kenny I have removed one of autorelease to WebView and it's work in my case fine. Does [WebView release] will release http request also or just unload data from WebView and connection to URL is active ? I will also take care of setup frame and title before assigning.Thasnk
TechFusion
@Tech: Everything the web view own will be `-release`'d when the web view is `-dealloc`'ed (note release ≠ deallocate). In your case, `self` will `-retain` the web view when assigning to `self.view`, so `-release`'ing it will not kill the connections.
KennyTM
Hello Kenny,I am also making self.WebView = nil in ViewDidUnload(). so to kill connection should I need to add following in dealloc task ?-(void)dealloc{ [super dealloc] [self.WebView dealloc] //Is this correct?}Thanks,
TechFusion
@Tech: No! Don't call `-dealloc` directly. If you really need to kill the connections, call `[webView stopLoading]`.
KennyTM
Thanks,Kenny for your prompt reply, Where should I call [WebView stopLoading] in -ViewDidUnload or -dealloc task ?Thanks,
TechFusion
@Tech: `-viewDidUnload` if you need to kill it early. Normally, it doesn't matter if you close it or not.
KennyTM