Wierd deallocating problem, please help


#1

For some reason, some of the assets get deallocated when in the for look where I am creating assets and assigning them to random employees. I introduced the the BREAK1 and BREAK2 logs in the for loop just to see where the deallocation is happening. I am also copying my code below.

Here’s my output
2013-05-11 07:16:43.379 BMITime (33)[4078:303] BREAK1
2013-05-11 07:16:43.381 BMITime (33)[4078:303] BREAK2
2013-05-11 07:16:43.382 BMITime (33)[4078:303] deallocating <Laptop0: $0>
2013-05-11 07:16:43.382 BMITime (33)[4078:303] BREAK1
2013-05-11 07:16:43.383 BMITime (33)[4078:303] BREAK2
2013-05-11 07:16:43.383 BMITime (33)[4078:303] deallocating <Laptop1: $17>
2013-05-11 07:16:43.384 BMITime (33)[4078:303] BREAK1
2013-05-11 07:16:43.384 BMITime (33)[4078:303] BREAK2
2013-05-11 07:16:43.385 BMITime (33)[4078:303] deallocating <Laptop2: $34>
2013-05-11 07:16:43.385 BMITime (33)[4078:303] BREAK1
2013-05-11 07:16:43.386 BMITime (33)[4078:303] BREAK2
2013-05-11 07:16:43.386 BMITime (33)[4078:303] deallocating <Laptop3: $51>
2013-05-11 07:16:43.386 BMITime (33)[4078:303] BREAK1
2013-05-11 07:16:43.387 BMITime (33)[4078:303] BREAK2
2013-05-11 07:16:43.387 BMITime (33)[4078:303] BREAK1
2013-05-11 07:16:43.388 BMITime (33)[4078:303] BREAK2
2013-05-11 07:16:43.388 BMITime (33)[4078:303] BREAK1
2013-05-11 07:16:43.389 BMITime (33)[4078:303] BREAK2
2013-05-11 07:16:43.389 BMITime (33)[4078:303] BREAK1
2013-05-11 07:16:43.390 BMITime (33)[4078:303] BREAK2
2013-05-11 07:16:43.390 BMITime (33)[4078:303] deallocating <Laptop7: $119>
2013-05-11 07:16:43.390 BMITime (33)[4078:303] BREAK1
2013-05-11 07:16:43.391 BMITime (33)[4078:303] BREAK2
2013-05-11 07:16:43.392 BMITime (33)[4078:303] deallocating <Laptop8: $136>
2013-05-11 07:16:43.392 BMITime (33)[4078:303] BREAK1
2013-05-11 07:16:43.393 BMITime (33)[4078:303] BREAK2
2013-05-11 07:16:43.394 BMITime (33)[4078:303] deallocating <Laptop9: $153>
2013-05-11 07:16:43.395 BMITime (33)[4078:303] Employees:(
"<Employee 0: $0 in assets>",
"<Employee 1: $0 in assets>",
"<Employee 2: $0 in assets>",
"<Employee 3: $68 in assets>",
"<Employee 4: $0 in assets>",
"<Employee 5: $85 in assets>",
"<Employee 6: $102 in assets>",
"<Employee 7: $0 in assets>",
"<Employee 8: $0 in assets>",
"<Employee 9: $0 in assets>"
)
2013-05-11 07:16:43.395 BMITime (33)[4078:303] Giving ownership of one employee
2013-05-11 07:16:43.396 BMITime (33)[4078:303] Deallocating <Employee 5: $85 in assets>
2013-05-11 07:16:43.397 BMITime (33)[4078:303] deallocating <Laptop5: $85>
2013-05-11 07:16:43.397 BMITime (33)[4078:303] Giving up ownership of array
2013-05-11 07:16:43.398 BMITime (33)[4078:303] Deallocating <Employee 0: $0 in assets>
2013-05-11 07:16:43.399 BMITime (33)[4078:303] Deallocating <Employee 1: $0 in assets>
2013-05-11 07:16:43.401 BMITime (33)[4078:303] Deallocating <Employee 2: $0 in assets>
2013-05-11 07:16:43.401 BMITime (33)[4078:303] Deallocating <Employee 3: $68 in assets>
2013-05-11 07:16:43.402 BMITime (33)[4078:303] deallocating <Laptop4: $68>
2013-05-11 07:16:43.403 BMITime (33)[4078:303] Deallocating <Employee 4: $0 in assets>
2013-05-11 07:16:43.404 BMITime (33)[4078:303] Deallocating <Employee 6: $102 in assets>
2013-05-11 07:16:43.405 BMITime (33)[4078:303] deallocating <Laptop6: $102>
2013-05-11 07:16:43.405 BMITime (33)[4078:303] Deallocating <Employee 7: $0 in assets>
2013-05-11 07:16:43.406 BMITime (33)[4078:303] Deallocating <Employee 8: $0 in assets>
2013-05-11 07:16:43.406 BMITime (33)[4078:303] Deallocating <Employee 9: $0 in assets>

main.m

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

int main(int argc, const char * argv[])
{
@autoreleasepool {
//create an array of employees
NSMutableArray *empls = [[NSMutableArray alloc] init];

    for (int i =0; i<10; i++) {
        Employee *pers =[[Employee alloc] init];
        [pers setWeightInKilos:90+i];
        [pers setHeightInMeters:1.8-i/10.0];
        [pers setEmployeeID:i];
        [empls addObject:pers];
    }
    //create 10 assets and assign them to employees
    for (int i = 0; i<10; i++) {
        //create an asset
        NSLog(@"BREAK1");
        Asset *ast = [[Asset alloc] init];
        NSString *currentLabel = [NSString stringWithFormat:@"Laptop%d",i];
        [ast setLabel:currentLabel];
        [ast setResaleValue:i*17];
        NSUInteger randomIndex = random()%empls.count;
        
        //find the random employee
        Employee *randomEmployee = [empls objectAtIndex:randomIndex];
        
        //add the created asset to the employee
        [randomEmployee addAssetsObject:ast];
        NSLog(@"BREAK2");
    }
    NSLog(@"Employees:%@",empls);
    NSLog(@"Giving ownership of one employee");
    [empls removeObjectAtIndex:5];
    NSLog(@"Giving up ownership of array");
    empls = nil;
}
return 0;

}
[/code]

Employee.h

#import <Foundation/Foundation.h>
#import "Person.h"
@class Asset;
@interface Employee : Person

{
    int employeeID;
    NSMutableArray *assets;
}
- (void) addAssetsObject: (Asset *) a;
- (unsigned int) valueOfAssets;
@property int employeeID;
@end

Asset.h

#import <Foundation/Foundation.h>
@interface Asset : NSObject
{
    NSString *label;
    unsigned int resaleValue;
}
@property (strong) NSString *label;
@property unsigned int resaleValue;
@end

Asset.m

#import "Asset.h"
@implementation Asset
@synthesize label, resaleValue;
-(NSString *)description 
{
    return [NSString stringWithFormat:@"<%@: $%d>",self.label, self.resaleValue];
}
-(void) dealloc
{
    NSLog(@"deallocating %@",self);
}
@end

Employee.m

#import "Employee.h"
#import "Asset.h"
@implementation Employee
@synthesize employeeID;
-(void)addAssetsObject:(Asset *)a
{
    //is asset emptry?
    if (!assets) {
        assets = [[NSMutableArray alloc] init];
    }else{
        [assets addObject:a];
    }
}
-(unsigned int) valueOfAssets
{
    unsigned int sum = 0;
    for (Asset *ast in assets) {
        sum += ast.resaleValue;
    }
    return sum;
}
- (float) bodyMassIndex
{
    float normalBMI = [super bodyMassIndex];
    return normalBMI * 0.9;
}
-(NSString *) description
{
    return [NSString stringWithFormat:@"<Employee %d: $%d in assets>",self.employeeID, self.valueOfAssets];
}
-(void) dealloc
{
    NSLog(@"Deallocating %@", self);
}
@end

Person.h

//#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
    float heightInMeters;
    int weightInKilos;
}
@property float heightInMeters;
@property int weightInKilos;
-(float)bodyMassIndex;
@end

Person.m

#import "Person.h"
@implementation Person
@synthesize heightInMeters, weightInKilos;
-(float)bodyMassIndex
{
    float h = self.heightInMeters;
    float w = self.weightInKilos;
    return w/(h*h);
}
@end

#2

Never mind, found the error in my code. In Employee.m I needed:

     if (!assets) {
        assets = [[NSMutableArray alloc] init];
    }
        [assets addObject:a];

instead of what I had:

if (!assets) {
        assets = [[NSMutableArray alloc] init];
    }else{
        [assets addObject:a];
    }