0

iPhone初心者ですが、

ここでアップルが提案するように NSURLConnection を作成しています が、ビューを閉じるとアプリがクラッシュしNSZombieEnabledます。-[CALayer release]: message sent to deallocated instance 0x68b8f40

に Web ページを表示していWebviewます。ユーザーが Web ビューのダウンロード リンクをクリックすると、shouldStartLoadWithRequest作成中のこのメソッド内でメソッドが呼び出されますNSURLConnection

これが私のコードスニペットです。

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data1
{
    [receivedData appendData:data1];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);

    DirPath=[self applicationDocumentsDirectory];

     NSLog(@"DirPath=%@",DirPath);
    [receivedData writeToFile:DirPath atomically:YES];

    UIAlertView* Alert = [[UIAlertView alloc] initWithTitle:@"Download Complete !"
                                                         message:nil delegate:nil 
                                               cancelButtonTitle:@"OK"
                                               otherButtonTitles:nil];
    [Alert show];
    [Alert release];


    // release the connection, and the data object
    [connection release];
    [receivedData release];
}


- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error1
{
    [connection release];
    [receivedData release];

    // inform the user
    NSLog(@"Connection failed! Error - %@ %@",
          [error1 localizedDescription],
          [[error1 userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {


    //CAPTURE USER LINK-CLICK.

            Durl=[[url absoluteString]copy];

            //Checking for Duplicate .FILE at downloaded path....

            BOOL success =[[NSFileManager defaultManager] fileExistsAtPath:path];
            lastPath=[[url lastPathComponent] copy];

            if (success) //if duplicate file found...
            {
                UIAlertView* Alert = [[UIAlertView alloc] initWithTitle:@"This FILE is already present in Library."
                                                                     message:@"Do you want to Downlaod again ?" delegate:self 
                                                           cancelButtonTitle:nil
                                                           otherButtonTitles:@"Yes",@"No",nil];
                [Alert show];
                [Alert release];

            }
            else  //if duplicate file not found directly start download...
            {
                // Create the request.
                NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:Durl]
                                                          cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                      timeoutInterval:60.0];

                // create the connection with the request and start loading the data
                NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
                if (theConnection) {
                    // Create the NSMutableData to hold the received data.
                    receivedData = [[NSMutableData data] retain];
                } else {
                    NSLog(@"Inform the user that the connection failed."); 
                }

    return YES;   
}

どんな助けでも大歓迎です。

4

2 に答える 2

0

dealloc の実装で、 を にリセットしていることを確認してdelegateくださいnil。そうするための別の場所はviewWillDisappear:.

アプリがクラッシュしたり、ゾンビにアクセスしたりする理由は、UIWebView既に割り当てが解除されているにもかかわらず、インスタンスが viewController をコールバックしようとしている可能性があるためです。それを防ぐには、viewController が範囲外になる前に、delegateその をUIWebView元に戻す必要があります。nilこれは、iOS5 の ARC 実装より前に委任を使用する場合の一般的な問題です。iOS5 はついに弱い参照を提供しnilます。インスタンスの割り当てが解除されると、それらは実際にはそれ自体になります。

例 A:

- (void)dealloc
{
    [...]
    _webView.delegate = nil;
}

例 B:

- (void)viewWillDisappaer:(BOOL)animated
{
    [super viewWillDisappaer:animated];
    _webView.delegate = nil;
}

編集:あなたの質問をもう一度読んだ後、メッセージが CALayer に送信されるため、ゾンビが UIView または UIControl であることに気付きました。webView に関連するすべてのコードを一時的に削除して、問題が実際に Web ビューに関連していることを確認してください。

于 2012-08-23T06:30:35.987 に答える
0

私はこれに問題があると思います NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

NSURLConnectionビューを閉じると、すべてがそのビューに表示されますdeallocated。しかし、NSURLConnectionデリゲート メソッドを呼び出そうとすると、クラッシュが発生します。

したがって、これを行うにはデリゲートNSURLConnectionを nil に設定する必要があり、インターフェイスでviewWillDisappearオブジェクトを作成する必要がありNSURLConnectionます。

@interface yourClass
{
   NSURLConnection *theConnection;
}
@end

メートルで

theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

- (void)viewWillDisappaer:(BOOL)animated
{
    [super viewWillDisappaer:animated];
    theConnection.delegate = nil;
}
于 2012-08-23T16:19:11.657 に答える