簡體   English   中英

取消分配UILabel

[英]Dealloc UILabel

這是我的代碼

- (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版本]或[desc版本]中,應用程序崩潰?

您在該問題中有很多代碼。

從我看到的代碼中,labelTitle看起來不錯,但是desc是一個自動發布的對象。

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

它的保留計數為+1,但已自動釋放,它將在將來的某個時間點(通常在變量超出范圍后不久)遞減保留計數。 因此,沒有理由釋放它。

您可以在apple的docs上閱讀有關NSAutoReleasePool的更多信息。

您還可以查看NSAutoreleasePool自動釋放池如何工作?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM