Deallocating message not showing up


#1

Hello there,

Everything seems to be working as expected, however the messages for deallocating are not appearing after I remove the Employee with an Index of 5, or setting all employees to nil.

My output:

2013-01-09 06:02:25.862 BMITime[634: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-01-09 06:02:25.864 BMITime[634:303] Giving up ownership of one employee 2013-01-09 06:02:25.864 BMITime[634:303] Giving up ownership of array

From what is shown in the book, I should be seeing lines after the last two, similar to “deallocating <Employee 5: $136 in assets>”, etc.

My code (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 Employee objects
    NSMutableArray *employees = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < 10; i++){
        
        // Create an instance of Employee
        Employee *person = [[Employee alloc] init];
        
        // Give teh instance variables interesting values
        [person setWeightInKilos:90 + i];
        [person setHeightInMeters:1.8 - i/10.0];
        [person setEmployeeID:i];
        
        // Put the employees in teh 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 lable
        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];
        
        // Assign the asset to the employee
        [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]

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;
    }

  • (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);
    [super dealloc];
    }

@end
[/code]

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);
    [super dealloc];
    }

@end[/code]

Thanks for the help!


#2

It could be that your program is exiting too quickly in Xcode for the logs to make it to the console.

Either run your program from the command line or add the following statement to main:

int main (...)
{
   ...
   fflush (NULL);
   return 0;
}

#3

I had the same issue.

After using a couple of break points and finding out that my dealloc methods were not being called. I did a little digging and discovered that if Automatic Reference Counting (ARC) is turned off Xcode then invokes garbage collection (at least that is how I understand it) which does not use the dealloc method and your methods become a no-op. If you turn on ARC you then need to either remove or comment out your [super dealloc] calls you should get the result from the book.


#4

I have the same problem, but when looking at the Build settings, all I see under “Apple LLMV compiler 3.0 - Language” is two entries (re. prefix header), but nothing else, so I can’t check if ARC is invoked in this project or not. Is there something I need to do somewhere to populate this part of the Build settings?


#5

I did some work on this, because I want to get this right! I have been using Xcode 3.5 in OS 10.6.8 and of course there is no ARC, so [super dealloc] was required, and my own dealloc methods were being bypassed, as mpalangio said. But the program ran OK without the deallocating messages.

SO I “got” a copy of Xcode 4.2 with iOS 5 SDK for Snow Leopard (without paying Apple’s fee: why should a Snow Leopard user have to pay for something which a Lion user gets for nothing?) and I copied across the BMI project as modified for ARC investigation, and switched on ARC.

I got an immediate Build Failed messsage: Apple Mach-O Linker (ld) Error: Command /XCode4 Developer/usr/bin/clang failed with exit code 1. No other details that I could find. I’m pretty sure all my code is as per the book. I’m using Apple LLVM compiler 3.0. When I switch off ARC, the program compiles and runs in Xcode 4.2, but of course without the deallocating output.

Is there more I need to do when switching on ARC in the Build Settings area?


#6

Solved. According to Apple, "[color=#800000]The Snow Leopard version of Xcode 4.2 doesn’t support ARC at all on OS X, because it doesn’t include the 10.7 SDK[/color]" so I’m stuffed if I want to trace the workings of ARC using Hillegass (… and Ward :smiley: ). All that effort trying to find a fix, and trying everything I could think of, only to find after days of Goooooogling that it isn’t possible.… :imp: