Retain Cycle Demo Objects delete


#1

i was doing the Retain Cycle demo and my objects are deleting with out a problem.

BNRItem.h

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

@interface BNRItem : NSObject
{
NSString *itemName;
NSString *serialNumber;
int valueInDollars;
NSDate *dateCreated;

BNRItem *containedItem;
BNRItem *container;

}

+(id)randomItem;

-(id)initWithItemName:(NSString *)name
valueInDollars:(int)value
serialNumber:(NSString *)sNumber;

-(void)setContainedItem:(BNRItem *)i;
-(BNRItem *)containedItem;

-(void)setContainer:(BNRItem *)i;
-(BNRItem *)container;

-(void)setItemName:(NSString *)str;
-(NSString *)itemName;

-(void)setSerialNumber:(NSString *)str;
-(NSString *)serialNumber;

-(void)setValueInDollars:(int)i;
-(int)valueInDollars;

-(NSDate *)dateCreated;
@end
[/code]
BNRItem.m

[code]#import “BNRItem.h”

@implementation BNRItem
-(id)init
{
return [self initWithItemName:@“Item"
valueInDollars:0
serialNumber:@”"];
}

+(id)randomItem
{
NSArray *randomAdjectiveList = [NSArray arrayWithObjects:@“Fluffy”,
@“Rusty”,
@“Shiny”, nil];
NSArray *randomNounList = [NSArray arrayWithObjects:@“Bear”,
@“Spork”,
@“Mac”, nil];
NSInteger adjectiveIndex = rand() % [randomAdjectiveList count];
NSInteger nounIndex = rand() % [randomNounList count];

NSString *randomName = [NSString stringWithFormat:@"%@ %@",
                        [randomAdjectiveList objectAtIndex:adjectiveIndex],
                        [randomNounList objectAtIndex:nounIndex]];
int randomValue = rand() % 100;

NSString *randomSerialNumber = [NSString stringWithFormat:@"%c%c%c%c%c",
                                '0' + rand() % 10,
                                'A' + rand() % 26,
                                '0' + rand() % 10,
                                'A' + rand() % 26,
                                '0' + rand() % 10];

BNRItem *newItem =
[[self alloc] initWithItemName:randomName
                valueInDollars:randomValue
                  serialNumber:randomSerialNumber];
return newItem;

}
-(id)initWithItemName:(NSString *)name
valueInDollars:(int)value
serialNumber:(NSString *)sNumber
{
//Call the Superclass’s Designated initializer
self = [super init];
//Did the superclass’s designated initializer succeed?
if (self){
//Give the Instance variable initial values
[self setItemName:name];
[self setSerialNumber:sNumber];
[self setValueInDollars:value];
dateCreated = [[NSDate alloc] init];
}
//Return the address of the newly initialized object
return self;
}

  • (void)setItemName:(NSString *)str
    {
    itemName = str;
    }

  • (NSString *)itemName
    {
    return itemName;
    }

-(void)setSerialNumber:(NSString *)str
{
serialNumber = str;
}

-(NSString *)serialNumber;
{
return serialNumber;
}

  • (void)setValueInDollars:(int)i
    {
    valueInDollars = i;
    }
    -(int)valueInDollars
    {
    return valueInDollars;
    }

-(NSDate *)dateCreated
{
return dateCreated;
}

-(NSString *)description
{
NSString *descriptionString =
[[NSString alloc] initWithFormat:@"%@ (%@): Worth $%d, recorded on %@",
itemName,
serialNumber,
valueInDollars,
dateCreated];
return descriptionString;
}

  • (void)dealloc
    {
    NSLog(@“Destroyed: %@”, self);
    }

-(void)setContainedItem:(BNRItem *)i
{
containedItem = i;
}

-(BNRItem *)containedItem
{
return containedItem;
}

-(void)setContainer:(BNRItem *)i
{
container = i;
}
-(BNRItem *)container
{
return container;
}
@end
[/code]

main.m

#import <Foundation/Foundation.h>
#import "BNRItem.h"
int main (int argc, const char * argv[])
{

    @autoreleasepool {

        NSMutableArray *items = [[NSMutableArray alloc] init];
//        for (int i = 0; i < 10; i++) {
//            BNRItem *p = [BNRItem randomItem];
//            [items addObject:p];
//        }
//        for(BNRItem *item in items){
//            NSLog(@"%@",item);
//        }
        //destroy the array pointed to by items
        BNRItem *backpack = [[BNRItem alloc] init];
        [backpack setItemName:@"Backpack"];
        [items addObject];
        
        BNRItem *calculator = [[BNRItem alloc] init];
        [calculator setItemName:@"Calculator"];
        [items addObject:calculator];
        
        [backpack setContainedItem:calculator];
         
        
        NSLog(@"Setting Items to Nil...");
        items = nil;
    }
    return 0;
}

terminal output

2012-06-01 22:27:37.704 RandomPossessions[76450:707] Setting Items to Nil...
2012-06-01 22:27:37.723 RandomPossessions[76450:707] Destroyed: Backpack (): Worth $0, recorded on 2012-06-02 02:27:37 +0000
2012-06-01 22:27:37.727 RandomPossessions[76450:707] Destroyed: Calculator (): Worth $0, recorded on 2012-06-02 02:27:37 +0000

#2

That’s because you didn’t include the line

[i setContainer:self];

in the setContainedItem: method.