2012-03-19 163 views
1

我有一个UIWebView的UIViewController子类。它增加了Web视图,因为它的视图子视图和dealloc释放它。以下是相关代码:UIWebView发布导致崩溃

#import "MediaVC.h" 

@implementation MediaVC 
@synthesize file, repository, server, delegate; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 


    webView = [[UIWebView alloc] initWithFrame:[[self view] bounds]]; 
    [webView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)]; 
    [webView setScalesPageToFit:YES]; 
    [webView setMultipleTouchEnabled:YES]; 

    [[self view] addSubview:webView]; 

    [self displayFile]; 


    [flex release]; 
    [buttonDone release]; 
    [buttonAction release]; 
} 


- (void)displayFile { 

    if (!mimeString) { 
     [webView loadData:nil 
       MIMEType:nil 
     textEncodingName:nil 
        baseURL:nil]; 


     [SVProgressHUD dismissWithError:@"Can't display this file." afterDelay:2]; 
    } else { 

     [[DataCenter sharedInstance] getDataForFile:file 
              server:server 
             repository:repository 
              isThumb:NO 
             completion:^(NSData *data) { 

              NSString *filenamePath =[NSString stringWithFormat:@"temp.%@", [[file path] pathExtension]]; 
              NSString *docDir = [DataCenter getDocumentsDirectoryPath]; 

              NSString *fullPath = [docDir stringByAppendingPathComponent:filenamePath]; 

              [data writeToFile:fullPath atomically:YES]; 

              [webView loadData:data 
                 MIMEType:[DataCenter mimeTypeForFile:file] 
               textEncodingName:nil 
                 baseURL:nil]; 

              if (!data) { 
               [SVProgressHUD dismissWithError:@"Error!"]; 
              } else { 
               [SVProgressHUD dismiss]; 
              } 

             } progress:^(float progress) { 
              [SVProgressHUD setFloat:progress]; 
             }]; 


    } 

} 

- (void)dealloc { 
    [buttonNext release]; 
    [buttonBack release]; 
    //[webView release]; ---CRASH HAPPENS IF I UNCOMMENT THIS LINE 
    [super dealloc]; 
} 

@end 

该应用程序在[super dealloc];上崩溃。我有NSZombieEnabled,它不会给我任何有关“错误发送到释放实例....”的错误。我发现的是,如果我注释掉[webView release],,崩溃不会发生。 我检查,我只分配一次UIWebView并释放一次。

UPDATE请在下面找到完整的课程。正如我所说,有很多在这个类,这不是相关的,在这我找不到任何问题,也许你们找到的东西:

#import "MediaVC.h" 
#import "DataCenter.h" 
#import "SVProgressHUD.h" 
#import "File.h" 

@implementation MediaVC 
@synthesize file, repository, server, delegate; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    UIBarButtonItem *buttonDone = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
                       target:self 
                       action:@selector(done)]; 

    UIBarButtonItem *buttonAction = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction 
                        target:self 
                        action:@selector(action:)]; 


    buttonNext = [[UIBarButtonItem alloc] initWithTitle:@"Next" 
                style:UIBarButtonItemStyleDone 
               target:self 
               action:@selector(next)]; 

    buttonBack = [[UIBarButtonItem alloc] initWithTitle:@"Back" 
                style:UIBarButtonItemStyleDone 
               target:self 
               action:@selector(back)]; 

    UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                      target:nil 
                      action:nil]; 


    [[self navigationItem] setLeftBarButtonItem:buttonDone]; 
    [[self navigationItem] setRightBarButtonItem:buttonAction]; 
    [[self navigationController] setToolbarHidden:NO]; 
    [self setToolbarItems:[NSArray arrayWithObjects:buttonBack, flex, buttonNext, nil] animated:YES]; 



    webView = [[UIWebView alloc] initWithFrame:[[self view] bounds]]; 
    [webView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)]; 
    [webView setScalesPageToFit:YES]; 
    [webView setMultipleTouchEnabled:YES]; 

    [[self view] addSubview:webView]; 

    [self displayFile]; 


    [flex release]; 
    [buttonDone release]; 
    [buttonAction release]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return YES; 
} 

- (void)done { 
    [delegate doneMediaVC:self]; 
} 

- (void)action:(id)button { 
    NSString *filenamePath = [file path]; 
    NSString *docDir = [DataCenter getDocumentsDirectoryPath]; 

    NSString *fullPath = [docDir stringByAppendingPathComponent:filenamePath]; 

    NSURL *url = [NSURL fileURLWithPath:fullPath]; 
    UIDocumentInteractionController *c = [UIDocumentInteractionController interactionControllerWithURL:url]; 
    BOOL success = [c presentOpenInMenuFromBarButtonItem:buttonBack animated:YES]; 
    if (!success) { 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"An application cannt be found to open this file" 
                  delegate:nil 
                cancelButtonTitle:@"OK" 
                otherButtonTitles:nil, nil]; 
     [alertView show]; 
     [alertView release]; 
    } 
} 

- (void)displayFile { 

    [self setTitle:[file name]]; 

    [SVProgressHUD showInView:[[self navigationController] view] 
         status:@"Loading..." 
      networkIndicator:YES 
        progress:YES]; 

    NSString *mimeString = [DataCenter mimeTypeForFile:file]; 


    if (!mimeString) { 
     [webView loadData:nil 
       MIMEType:nil 
     textEncodingName:nil 
        baseURL:nil]; 


     [SVProgressHUD dismissWithError:@"Can't display this file." afterDelay:2]; 
    } else { 

     [[DataCenter sharedInstance] getDataForFile:file 
              server:server 
             repository:repository 
              isThumb:NO 
             completion:^(NSData *data) { 

              NSString *filenamePath =[NSString stringWithFormat:@"temp.%@", [[file path] pathExtension]]; 
              NSString *docDir = [DataCenter getDocumentsDirectoryPath]; 

              NSString *fullPath = [docDir stringByAppendingPathComponent:filenamePath]; 

              [data writeToFile:fullPath atomically:YES]; 

              [webView loadData:data 
                 MIMEType:[DataCenter mimeTypeForFile:file] 
               textEncodingName:nil 
                 baseURL:nil]; 

              if (!data) { 
               [SVProgressHUD dismissWithError:@"Error!"]; 
              } else { 
               [SVProgressHUD dismiss]; 
              } 

             } progress:^(float progress) { 
              [SVProgressHUD setFloat:progress]; 
             }]; 


    } 

    if (![delegate canGoNext:self currentFile:file]) { 
     [buttonNext setEnabled:NO]; 
    } else { 
     [buttonNext setEnabled:YES]; 
    } 


    if (![delegate canGoPrevious:self currentFile:file]) { 
     [buttonBack setEnabled:NO]; 
    } else { 
     [buttonBack setEnabled:YES]; 
    } 
} 

- (void)back { 
    [self setFile:[delegate getPreviousFileForMediaVC:self currentFile:file]]; 
    [self displayFile]; 
} 

- (void)next { 
    [self setFile:[delegate getNextFileForMediaVC:self currentFile:file]]; 
    [self displayFile]; 
} 

- (void)dealloc { 
    [buttonNext release]; 
    [buttonBack release]; 
    //[webView release]; 
    [super dealloc]; 
} 

@end 

谢谢!

+1

webview变量的分配和释放没有任何问题。也许你创建了一个映射到webview的保留属性?如果没有,其他的事情一定会导致事故。 – 2012-03-19 18:35:18

+0

@PhilippeLeybaert查看缺少的代码量,实际代码很多可能是错误的。 – mvds 2012-03-19 18:44:55

+0

我知道,这就是为什么我说这个变量的alloc/release没有问题,而其他的可能会导致这个问题。 – 2012-03-19 18:48:58

回答

7

您是否在文档中看到此行?

重要在发布UIWebView的实例并为其设置代理之前,您必须先将其委托属性设置为nil。例如,在你的dealloc方法中可以完成这个 。

您的“相关代码”没有显示您设置委托,但是再次,您的相关代码根本没有完成。 (flex?buttonDone?buttonAction?)

请分享全部代码如果你想要一个很好的答案。

+0

我刚在原始问题中粘贴了完整的类。 – 0xSina 2012-03-20 15:54:00