0

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?

Matt Bishop
  • 1,010
  • 7
  • 18
paul_1991
  • 245
  • 4
  • 13

1 Answers1

3

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?

Community
  • 1
  • 1
Sam
  • 26,946
  • 12
  • 75
  • 101
  • thanks for the answer sam!! So if I understand only system can release an autorelase object? – paul_1991 Sep 15 '11 at 20:21
  • All threads have (or should have) an NSAutoReleasePool*. When you call `autorelease` message on an object, it gets added to this auto release pool. Every so often the pool 'drains' or cleans up all the objects that were autoreleased. The main thread has a message pump that processes UIEvents and will drain this pool between UIEvents. This is how I understand it. – Sam Sep 15 '11 at 20:26
  • Thank yuo so much Sam... So all other objects in my code are correctly deallocated? The view the was returned by my function was automatically deallocated by the system? – paul_1991 Sep 15 '11 at 20:28
  • YES, you should not release an 'autoreleased object'. Autoreleasing an object will NOT decrement the retain count, but that's part of the point. You want to release it once sometime in the future. This is why when you return objects from functions you autorelease it (because you don't want to leak memory). It is up to the caller of the function to retain it, otherwise the object will get cleaned up after it leaves scope when it's owning NSAutoReleasePool drains. – Sam Sep 15 '11 at 20:30
  • Looks like you call `[view addSubview:labelTitle];` twice. Not sure if this would cause bad behavior or not, but seems weird to add it twice. If you want it in twice, copy it and add the copy. Also looks like you either need to release `labelTitle` after you're done using it, or autorelease it so that you don't have to release it. – Sam Sep 15 '11 at 20:34
  • Thanks Sam for all your help! Now I undestand everything! – paul_1991 Sep 15 '11 at 20:36