Deallocating messages after creating arrays in BMItime


#1

Would please someone explain this behavior of main.m

If in main.m i run only NSLog(@"Employees :%@", employees); I get the following output:

2013-08-23 11:47:40.815 BMITime[2304:303] Employees :( "<Employee 0: $0 in assets>", "<Employee 1: $153 in assets>", "<Employee 2: $119 in assets>", "<Employee 3: $68 in assets>", "<Employee 4: $0 in assets>", "<Employee 5: $136 in assets>", "<Employee 6: $119 in assets>", "<Employee 7: $34 in assets>", "<Employee 8: $0 in assets>", "<Employee 9: $136 in assets>" ) 2013-08-23 11:47:40.817 BMITime[2304:303] deallocating employee <Employee 0: $0 in assets> 2013-08-23 11:47:40.817 BMITime[2304:303] deallocating employee <Employee 1: $153 in assets> 2013-08-23 11:47:40.817 BMITime[2304:303] deallocating asset <Laptop 9: $153> 2013-08-23 11:47:40.818 BMITime[2304:303] deallocating employee <Employee 2: $119 in assets> 2013-08-23 11:47:40.818 BMITime[2304:303] deallocating asset <Laptop 7: $119> 2013-08-23 11:47:40.818 BMITime[2304:303] deallocating employee <Employee 3: $68 in assets> 2013-08-23 11:47:40.818 BMITime[2304:303] deallocating asset <Laptop 0: $0> 2013-08-23 11:47:40.819 BMITime[2304:303] deallocating asset <Laptop 4: $68> 2013-08-23 11:47:40.819 BMITime[2304:303] deallocating employee <Employee 4: $0 in assets> 2013-08-23 11:47:40.819 BMITime[2304:303] deallocating employee <Employee 5: $136 in assets> 2013-08-23 11:47:40.820 BMITime[2304:303] deallocating asset <Laptop 3: $51> 2013-08-23 11:47:40.820 BMITime[2304:303] deallocating asset <Laptop 5: $85> 2013-08-23 11:47:40.820 BMITime[2304:303] deallocating employee <Employee 6: $119 in assets> 2013-08-23 11:47:40.821 BMITime[2304:303] deallocating asset <Laptop 1: $17> 2013-08-23 11:47:40.821 BMITime[2304:303] deallocating asset <Laptop 6: $102> 2013-08-23 11:47:40.821 BMITime[2304:303] deallocating employee <Employee 7: $34 in assets> 2013-08-23 11:47:40.822 BMITime[2304:303] deallocating asset <Laptop 2: $34> 2013-08-23 11:47:40.822 BMITime[2304:303] deallocating employee <Employee 8: $0 in assets> 2013-08-23 11:47:40.823 BMITime[2304:303] deallocating employee <Employee 9: $136 in assets> 2013-08-23 11:47:40.823 BMITime[2304:303] deallocating asset <Laptop 8: $136>

Here is my main.m

[code]//
// main.m
#import <Foundation/Foundation.h>
#import “Employee.h”
#import “Asset.h”

int main(int argc, const char * argv[])
{
@autoreleasepool {
//Create an array of Employee objects
NSMutableArray *employees = [[NSMutableArray alloc]init];
for (int i =0; i< 10; i++) {

        //Create an instance of Employee
        Employee *person = [[Employee alloc]init];
        
        //Set up initial values
        [person setWeightInKilos:90+i];
        [person setHeightInMeters:1.0-i/10.00];
        [person setEmployeeID:i];
        
        //put the emloyee in the employee array
        [employees addObject:person];
    }
    //create 10 assets
    for (int i = 0; i<10; i++) {
        
        //Create an Asset
        Asset *asset = [[Asset alloc]init];
        
        //Give it an interesting label
        NSString *currentLabel = [NSString stringWithFormat:@"Laptop %d", i];
        [asset setLabel:currentLabel];
        [asset setResaleValue:i* 17];
        
        //Get a random number between 0 and 9 inclusive
        NSUInteger randomIndex = random() % [employees count];
        
        //Find that employee
        Employee *randomPerson = [employees objectAtIndex:randomIndex];
        [randomPerson addAssetObject:asset];
    }
          NSLog(@"Employees :%@", employees);

// NSLog(@“Giving up ownership of one employee”);
// [employees removeObjectAtIndex:5];
// NSLog(@“Giving up ownership of array”);
// employees = nil;
}
return 0;
}

[/code]

If I uncomment the last lines of main.m then I get the expected output like this:

2013-08-23 11:50:43.291 BMITime[2337:303] Employees :( "<Employee 0: $0 in assets>", "<Employee 1: $153 in assets>", "<Employee 2: $119 in assets>", "<Employee 3: $68 in assets>", "<Employee 4: $0 in assets>", "<Employee 5: $136 in assets>", "<Employee 6: $119 in assets>", "<Employee 7: $34 in assets>", "<Employee 8: $0 in assets>", "<Employee 9: $136 in assets>" ) 2013-08-23 11:50:43.293 BMITime[2337:303] Giving up ownership of one employee 2013-08-23 11:50:43.293 BMITime[2337:303] deallocating employee <Employee 5: $136 in assets> 2013-08-23 11:50:43.293 BMITime[2337:303] deallocating asset <Laptop 3: $51> 2013-08-23 11:50:43.294 BMITime[2337:303] deallocating asset <Laptop 5: $85> 2013-08-23 11:50:43.294 BMITime[2337:303] Giving up ownership of array 2013-08-23 11:50:43.294 BMITime[2337:303] deallocating employee <Employee 0: $0 in assets> 2013-08-23 11:50:43.295 BMITime[2337:303] deallocating employee <Employee 1: $153 in assets> 2013-08-23 11:50:43.295 BMITime[2337:303] deallocating asset <Laptop 9: $153> 2013-08-23 11:50:43.295 BMITime[2337:303] deallocating employee <Employee 2: $119 in assets> 2013-08-23 11:50:43.296 BMITime[2337:303] deallocating asset <Laptop 7: $119> 2013-08-23 11:50:43.296 BMITime[2337:303] deallocating employee <Employee 3: $68 in assets> 2013-08-23 11:50:43.296 BMITime[2337:303] deallocating asset <Laptop 0: $0> 2013-08-23 11:50:43.297 BMITime[2337:303] deallocating asset <Laptop 4: $68> 2013-08-23 11:50:43.297 BMITime[2337:303] deallocating employee <Employee 4: $0 in assets> 2013-08-23 11:50:43.297 BMITime[2337:303] deallocating employee <Employee 6: $119 in assets> 2013-08-23 11:50:43.298 BMITime[2337:303] deallocating asset <Laptop 1: $17> 2013-08-23 11:50:43.298 BMITime[2337:303] deallocating asset <Laptop 6: $102> 2013-08-23 11:50:43.298 BMITime[2337:303] deallocating employee <Employee 7: $34 in assets> 2013-08-23 11:50:43.299 BMITime[2337:303] deallocating asset <Laptop 2: $34> 2013-08-23 11:50:43.299 BMITime[2337:303] deallocating employee <Employee 8: $0 in assets> 2013-08-23 11:50:43.299 BMITime[2337:303] deallocating employee <Employee 9: $136 in assets> 2013-08-23 11:50:43.300 BMITime[2337:303] deallocating asset <Laptop 8: $136>


#2

MrZander (I hope you are not the infamous Ed Zander),

Do you mean employees and assets objects are still being released despite the commented out statement:

...
//       employees = nil;
    }
    return 0;
}

That’s because the scope in which the employees object gets created:

int main(int argc, const char * argv[])
{
    @autoreleasepool {
         NSMutableArray *employees = [[NSMutableArray alloc]init];
         ...
//       employees = nil;
    }
    return 0;
}

is being destroyed and as a result the employees object is being released automatically since there are not any strong references to it.


#3

Thank you, ibex10.

Yes, that is what i mean.

[quote=“ibex10”]
That’s because the scope in which the employees object gets created:

int main(int argc, const char * argv[]) { @autoreleasepool { NSMutableArray *employees = [[NSMutableArray alloc]init]; ... // employees = nil; } return 0; }

is being destroyed and as a result the employees object is being released automatically since there are not any strong references to it.[/quote]

OK, I understand now that this behavior is to be expected and ARC is now handling objects’ deallocation in the background. I was not sure if there was any error in the code.

P.S. I am not ED :slight_smile: