CALayer Zombie при удалении SubView - PullRequest
1 голос
/ 28 октября 2011

У меня есть подробный вид.Одним из значений является URL-адрес, указывающий на изображение.

Если URL-адрес не равен нулю, он создает временное представление для отображения индикатора выполнения и загружает изображение с фоном NSURLConnection.Когда он будет готов, я удаляю «Просмотр прогресса», создаю и добавляю новый «Просмотр изображений» с фото, заменяющим старый режим просмотра.

Я всегда получаю значение EXC_BAD_ACCESS при попытке удалить представление с помощью [[self.view viewWithTag:1100] removeFromSuperview];.Я проверил код в каждой строке и не могу угадать, где ошибка, поэтому, возможно, я пропустил какое-то правило.

Инструменты дают мне следующую информацию:

#   Address Category    Event Type  RefCt   Timestamp   Size    Responsible Library Responsible Caller
0   0x5ded960   CALayer Malloc  1   00:10.772.601   48  UIKit   -[UIView _createLayerWithFrame:]
1   0x5ded960   CALayer Zombie  -1  00:14.720.076   0   QuartzCore  -[CALayerArray copyWithZone:]

И это код:

- (void) loadDataIntoView
{

    // Create the View without the photo
    self.nombreTextView.text = self.evento.nombre;
    self.fechaTextField.text = @"Del 12 de Junio de 2010 al 3 de Octubre de 2011";

    // Check is a photo url exists

    if (self.evento.foto) {
        // Download the photo in background
        self.fotoConnection = [NSURLConnection connectionWithRequest:
                               [NSURLRequest requestWithURL:self.evento.foto] 
                                                            delegate:self];
        // Erros are handled by the delegate methods
        // This error should never happen
        if (!self.fotoConnection) {
            NSLog(@"Error creating connection");
            exit(0);
        }
        // Create the entradilla View
        UITextView *entradillaTextView = [[UITextView alloc] init];
        entradillaTextView.frame = CGRectMake(26, 216, 280, 145);
        entradillaTextView.text = self.evento.entradilla;
        entradillaTextView.tag = 1000;
        [self.view addSubview:entradillaTextView];
        [entradillaTextView release];

        // Create the progess View
        UIView *progressView = [[UIView alloc] initWithFrame:CGRectMake(26, 76, 270, 130)];
        progressView.tag = 1100;

        // Create progressView Label
        UILabel *legendLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 270, 20)];
        legendLabel.text = @"descargando fotografía";
        legendLabel.font = [UIFont systemFontOfSize:11];
        legendLabel.textColor = [UIColor lightGrayColor];
        legendLabel.textAlignment = UITextAlignmentCenter;

        // Create progressView progressBar
        UIProgressView *auxView = [[UIProgressView alloc] initWithFrame:CGRectMake(35, 20, 200, 40)];
        self.progressBar = auxView;
        [auxView release];
        self.progressBar.tag = 1200;

        [progressView addSubview:legendLabel];
        [legendLabel release];

        [progressView addSubview:progressBar];
        [progressBar release];

        [self.view addSubview:progressView];
        [progressView release];

    } else {
        // Create the entradilla View without any foto view space on top
        UITextView *entradillaTextView = [[UITextView alloc] init];
        entradillaTextView.frame = CGRectMake(26, 76, 280, 283);
        entradillaTextView.text = self.evento.entradilla;
        entradillaTextView.tag = 1000;
        [self.view addSubview:entradillaTextView];
        [entradillaTextView release];
    }

    // Configure the ToolBar

    // Create the favourites button if evento is not favourite
    UIBarButtonItem *favouritesButton;
    if (!self.evento.isFavourite) {
        NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"29-heart" ofType:@"png"];
        UIImage *addFavouriteImage = [UIImage imageWithContentsOfFile:imagePath];
        favouritesButton = [[UIBarButtonItem alloc] initWithImage:addFavouriteImage style:UIBarButtonItemStylePlain target:self action:@selector(addFavoritosButtonPressed)];
        //        favouritesButton = [[UIBarButtonItem alloc] initWithTitle:@"+ Favoritos" style:UIBarButtonItemStyleBordered target:self action:@selector(addFavoritosButtonPressed)];
    } else {
        favouritesButton = [[UIBarButtonItem alloc] initWithTitle:@"- Favoritos" style:UIBarButtonItemStyleBordered target:self action:@selector(removeFavoritosButtonPressed)];
    }
    // Create the Web buttom
    UIBarButtonItem *webButton = [[UIBarButtonItem alloc] initWithTitle:@"Mostrar Web" style:UIBarButtonItemStyleBordered target:self action:@selector(webButtonPressed)];

    // Create the Map buttom
    UIBarButtonItem *mapButton = [[UIBarButtonItem alloc] initWithTitle:@"Mostrar Mapa" style:UIBarButtonItemStyleBordered target:self action:@selector(mapButtonPressed)];
    self.toolbarItems = [NSArray arrayWithObjects:favouritesButton, webButton, mapButton, nil];
    [favouritesButton release];
    [webButton release];
    [mapButton release];    

}

#pragma Mark -
#pragma Mark NSURLConnection Delegate

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse *resp = (NSHTTPURLResponse *) response;
    if ((resp.statusCode >= 200) && (resp.statusCode < 300)) {
        // OK status Code
        // Prepare the temp array
        self.fotoData = [NSMutableData data];

        // Get the lenght of data to download for progress bar
        self.dataSizeToDownload = resp.expectedContentLength;
        self.dataSizeDownloaded = 0;
    } else {
        // Wrong status Code
        self.fotoData = nil;
        self.progressBar = nil;
        [connection cancel];
        self.fotoConnection = nil;

        // Reconfigure the View
        // Remove the old entradilla
        [[self.view viewWithTag:1000] removeFromSuperview];
        // Create the repositioned entradilla View
        UITextView *entradillaTextView = [[UITextView alloc] init];
        entradillaTextView.frame = CGRectMake(26, 76, 280, 283);
        entradillaTextView.text = self.evento.entradilla;
        entradillaTextView.tag = 1000;
        [self.view addSubview:entradillaTextView];
        [entradillaTextView release];

    }
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.fotoData appendData:data];

    self.dataSizeDownloaded = self.dataSizeDownloaded + (float)data.length;
    if ( self.dataSizeToDownload != 0) {
        float prog = (float)( self.dataSizeDownloaded / self.dataSizeToDownload );
        [self performSelectorOnMainThread:@selector(updateProgressBarWithValue:) withObject:[NSNumber numberWithFloat:prog] waitUntilDone:NO];
    }    
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // If no error create a new View and Reload the data
    self.fotoConnection = nil;
    if (self.fotoData) {

        self.progressBar = nil;

// !!!!!!!!! THIS IS THE EXC_BAD_ACCESS !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        [[self.view viewWithTag:1100] removeFromSuperview];

        UIImageView *fotoImageView = [[UIImageView alloc] initWithImage:
                                      [UIImage imageWithData:self.fotoData]];            
        fotoImageView.frame = CGRectMake( 26, 76, 270, 130);
        fotoImageView.contentMode = UIViewContentModeScaleAspectFit;
        [self.view addSubview:fotoImageView];


        [fotoImageView release];        
        [self.view reloadInputViews];


    } else {
        // Error downloading foto
    }

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    self.fotoConnection = nil;
    self.fotoData = nil;
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:error.description message:nil delegate:nil cancelButtonTitle:@"aceptar" otherButtonTitles:nil];
    [alert show];
    [alert release];
}


- (void) updateProgressBarWithValue:(NSNumber *)value
{
    float progress = [value floatValue];
    NSLog(@"Progress %f", progress);
    //self.progressBar.progress = progress;
}

1 Ответ

1 голос
/ 29 октября 2011

Наконец-то я понял это сам.Я просто ухожу из приложения на несколько часов, а затем я обнаружил [progressBar release], которого там быть не должно, поскольку я объявил это как свойство.Это не соответствовало другому коду, где я просто объявлял локальные переменные, поэтому я не осознавал каждый раз, когда проверяю код.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...