Nishi
Nishi

Reputation: 703

App is crashing while releasing view controller object

My app is crashing while releasing view controller object. Here is my code.

TagCloudWebViewController *controller=[[[TagCloudWebViewController alloc]init]autorelease];
controller.htmlString=[[notification userInfo] valueForKey:@"url"];
[self.navigationController pushViewController:controller animated:YES];

This is my code from wheny above method is called

-(void)viewDidLoad{
 [super viewDidLoad]; 
[[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(openTextInWebview:) name:@"kTouchTextUrl" object:Nil];
}

and

#pragma mark - UIGestureDelegate
- (void)longPressRecognized:(UILongPressGestureRecognizer *)longPressRecognizer {
CGPoint touchPoint = [longPressRecognizer locationInView:self];
NSArray *subviews = self.subviews;
for (int i=0; i<subviews.count; i++) {
    TagView * tagLabel = (TagView *)[subviews objectAtIndex:i];

        if ( CGRectContainsPoint( [tagLabel frame], touchPoint ) ) {
            NSArray*objectArray=[[[NSArray alloc] initWithObjects:tagLabel.customLink, nil] autorelease];
            NSArray*keyArray=[[[NSArray alloc] initWithObjects:@"url",  nil] autorelease];
            NSDictionary *userInfo = [NSDictionary dictionaryWithObjects:objectArray forKeys:keyArray ];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"kTouchTextUrl" object:nil userInfo:userInfo];
            //[[UIApplication sharedApplication] openURL:[NSURL URLWithString: tagLabel.customLink]];
            break;
        }
    }
}

and this is notification method

DidLoad method

- (void) viewDidLoad {
[super viewDidLoad];

    _webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    _webView.delegate = self;
    _webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth
                                 | UIViewAutoresizingFlexibleHeight);
    _webView.scalesPageToFit = YES;
    [self.view addSubview:_webView];

    [self initSpinner];

    if (htmlString) {
        [self openURL:[NSURL URLWithString:htmlString]];
    }
}

WebView delgate method

-(void) webViewDidStartLoad:(UIWebView *)webView {
self.navigationItem.title = @"Loading...";
[spinnerView startAnimating];  
isLoading = YES;

}

-(void) webViewDidFinishLoad:(UIWebView*)webView {
self.navigationItem.title = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];
[self performSelector:@selector(stopSpinner) withObject:nil afterDelay:0.1]; 
isLoading = NO;
}

-(void) webView:(UIWebView*)webView didFailLoadWithError:(NSError*)error {
[self webViewDidFinishLoad:webView];
[self performSelector:@selector(stopSpinner) withObject:nil afterDelay:0.1]; 
isLoading = NO;
}

Upvotes: 0

Views: 245

Answers (3)

Sanoj Kashyap
Sanoj Kashyap

Reputation: 5060

I don not see any thing that causing crashing.I think you have changed your question.As per question you not using ViewController any where.Please show more details.

Updated: Please check, you are creating autoreleased object, it might be you are releasing some where by mistake.Try to avoid autoreleased object as it remain in the Pool and later releases ,which may causes a memory issue for you.

Upvotes: 0

Nishi
Nishi

Reputation: 703

The above problem is occurs due to WebView delgate. After pressing back button the reference of the object is deallocating from memory due to this app is crashing while releasing the viewcontroller object. I did some thing like

-(void) viewDidDisappear:(BOOL)animated{
if([_webView isLoading]) {
    [_webView stopLoading];
}
[_webView setDelegate:nil];

}

due to above code my crashing has been resolved

Upvotes: 0

Rob
Rob

Reputation: 437872

Update: The following answer was in response to the original question of why the UIViewController with a UIWebView was not appearing. The OP's original theory was that it was related to some problem regarding the premature releasing of the view controller object. But, as outlined in my answer below, I suspect the problem was related to the creation of the view controller itself. Anyway, my answer to the original question follows (but it unfortunately has nothing to do with the revised question):

I personally don't suspect the problem has anything to do with the releasing of the controller. I think it is in the creation of the controller's view. There's nothing in the above code that causes the object to be released, so you problem rests elsewhere and you need to show more code. If you're concerned about your notification center stuff, try bypassing it and just add a button that does your TagCloudWebViewController alloc/init, sets htmlString and pushes, and see if that still causes your program to crash, which I suspect it will.

I notice that you're creating your controller via alloc and init, but not initWithNibNamed. According to the UIViewController Class Reference: "If you cannot define your views in a storyboard or a nib file, override the loadView method to manually instantiate a view hierarchy and assign it to the view property."

So, bottom line, either use a NIB, use a storyboard or define a loadView. Defining a viewDidLoad doesn't preclude the need for a loadView. You need to create a view for your controller, which is done for you if you use NIB or storyboard, or you have to do manually via loadView, if you don't use NIB or storyboard.

See iPhone SDK: what is the difference between loadView and viewDidLoad? for a discussion of the differences between viewDidLoad and loadView.

Upvotes: 1

Related Questions