0

I am creating multiple labels within a loop, and im confused on memory management since every instance of the label has the same name since they are all created within a loop so would that mean that each time a new label is created within the loop, the last label allocated would be overridden and no longer take up memory or is it still stored in memory. Quite confused, please help, thanks in advance.

- (void)fetchEntrys
{

JournalrAppDelegate *appDelegate =
[[UIApplication sharedApplication] delegate];

NSManagedObjectContext *context =
[appDelegate managedObjectContext];

NSEntityDescription *entityDesc =
[NSEntityDescription entityForName:@"Entrys"
            inManagedObjectContext:context];

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];


NSManagedObject *matches = nil;

NSError *error;
NSArray *objects = [context executeFetchRequest:request
                                          error:&error];

if ([objects count] == 0) {
    NSLog(@"No matches");
} else {


    NSLog(@"Matches found");



    matches = objects[1];

    NSString *firstEntry = [matches valueForKey:@"entry"];
    NSLog(@"First Entry: %@", firstEntry);
    self.totlal = [objects count];
     int i = [objects count] - 1;
    NSLog(@"%i", i);
    while ( i >  -1) {
        matches = objects[i];
        NSString *entry = [matches valueForKey:@"entry"];
        NSDate *date = [matches valueForKey:@"date"];
        NSDateFormatter *formatDate = [[NSDateFormatter alloc]init];
        [formatDate setDateFormat:@"MM/dd/YY"];
        NSString *dateString = [formatDate stringFromDate:date];


        NSLog(@"Date: %@", dateString);
         NSLog(@"Entry: %@", entry);

        UILabel *dateLabel = [[UILabel alloc] init];
        dateLabel.text = dateString;
        dateLabel.frame = CGRectMake(20, cY, 100, 30);
        dateLabel.numberOfLines = 3;
        dateLabel.font = [UIFont boldSystemFontOfSize:24.0];
        [dateLabel setTag:i];

        UIButton *deleteButton  = [[UIButton alloc]initWithFrame:CGRectMake(230, dateLabel.frame.origin.y, 70, 27)];
        [deleteButton addTarget:self action:@selector(deleteButtonPressed) forControlEvents:UIControlEventTouchDown];
        [deleteButton setBackgroundImage:[UIImage imageNamed:@"delete.jpg"] forState:UIControlStateNormal];
        [deleteButton setTag:i];


        cY += 35;

        UILabel *labelEntry = [[UILabel alloc]init];
        labelEntry.numberOfLines = 0;
        labelEntry.text = entry;
        CGRect lblFrame =  CGRectMake(20, cY, 280, 1000);
        labelEntry.frame = lblFrame;
        labelEntry.textAlignment = NSTextAlignmentLeft;
        labelEntry.backgroundColor = [UIColor clearColor];
        [labelEntry sizeToFit];
        [labelEntry setTag:i];





        UILabel *outerLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, cY - 50, 300, labelEntry.frame.size.height + 80)];
        [scrollView addSubview:outerLabel];

        outerLabel.layer.borderColor = [UIColor grayColor].CGColor;
        outerLabel.layer.borderWidth = 1.0;
        outerLabel.tag = i;

        if(i ==[objects count] - 1){
            self.firstY = outerLabel.frame.origin.y;
        }

        //UIImageView *backgroundImage = [[UIImageView alloc]initWithFrame:CGRectMake(10, cY, 312, labelEntry.frame.size.height)];
        //backgroundImage.contentMode = UIViewContentModeScaleToFill;
        //backgroundImage.image = [UIImage imageNamed:@"post.jpg"];
        //NSLog(@"Image Height :%f", backgroundImage.frame.size.height);

        //[scrollView addSubview:backgroundImage];






        cY += labelEntry.frame.size.height;
        NSLog(@"Label Height: %f", labelEntry.frame.size.height);
        NSLog(@"currenty: %f", cY);
        cY += 100.f;

        scrollView.scrollEnabled = YES;
        scrollView.showsVerticalScrollIndicator = YES;
        [scrollView addSubview: labelEntry];
        [scrollView addSubview:dateLabel];
        [scrollView addSubview:deleteButton];

        NSLog(@"%i", i);

                    i--;
    }

    scrollView.contentSize = CGSizeMake(320, cY);
    NSLog(@"cY: %f", cY);
    self.currentI = i;
user2489946
  • 281
  • 3
  • 12
  • Please post a minimal listing of the code that shows the problem. – jarmod Aug 11 '13 at 19:16
  • You're adding them as subviews of your scrollview as you go along which is fine - that increments the retain count for each label. You should release each label after it's added to the scrollview and the labels will now be owned by the scrollview and it will release them eventually. See http://stackoverflow.com/questions/4163908/does-addsubview-increment-retain-count – jarmod Aug 11 '13 at 19:24
  • @jarmod I thought [view release] was deprecated under ARC? – user2489946 Aug 11 '13 at 19:26
  • Yup, I believe you're right. I prefer to be explicit. – jarmod Aug 11 '13 at 19:35

1 Answers1

0

As long as there is still some reference to the label, its memory will be kept. For example, if you use [self.view addSubview:label], then the label will stay in memory as long as self.view does.

Likewise, if you add the label to an array, the label's memory will be retained until te array is released.

Once you create a new label with the same instance name, you can no longer access the old one through it. If your label is a subview of a view, you can use [view subviews] to return an array of subviews and use that array to find the label.

Jsdodgers
  • 5,253
  • 2
  • 20
  • 36
  • But each label is of the same instance name, so how would i change a property of the second label if it has the same instance name as the other 100 labels? And how can two class instances exist with the same name – user2489946 Aug 11 '13 at 19:22
  • Once you create a new label with the same instance name, you can no longer access the old one through it. If your label is a subview of a view, you can use `[view subviews]` to return an array of subviews and use that array to find the label. – Jsdodgers Aug 11 '13 at 19:25