Built example, but deallocations don't match book


#1

Again, I’m missing something. I built the example and have gone over the code several times, but must be missing something. In Aaron’s example, asset descriptions print to NSLog as they are deallocated. Mine do not. Here’s the result of running the program:

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>" ) Giving up ownership of one employee Giving up ownership of array

Here’s the code I’ve got (bodyMassIndex method deleted as it is not used in this iteration):

This is Asset.h

[code]#import “Person.h”
@class Asset;

@interface Employee : Person
{
int employeeID;
NSMutableArray *assets;
}
@property int employeeID;

  • (void)addAssetsObject:(Asset *)a;
  • (unsigned int)valueOfAssets;

@end[/code]

And here’s Asset.m

[code]#import “Asset.h”

@implementation Asset

@synthesize label, resaleValue;

  • (NSString *)description
    {
    return [NSString stringWithFormat:@"<%@: $%d >",[self label], [self resaleValue]];
    }

  • (void)dealloc
    {
    NSLog(@“deallocating %@”, self);
    }
    @end
    [/code]

Employee.h follows:

#import "Person.h"
@class Asset;

@interface Employee : Person
{
    int employeeID;
    NSMutableArray *assets;
}
@property int employeeID;

- (void)addAssetsObject:(Asset *)a;
- (unsigned int)valueOfAssets;

@end

And then Employee.m:

[code]#import “Employee.h”
#import “Asset.h”

@implementation Employee

@synthesize employeeID;

  • (void)addAssetsObject:(Asset *)a
    {
    // Is assets nil?
    if (!assets)
    {
    // create the array
    assets = [[NSMutableArray alloc] init];
    }
    [assets addObject:a];
    }

  • (unsigned int)valueOfAssets
    {
    // Sum up the resale value of the assets
    unsigned int sum = 0;
    for (Asset *a in assets)
    {
    sum += [a resaleValue];
    }
    return sum;

}

  • (NSString *)description
    {
    return [NSString stringWithFormat:@"<Employee %d: $%d in assets>", [self employeeID], [self valueOfAssets]];
    }

  • (void)dealloc
    {
    NSLog(@“deallocating %@”, self);
    }
    @end
    [/code]
    And, finally, here’s main.m:

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

#define NSLog(FORMAT, …) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##VA_ARGS] UTF8String]);

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++) 
    {
        Employee *person = [[Employee alloc] init];
        // Assign values to the instance variables
        [person setWeightInKilos: 90 + i];
        [person setHeightInMeters:1.8 - i/10.0];
        [person setEmployeeID:i];
 
        [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 *randomEmployee = [employees objectAtIndex: randomIndex];
        [randomEmployee addAssetsObject: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]


#2

You might want to check a posting on the same subject “Not seeing dealloc messages”, started on Oct 27, 2011.

viewtopic.php?f=151&t=3078

I had the same problem and noted that the version of X-code I was running was an old one. I loaded the latest version and used the suggested menu ‘Edit > Refactor > Convert to Objective C ARC’. The program then worked.