Console not showing dealloc


#1

I seem to have the exact same code as the book, but this is all I get from the console when I run BMITime:

2013-03-28 12:25:59.595 BMITime[24172: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-03-28 12:25:59.597 BMITime[24172:303] Giving up ownership of one employee 2013-03-28 12:25:59.597 BMITime[24172:303] Giving up ownership of array

Am I missing something? Did XCode 4.6 change anything that makes this not work anymore?

Thanks, guys.


#2

Runs fine in 4.6.1.

If you want to post your files, I can take a quick peak at your code.


#3

Thank you so much for doing that. Here it is:

Person.h

[code]#import <Foundation/Foundation.h>

// The class Person inherits all the instance variables and methods defined by the class NSObject
@interface Person : NSObject
{
// It has two instance variables
float heightInMeters;
int weightInKilos;
}

/*
// You will be able to set those instance variables using these methods

  • (float)heightInMeters;
  • (void)setHeightInMeters:(float)h;
  • (int)weightInKilos;
  • (void)setWeightInKilos:(int)w; // these are setter and getter methods
    */

@property float heightInMeters; // The @property replaces the methods declared above. setter and getter methods all in one line
@property int weightInKilos;

// This method calculates the Body Mass Index

  • (float)bodyMassIndex;

@end[/code]

Person.m

[code]#import “Person.h”

@implementation Person

@synthesize heightInMeters, weightInKilos; // This is making the syntax easier than typing all that out below. simpler text

/*

  • (float)heightInMeters
    {
    return heightInMeters;
    }

  • (void)setHeightInMeters:(float)h
    {
    heightInMeters = h;
    }

  • (int)weightInKilos
    {
    return weightInKilos;
    }

  • (void)setWeightInKilos:(int)w
    {
    weightInKilos = w;
    }

*/

  • (float)bodyMassIndex
    {
    // return weightInKilos / (heightInMeters * heightInMeters);
    float h = [self heightInMeters];
    return [self weightInKilos] / (h * h); // This is another way to write what i did in line 40. using the local variable ‘self’
    }

@end[/code]

Employee.h

[code]#import <Foundation/Foundation.h>
#import “Person.h”
@class Asset;

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

@property int employeeID;

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

@end[/code]

Employee.m (Note: triggers “Method possibly missing a [super dealloc] call” alert)

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

@end[/code]

Asset.h

[code]#import <Foundation/Foundation.h>

@interface Asset : NSObject
{
NSString *label;
unsigned int resaleValue;
}

@property (strong) NSString *label; // (strong) means ‘This is a pointer to an object upon which I claim ownership’
@property unsigned int resaleValue;

@end[/code]

Asset.m (Note: triggers “Method possibly missing a [super dealloc] call” alert)

[code]#import “Asset.h”

@implementation Asset

@synthesize label, resaleValue;

  • (NSString *)description // the ‘description’ method from NSObject returns a string that is a useful description of an instance of the class.
    {
    return [NSString stringWithFormat:@"<%@: $%d >", [self label], [self resaleValue]];
    // I used the ‘description’ method to return a string that includes the instance’s ‘label’ and ‘resaleValue’
    }

  • (void)dealloc
    {
    NSLog(@“deallocating %@”, self);
    }
    @end[/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 the instance variables interesting values
        [person setWeightInKilos: 90 + i];
        [person setHeightInMeters: 1.8 - i/10.0];
        [person setEmployeeID: i];
        
        // Put the employee 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 employeee
        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]


#4

I am getting he same results as the original poster (plankton). The NSLof statement in the Employee.m file appears to be ignored.


#5

Based on the debug output and the trigger references (method possibly missing [super dealloc] call), I believe that when you created BMITime you forgot to check off the:
“Use Automatic Reference Counting” (also referred to as ARC) box.

When ARC is off, my output matches yours. When it’s on, the program deallocs as expected.

Click on your project, then click on build settings and type in: CLANG_ENABLE_OBJC_ARC into the search box. See if the result is yes or no.


#6

[quote=“Tiberius”]Based on the debug output and the trigger references (method possibly missing [super dealloc] call), I believe that when you created BMITime you forgot to check off the:
“Use Automatic Reference Counting” (also referred to as ARC) box.

When ARC is off, my output matches yours. When it’s on, the program deallocs as expected.

Click on your project, then click on build settings and type in: CLANG_ENABLE_OBJC_ARC into the search box. See if the result is yes or no.[/quote]

Tiberius, you got it. Works perfectly now.

Thank you.


#7

[quote=“Tiberius”]Based on the debug output and the trigger references (method possibly missing [super dealloc] call), I believe that when you created BMITime you forgot to check off the:
“Use Automatic Reference Counting” (also referred to as ARC) box.

When ARC is off, my output matches yours. When it’s on, the program deallocs as expected.

Click on your project, then click on build settings and type in: CLANG_ENABLE_OBJC_ARC into the search box. See if the result is yes or no.[/quote]

Thank you - this solved my problem too!


#8

I had the same problem as the 2 people above. My problem was also solved by changing the no to yes.
Is it good practice to always have the ARC box checked YES?


#9

[quote=“daman73”]I had the same problem as the 2 people above. My problem was also solved by changing the no to yes.
Is it good practice to always have the ARC box checked YES?[/quote]

Yes it makes life so much easier not having to worry about memory management in such detail. (Retain / release ) etc.
All the code in the book assumes you’re using ARC.


#10

Thanks, I was having the same problem. I had started the project using a pre-ARC version of Xcode, so the option defaulted to “no.”