When is 'dealloc' really called?

Discussion in 'iOS Programming' started by larswik, Oct 2, 2011.

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    My table view has been acting a little odd and I discovered a big no no, I think. When my app launches it checks to see if there is a plist file on the iphone, If there is it instantiates 3 NSMutableArrays and adds the content from the plist. otherwise it creates a new plist file and instantiates the 3 NSMutableArrays.
    Code:
    - (void)readPlist{
        NSLog(@"Reading files from plist");
    
        NSString *filePath = [self dataFilePath];
        if([[NSFileManager defaultManager] fileExistsAtPath:filePath])
        {
            savedList = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
            
            NSArray *tempArray = [[NSArray alloc] initWithArray: [savedList objectForKey:@"monthly"]];
            monthlyPayments = [[NSMutableArray alloc] initWithArray: tempArray copyItems:YES];
            [tempArray release];
            NSLog(@"Files read monthly:%@",monthlyPayments);
            
            NSArray *tempArrayTwo = [[NSArray alloc] initWithArray: [savedList objectForKey:@"total"]];
            totalPayments = [[NSMutableArray alloc] initWithArray: tempArrayTwo copyItems:YES];
            [tempArrayTwo release];
            NSLog(@"Files read total:%@",totalPayments);
            
            NSArray *tempArrayThree = [[NSArray alloc] initWithArray: [savedList objectForKey:@"item"]];
            itemsList = [[NSMutableArray alloc] initWithArray: tempArrayThree copyItems:YES];
            [tempArrayThree release];
            NSLog(@"Files read item:%@",itemsList);
        } 
        else{
            savedList = [[NSMutableDictionary alloc] init];
            [savedList writeToFile: filePath atomically:YES]; // if it can't find it, it writes a blank file.
            NSLog(@"No Plist on file - created new one");
            monthlyPayments = [[NSMutableArray alloc] init];
            totalPayments = [[NSMutableArray alloc] init];
            itemsList = [[NSMutableArray alloc] init];
        }
    }
    
    I have the release for all 3 Arrays in the dealloc. When the user deletes a row I write the new information to the plist and then re load the new information from the plist again and re calculate the totals which are displayed in a UILabel.

    After writing to the plist I made a mistake to say [itemsList removeAllObject]; This only removed the objects. I then made a call to read it in again [self readPlist]; and here lies my question. Is dealloc called before the new NSMutableArrays are instantiated to release them, or did I just overwrite my old arrays with new ones with the same names causing a memory leak?

    In the table method that deletes the row from the table view I now added the releases for my 3 Arrays.
    Code:
    .....
        [self writePlist];
        [itemsList release];
        [monthlyPayments release];
        [totalPayments release];
        [self readPlist];
    .....
    
    But now there are 2 sets of releases one in dealloc and another I release myself.

    What I am unsure of is that if the program sees that it is about to allocate space for a new Array, with the same name, will it call dealloc first to release the old array before the new one is created? Or do I have to release it myself like the code snippet above? My only worry then is having 2 releases for the same object, in dealloc and in a method.

    From what I read dealloc is only called as it exits the CurrentViewController. But I want to be sure I read that correctly and make sure I can have to releases for the same object.
     
  2. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #2
    Your description of what's going on is too difficult to parse and give a precise answer, so I'll give you a general one instead:

    If you are doing memory management correctly, you should never have to think about when exactly an object is deallocated. This is important, because it is almost impossible to know when dealloc will be called for a given object. As long as you don't release an object until you are really done using it, you shouldn't care when dealloc is called.

    Your question also makes me wonder if you understand the difference between a variable name and the object that it points to.
     
  3. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #3
    The list was not operating the way I was anticipating so I asked the question and then started over with the whole class. I had started putting a lot of band aid code. I stepped back from the problem and started fresh by writing it down to paper.

    I am still jumping over some hurdles but but it is clearer now since I started over.

    Variable names from my understanding is that you declare a variable and the variable name will point to the address in memory that holds your instantiated object. A variable name is nothing more then a pointer to the memory address unless it is a primitive type like int, char, float and so on.

    hope I got that right.
     

Share This Page