2012-03-19 81 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