Search code examples
iphoneobjective-cdealloc

Dealloc UILabel


This is my code

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index
{
    MWFeedItem *item = [reader.feedItems objectAtIndex:index];

    //INIZIALIZZO L'ARRAY CARICANDOLO DAL FILE!!!!
    //[reader.feedItems initWithContentsOfFile:[[NSDictionary alloc] initWithContentsOfFile:@"Library/NewsPad"]];

    //create a numbered view
    UIView *view = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"page_iPhone_Vertical.png"]] autorelease];

    CGRect frame = CGRectMake(40, 118, 228, 100);

    UILabel *labelTitle = [[UILabel alloc] initWithFrame:frame];
    labelTitle.backgroundColor = [UIColor clearColor];
    labelTitle.textAlignment = UITextAlignmentLeft;
    labelTitle.font = [UIFont fontWithName:@"Helvetica-Bold" size:12];
    labelTitle.numberOfLines=3;
    [view addSubview:labelTitle];

    labelTitle.text = item.title;

    [view addSubview:labelTitle];

    /* DATA
     RSSEntry *entry = [_allEntries objectAtIndex:index];

     NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
     [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
     [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
     NSString *articleDateString = [dateFormatter stringFromDate:entry.articleDate];
     */

    NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    NSInteger loadImage=[settings objectForKey:@"loadImage"];

    /******** Get the image **********/
    NSString *url = [self getFirstImage:item.summary];
    //NSString *url = item.image;

    if (loadImage != 0 && url != nil) {
        //Create a managed image view and add it to the cell (layout is very naieve)

        image = [[EGOImageView alloc] initWithPlaceholderImage:[UIImage imageNamed:@"loading.png"]];
        image.frame = CGRectMake(45, 200, 210, 150);
        [view addSubview:image];
        image.imageURL = [NSURL URLWithString:url];

        /* OLD!!!
         HJManagedImageV *image;
         image = [[[HJManagedImageV alloc] initWithFrame:CGRectMake(45, 200, 210, 150)] autorelease];
         image.tag = 999;

         [view addSubview:image];

         //set the URL that we want the managed image view to load
         image.url = [NSURL URLWithString:url];

         //tell the object manager to manage the managed image view, 
         //this causes the cached image to display, or the image to be loaded, cached, and displayed
         [objMan manage:image];
         */

        frame= CGRectMake(40, 205, 230, 400);
        UILabel *desc = [[[UILabel alloc] initWithFrame:frame] autorelease];
        desc.numberOfLines=6;
        desc.backgroundColor = [UIColor clearColor];
        //desc.textAlignment = UITextAlignmentCenter;
        desc.font = [desc.font fontWithSize:12];
        [view addSubview:desc]; 
        //SETTO DESCRIPTION

        //rimuovo tag html
        NSString *descrizione=[item.summary stringByConvertingHTMLToPlainText];
        /* DEBUG
         NSString *descrizione=item.summary;

         */
        [desc setText:descrizione];

        //NSLog(item.summary);    
    }
    else {
        frame= CGRectMake(40, 90, 235, 400);
        UILabel *desc = [[[UILabel alloc] initWithFrame:frame] autorelease];
        desc.numberOfLines=15;
        desc.backgroundColor = [UIColor clearColor];
        //desc.textAlignment = UITextAlignmentCenter;
        desc.font = [desc.font fontWithSize:12];
        [view addSubview:desc]; 
        //SETTO DESCRIPTION
        //rimuovo tag html
        NSString *descrizione=[item.summary stringByConvertingHTMLToPlainText];
        /* DEBUG
         NSString *descrizione=item.summary;
         */
        [desc setText:descrizione];  
    }

    return view;
}

Why If I add to my code [labelTitle release] or [desc release] the app crashs?


Solution

  • You have a lot of code in that question.

    labelTitle looks fine from the code I see, but desc is an autoreleased object.

    UILabel *desc = [[[UILabel alloc] initWithFrame:frame] autorelease];
    

    It has a retain count of +1, but it has been autoreleased which will decrement that retain count at some future point in time (typically shortly after the variable goes out of scope). So, there is no reason to release it.

    You can read more about NSAutoReleasePool on apple's docs.

    You can also look at How does the NSAutoreleasePool autorelease pool work?