Dealloc UILabel

时间:2011-09-15 20:14:35

标签: iphone objective-c dealloc

这是我的代码

- (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;
}

为什么如果我添加到我的代码[labelTitle release]或[desc release],应用程序会崩溃?

1 个答案:

答案 0 :(得分:3)

你在这个问题上有很多代码。

labelTitle从我看到的代码看起来很好,但是desc是一个自动释放的对象。

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

它的保留计数为+1,但它已被自动释放,这将减少在某个未来某个时间点保留计数(通常在变量超出范围后不久)。所以,没有理由发布它。

您可以阅读有关NSAutoReleasePool on apple's docs的更多信息。

您还可以查看How does the NSAutoreleasePool autorelease pool work?