2012-03-19 19 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を一度割り当てて一度だけ解放します。

更新日以下のクラスを完全に見つけてください。

#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変数の割り当てと解放に問題はありません。ウェブビューにマッピングされた保持されたプロパティを作成したのでしょうか?もしそうでなければ、何か他のものがクラッシュを引き起こしているに違いない。 –

+0

@PhilippeLeybaertは、欠落しているコードの量を見て、実際のコードでは多くのことが間違っている可能性があります。 – mvds

+0

私が知っているのは、変数のalloc/releaseに何も問題がないと言いました。 –

答えて

7

この行はドキュメントにありますか?あなたは 、デリゲートを設定していたためのUIWebViewのインスタンスを解放する前に重要

には、最初にnilにそのデリゲートプロパティを設定する必要があります。この は、たとえばdeallocメソッドで行うことができます。

"関連コード"に代理人の設定が表示されていませんが、関連するコードが完全ではありません。 (flex?buttonDone?buttonAction?)

良い答えが必要な場合は、すべてコードを共有してください。

+0

私はちょうど元の質問の完全なクラスを貼り付けました。 – 0xSina

関連する問題