0

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;
}
  • (void) openURL:(NSURL*)URL { NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:URL]; [_webView loadRequest:request]; }
Nishi
  • 683
  • 1
  • 12
  • 31
  • 2
    You might want to consider switching to ARC. Anyway: in your code `controller` is autoreleased - if you release it also later in the code it is bound to crash. – Rok Jarc Aug 23 '12 at 06:53
  • I am not getting your point please explain. – Nishi Aug 23 '12 at 07:03
  • what is displayed in console while app crashed? can you add that in question then it would be better for answering – iMeMyself Aug 23 '12 at 07:13
  • *** -[TagCloudWebViewController respondsToSelector:]: message sent to deallocated instance 0x8595b90 getting this error message on console – Nishi Aug 23 '12 at 08:08
  • @Rob I had also tried with release still I am getting crashing – Nishi Aug 23 '12 at 08:10
  • Yes. I tried release statement at that time autorelease is not there.... Still I am getting crashing issue. What should I do? – Nishi Aug 23 '12 at 08:25
  • @Rob Yes you are right. Yes htmlString is only string not an IBoutlet. – Nishi Aug 23 '12 at 08:27
  • No I didnt define my viewcontroller via NIB. And also I am using NSNotification method to call this viewcontroller. I think some reference is calling it after deallocing the viewcontroller object.How should I release the Nsnotification reference? – Nishi Aug 23 '12 at 08:35
  • @Rob I had edited my code... Just check it out and help me on this – Nishi Aug 23 '12 at 08:51
  • I had used didLoad method rather than loadView, Just edited the code please check it out – Nishi Aug 23 '12 at 09:02

3 Answers3

1

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.

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Crashing issue solved till webview is not loaded after loading of web view its crashes and giving same mesage *** -[TagCloudWebViewController respondsToSelector:]: message sent to deallocated instance 0x8595b90 – Nishi Aug 23 '12 at 09:43
  • @Nishi If I understand you correctly, the view now shows up fine, but it crashes when the load is done. If so, you'll obviously have to share your code for your `UIWebViewDelegate` methods. (By the way, you have radically changed your question, which is to be discouraged because it invalidates all answers (making it hard for other people who stumble along later to figure out what's going on because the answers will not make any sense when considering the newly revised question). In the future, you might want to refrain from radical changes to your questions.) – Rob Aug 23 '12 at 10:19
  • Solved my issue. It is happening due to WebView Delegate.I had used webvie.delgate=nil on viewWillDisapper method. Crashing issue solve.Thanks to all – Nishi Aug 24 '12 at 07:09
0

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.

Sanoj Kashyap
  • 5,020
  • 4
  • 49
  • 75
0

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

Nishi
  • 683
  • 1
  • 12
  • 31