关于ios:应用程序在释放视图控制器对象时崩溃

App is crashing while releasing view controller object

我的应用程序在释放视图控制器对象时崩溃。这是我的密码。

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

这是我的代码,上面的方法是什么时候调用的

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#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;
        }
    }
}

这是通知方式

DIDEAD方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- (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方法

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

}

1
2
3
4
5
6
7
8
9
10
11
-(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加载请求:请求];}


更新:下面的答案是对最初的问题的回应,即为什么带有UIWebViewUIViewController没有出现。OP最初的理论是,它与视图控制器对象过早释放有关。但是,正如下面我的回答中概述的,我怀疑问题与视图控制器本身的创建有关。不管怎样,我对原始问题的回答如下(但不幸的是,这与修改后的问题无关):

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.


以上问题是由于WebView Delgate引起的。按下后退按钮后,对象的引用将从内存中释放,因为此应用程序在释放ViewController对象时崩溃。我做了一些事

1
2
3
4
5
-(void) viewDidDisappear:(BOOL)animated{
if([_webView isLoading]) {
    [_webView stopLoading];
}
[_webView setDelegate:nil];

}

由于上面的代码,我的崩溃已经解决了。


我看不到任何导致崩溃的东西。我想你已经改变了你的问题。根据问题,你没有在任何地方使用viewcontroller。请显示更多细节。

更新:请检查,您正在创建自动释放的对象,可能是您错误地释放了一些。请尝试避免自动释放的对象,因为它仍保留在池中,稍后释放,这可能会导致内存问题。