0

Hi i have problem with my app with memory leak on UILabel in cells on table view. Every time if I enter to table view and back, memory rise with 2mb, and if I enter to that view again it rise with next 2mb. Why I know that is because of UILabel : - I checked it with profiler:zombies and - If I remove UILabels from cell, I hadn't memory leaks

For start I defined my cell:

@interface ExpandableTableViewCell : UITableViewCell{
    BOOL hasData;

    NSString *documentTypeId;
    NSString *documentId;
    UILabel *label;
    UILabel *dayAndMonthLabel;
    UILabel *yearLabel;
    UIButton *iconButton;

    UILabel *firstLineLabel;
    UILabel *firstLineValue;

    UILabel *secondLineLabel;
    UILabel *secondLineValue;

    UILabel *thirdLineLabel;
    UILabel *thirdLineValue;

    UILabel *fourthLineLabel;
    UILabel *fourthLineValue;

    UILabel *fifthLineLabel;
    UILabel *fifthLineValue;
}

- (void) setLabelValue: (NSString*) value;
- (void) setDayAndMonthValue: (NSString*) value;
- (void) setYearValue: (NSString*) value;
- (void) setFirstLineLabelValue: (NSString*) value;
- (void) setFirstLineValue: (NSString*) value;
- (void) setSecondLineLabelValue: (NSString*) value;
- (void) setSecondLineValue: (NSString*) value;
- (void) setThirdLineLabelValue: (NSString*) value;
- (void) setThirdLineValue: (NSString*) value;
- (void) setFourthLineLabelValue: (NSString*) value;
- (void) setFourthLineValue: (NSString*) value;
- (void) setFifthLineLabelValue: (NSString*) value;
- (void) setFifthLineValue: (NSString*) value;

- (void) openCell;
- (void) closeCell;

- (UIButton*) getIconButton;

@property (nonatomic) BOOL hasData;
@property (nonatomic) NSString *documentTypeId;
@property (nonatomic) NSString *documentId;

@end

And init method looks like:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.clipsToBounds = true;
        [self setHasData: NO];
        [self setSelectionStyle:UITableViewRowAnimationBottom];
        [self setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

        iconButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [iconButton setFrame: CGRectMake(6, 24, 30, 30)];
        [iconButton setBackgroundColor: [UIColor clearColor]];
        [iconButton setImage:[UIImage imageNamed:@"invoiceIcon.png"] forState:UIControlStateNormal];
        [iconButton setImage:[UIImage imageNamed:@"invoiceIcon.png"] forState:UIControlStateDisabled];
        [iconButton setEnabled: YES];
        [iconButton addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchDown];
        [iconButton  addTarget:self action:@selector(buttonRelease:) forControlEvents:UIControlEventTouchUpInside];
        [iconButton  addTarget:self action:@selector(buttonRelease:) forControlEvents:UIControlEventTouchUpOutside];

        label = [[UILabel alloc] initWithFrame:CGRectMake(42, 4, 250, 16)];
        [label setBackgroundColor:[UIColor clearColor]];
        [label setTextColor: [UIColor  colorWithRed:105.0/255.0 green:154.0/255.0 blue:64.0/255.0 alpha:1.0]];
        [label setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16.0]];

        // creation date
        dayAndMonthLabel = [[UILabel alloc] initWithFrame:CGRectMake(266, 4, 50, 10)];
        [dayAndMonthLabel setBackgroundColor:[UIColor clearColor]];
        [dayAndMonthLabel setTextColor: [UIColor lightGrayColor]];
        [dayAndMonthLabel setFont:[UIFont fontWithName:@"Helvetica" size:10.0]];
        [dayAndMonthLabel setTextAlignment:NSTextAlignmentRight];


        yearLabel = [[UILabel alloc] initWithFrame:CGRectMake(266, 17, 50, 10)];
        [yearLabel setBackgroundColor:[UIColor clearColor]];
        [yearLabel setTextColor: [UIColor lightGrayColor]];
        [yearLabel setFont:[UIFont fontWithName:@"Helvetica" size:10.0]];
        [yearLabel setTextAlignment:NSTextAlignmentRight];
        //------

        // 1 ----
        firstLineLabel = [[UILabel alloc] initWithFrame:CGRectMake(42, 26, 90, 14)];
        [firstLineLabel setBackgroundColor:[UIColor clearColor]];
        [firstLineLabel setTextColor: [UIColor  colorWithRed:73.0/255.0 green:107.0/255.0 blue:196.0/255.0 alpha:1.0]];
        [firstLineLabel setFont:[UIFont fontWithName:@"Helvetica" size:14.0]];
        firstLineValue = [[UILabel alloc] initWithFrame:CGRectMake(42, 44, 250, 14)];
        [firstLineValue setBackgroundColor:[UIColor clearColor]];
        [firstLineValue setTextColor: [UIColor  darkGrayColor]];
        [firstLineValue setFont:[UIFont fontWithName:@"Helvetica" size: 14.0]];
        [firstLineValue setTextAlignment:NSTextAlignmentLeft];

        // 2 ----
        secondLineLabel = [[UILabel alloc] initWithFrame:CGRectMake(42, 62, 90, 14)];
        [secondLineLabel setBackgroundColor:[UIColor clearColor]];
        [secondLineLabel setTextColor: [UIColor  colorWithRed:73.0/255.0 green:107.0/255.0 blue:196.0/255.0 alpha:1.0]];
        [secondLineLabel setFont:[UIFont fontWithName:@"Helvetica" size:14.0]];
        secondLineValue = [[UILabel alloc] initWithFrame:CGRectMake(98, 62, 180, 14)];
        [secondLineValue setBackgroundColor:[UIColor clearColor]];
        [secondLineValue setTextColor: [UIColor  darkGrayColor]];
        [secondLineValue setFont:[UIFont fontWithName:@"Helvetica" size:14.0]];
        [secondLineValue setTextAlignment:NSTextAlignmentRight];

        // 3 ----
        thirdLineLabel = [[UILabel alloc] initWithFrame:CGRectMake(42, 80, 90, 14)];
        [thirdLineLabel setBackgroundColor:[UIColor clearColor]];
        [thirdLineLabel setTextColor: [UIColor  colorWithRed:73.0/255.0 green:107.0/255.0 blue:196.0/255.0 alpha:1.0]];
        [thirdLineLabel setFont:[UIFont fontWithName:@"Helvetica" size:14.0]];
        thirdLineValue = [[UILabel alloc] initWithFrame:CGRectMake(98, 80, 180, 14)];
        [thirdLineValue setBackgroundColor:[UIColor clearColor]];
        [thirdLineValue setTextColor: [UIColor  darkGrayColor]];
        [thirdLineValue setFont:[UIFont fontWithName:@"Helvetica" size:14.0]];
        [thirdLineValue setTextAlignment:NSTextAlignmentRight];

        // 4 ----
        fourthLineLabel = [[UILabel alloc] initWithFrame:CGRectMake(42, 96, 90, 13)];
        [fourthLineLabel setBackgroundColor:[UIColor clearColor]];
        [fourthLineLabel setTextColor: [UIColor  colorWithRed:73.0/255.0 green:107.0/255.0 blue:196.0/255.0 alpha:1.0]];
        [fourthLineLabel setFont:[UIFont fontWithName:@"Helvetica" size:13.0]];
        fourthLineValue = [[UILabel alloc] initWithFrame:CGRectMake(98, 96, 180, 13)];
        [fourthLineValue setBackgroundColor:[UIColor clearColor]];
        [fourthLineValue setTextColor: [UIColor  darkGrayColor]];
        [fourthLineValue setFont:[UIFont fontWithName:@"Helvetica" size:13.0]];
        [fourthLineValue setTextAlignment:NSTextAlignmentRight];

        // 5 ----
        fifthLineLabel = [[UILabel alloc] initWithFrame:CGRectMake(42, 109, 90, 13)];
        [fifthLineLabel setBackgroundColor:[UIColor clearColor]];
        [fifthLineLabel setTextColor: [UIColor  colorWithRed:73.0/255.0 green:107.0/255.0 blue:196.0/255.0 alpha:1.0]];
        [fifthLineLabel setFont:[UIFont fontWithName:@"Helvetica" size:13.0]];
        fifthLineValue = [[UILabel alloc] initWithFrame:CGRectMake(98, 109, 180, 13)];
        [fifthLineValue setBackgroundColor:[UIColor clearColor]];
        [fifthLineValue setTextColor: [UIColor  darkGrayColor]];
        [fifthLineValue setFont:[UIFont fontWithName:@"Helvetica" size:13.0]];
        [fifthLineValue setTextAlignment:NSTextAlignmentRight];

        [self.contentView addSubview: iconButton];
        [self.contentView addSubview: label];

        [self.contentView addSubview: dayAndMonthLabel];
        [self.contentView addSubview: yearLabel];

        [self.contentView addSubview: firstLineLabel];
        [self.contentView addSubview: firstLineValue];
        [self.contentView addSubview: secondLineLabel];
        [self.contentView addSubview: secondLineValue];
        [self.contentView addSubview: thirdLineLabel];
        [self.contentView addSubview: thirdLineValue];
        [self.contentView addSubview: fourthLineLabel];
        [self.contentView addSubview: fourthLineValue];
        [self.contentView addSubview: fifthLineLabel];
        [self.contentView addSubview: fifthLineValue];

    }
    return self;
}

exaple setter method looks:

- (void) setFirstLineValue: (NSString*) value
{
    [firstLineValue setText:value];
}

and in my view I had cellForRowAtIndexPath looks:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"DOCUMENT_LIST_CELL";

    ExpandableTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil){
        cell = [[ExpandableTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.documentTypeId = [NSString stringWithFormat:@"%@", fisDocumentTypeId ];
        if(indexPath.item >= start && indexPath.item <= end){
            [cell setHasData:TRUE];
        } else {
            UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
            spinner.frame = CGRectMake(148, 27, 24, 24);
            spinner.tag = 1234567;
            [cell setHasData:FALSE];
            [cell.contentView addSubview: spinner];
            [spinner startAnimating];
        }
    } else {
        id viewToRemove = [cell.contentView viewWithTag:1234567];
        [viewToRemove removeFromSuperview];
        viewToRemove = nil;

        if(indexPath.item >= start && indexPath.item <= end){
            [cell setHasData:TRUE];
        } else {
            UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
            spinner.frame = CGRectMake(148, 27, 24, 24);
            spinner.tag = 1234567;
            [cell setHasData:FALSE];
            [cell.contentView addSubview: spinner];
            [spinner startAnimating];
        }
    }

    if(indexPath.item >= start && indexPath.item <= end){
        NSMutableDictionary *dataDict = [documentListData objectAtIndex: (indexPath.item - start)];
        NSDate *localizedDate = [isoDateFormatter dateFromString:[dataDict valueForKey:@"creationDate"]];
        [cell setLabelValue: [dataDict valueForKey:@"identifier"] != [NSNull null] ? [dataDict valueForKey:@"identifier"] : [dataDict valueForKey:@"id"] ];
        cell.documentId = [NSString stringWithFormat:@"%@",[dataDict valueForKey:@"id"] ];
        [cell setDayAndMonthValue: [dayAndMonthFormatter stringFromDate:localizedDate] ];
        [cell setYearValue: [yearFormatter stringFromDate:localizedDate] ];

        for(int i = 0; i < 5; i++){
            if( [dataDict objectForKey: [NSString stringWithFormat:@"line%i",i ] ] != nil ){
                NSMutableDictionary *lineDict = [dataDict objectForKey: [NSString stringWithFormat:@"line%i",i ] ];

                switch (i) {
                    case 0:
                        [cell setFirstLineLabelValue: [lineDict objectForKey:@"field"]];
                        [cell setFirstLineValue: [[NSString stringWithFormat:@"%@", [lineDict objectForKey:@"value"]]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] ];
                        break;
                    case 1:
                        [cell setSecondLineLabelValue: [lineDict objectForKey:@"field"]];
                        [cell setSecondLineValue: [[NSString stringWithFormat:@"%@", [lineDict objectForKey:@"value"]] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] ];
                        break;
                    case 2:
                        [cell setThirdLineLabelValue: [lineDict objectForKey:@"field"]];
                        [cell setThirdLineValue: [[NSString stringWithFormat:@"%@", [lineDict objectForKey:@"value"]] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] ];
                        break;
                    case 3:
                        [cell setFourthLineLabelValue: [lineDict objectForKey:@"field"]];
                        [cell setFourthLineValue: [[NSString stringWithFormat:@"%@", [lineDict objectForKey:@"value"]] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] ];
                        break;
                    case 4:
                        [cell setFifthLineLabelValue: [lineDict objectForKey:@"field"]];
                        [cell setFifthLineValue: [[NSString stringWithFormat:@"%@", [lineDict objectForKey:@"value"]] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] ];
                        break;
                    default:
                        break;
                }
            }
        }

    } else {
        [cell setLabelValue: @""];
        [cell setDayAndMonthValue: @""];
        [cell setYearValue: @""];
        [cell setFirstLineLabelValue: @""];
        [cell setFirstLineValue: @""];
        [cell setSecondLineLabelValue: @""];
        [cell setSecondLineValue: @""];
        [cell setThirdLineLabelValue: @""];
        [cell setThirdLineValue: @""];
        [cell setFourthLineLabelValue:@""];
        [cell setFourthLineValue:@""];
        [cell setFifthLineLabelValue:@""];
        [cell setFifthLineValue:@""];
    }


    return cell;
}

EDIT ------------ For request of Alok, I'm puting code of creating view:

DocumentListViewController *documentListView = [[DocumentListViewController alloc] initWithStyle:UITableViewStylePlain documentTypeId:documentTypeId documentTypeName:translation documentTypeIcon:nil documentTypeCode: code andSyncField:syncField andSyncFieldTranslation:syncFieldTrans documentTypeData:documentTypeData];
    documentListView.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:documentListView animated:YES];

And I do nothink in view/controler when I touch bac button, only I have:

- (void)viewWillDisappear:(BOOL)animated
{
    [self.navigationController setToolbarHidden:YES animated:NO];
    [super viewWillDisappear:animated];
}
Ricardo Sanchez-Saez
  • 9,466
  • 8
  • 53
  • 92
lukisp
  • 1,031
  • 3
  • 14
  • 27
  • "Every time if I enter to table view and back", at first implement dealloc and put break point.then at the time of back button press is your -dealloc called ??? (i assume this view controller have uitableview as subview ?), – maddy Sep 27 '14 at 13:20
  • I added -(void)dealloc to Controller which extends UITableViewController and dealloc method wasn't called. – lukisp Sep 27 '14 at 13:34
  • 1
    It must be called, even if you are using ARC you can imeplement -dealloc {////// release special objects like notification, not in your case, dont call [super dealloc]; in arc ok}.can you tell me the flow when you open this subclass/extended view controller, how do you open it and how do you close it(on back button pressed), – maddy Sep 27 '14 at 13:35
  • Don't know, but if it should be,I think that mean that I had somewhere reference to view/controller ? – lukisp Sep 27 '14 at 13:39
  • the code of creating view with cells is in my first post (for code formating) – lukisp Sep 27 '14 at 13:50
  • Have you run Instruments to check for leaks? – Holly Sep 27 '14 at 13:50
  • Yes it show nothink :/ – lukisp Sep 27 '14 at 13:52

2 Answers2

0

please find sample test.zip on uitableview memory from your code i have made changes in firstLabel, firstLabelValue..secondLabel, secondLabelVlue....fifth. by making them property.commented your code of setting text in these labels.also there is a screen shot showinh memory release on push and pop see memory pattern in blue up and down... in cellForRowAtIndexPath setting text in your labels (firstLabel, firstLabelValue..secondLabel, secondLabelVlue....fifth) by propertis.put @autorelease in for loop. you can follow this pattern in other controls like button for more memory improvement verify it with your dictionary data and let me know.

thanks

maddy
  • 4,001
  • 8
  • 42
  • 65
0

I resoloved problem. The point to all was Alok post. I add method dealloc but it was never called. Then I tried to remove all references in viewWillDisappear method (seting them to nil), and when I do this, then dealloc method was called all memory was released.

Thanks

lukisp
  • 1,031
  • 3
  • 14
  • 27