Object - C memory manager 연습 및 결과 분석

연습 내용 은 주로 개 한 마리 와 주인 이다.
코드:
//
//  Dog.h
//  HelloDog
//
//  Created by jimzhai on 13-1-31.
//  Copyright (c) 2013  jimzhai. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
@class Person;
@interface Dog : NSObject
{
    int Id;
    NSMutableArray *hostArray;
    NSString *name;
}

@property int Id;

@property(assign) NSString* name;

-(void) addHost: (Person*)person;

-(void)printHosts;

-(void)deleteHost:(Person*) person;

@end//Dog
//
//  Dog.m
//  HelloDog
//
//  Created by jimzhai on 13-1-31.
//  Copyright (c) 2013  jimzhai. All rights reserved.
//

#import "Dog.h"

@implementation Dog

@synthesize Id = Id;

@synthesize name = name;

- (id)init
{
    self = [super init];
    if (self) {
        hostArray = [NSMutableArray new];
        hostArray = [NSMutableArray arrayWithCapacity:10];
        [hostArray retain];//retainCount+1
    }
    return self;
}//init

-(void) addHost:(Person *)person
{
    //[person retain];
    [hostArray addObject:person];
}//addHost

-(void)printHosts
{
    int count = 0;
    for(id p in hostArray)
    {
        NSLog(@"%@",[p name]);
        count++;
    }
    NSLog(@"totalHost: %d",count);
}//printHosts

-(void)deleteHost:(Person *)person
{
    [hostArray removeObject:person];
}//deleteHost

- (void)dealloc
{
    [hostArray release];
    NSLog(@"%@ has been killed!",name);
    [super dealloc];
}//dealloc

@end//Dog
//
//  Person.h
//  HelloDog
//
//  Created by jimzhai on 13-1-31.
//  Copyright (c) 2013  jimzhai. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Dog.h"
@class Dog;
@interface Person : NSObject
{
    NSString *name;
    Dog *dog;
}

@property(assign) NSString *name;

@property(retain) Dog *dog;

@end
//
//  Person.m
//  HelloDog
//
//  Created by jimzhai on 13-1-31.
//  Copyright (c) 2013  jimzhai. All rights reserved.
//

#import "Person.h"
#import "Dog.h"

@implementation Person

@synthesize name = name;

@synthesize dog = dog;

- (void)dealloc
{
    [dog release];
    dog = nil;
    NSLog(@"%@ dealloc!",name);
    [super dealloc];
}

@end
//  main.m
//  HelloDog
//
//  Created by jimzhai on 13-1-31.
//  Copyright (c) 2013  jimzhai. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        
        Dog *dog1 = [[Dog new] init];
        [dog1 setId:89757];
        [dog1 setName:@"good looking"];
        NSLog(@"dog1 retainCount: %ld",[dog1 retainCount]);
        
        Person *xiaoli = [Person alloc];
        NSLog(@"xiaoli retainCount: %ld",[xiaoli retainCount]);
        [xiaoli setName:@"xiaoli"];
        [xiaoli setDog:dog1];
        [dog1 addHost:xiaoli];
        NSLog(@"dog1 retainCount: %ld",[dog1 retainCount]);
        NSLog(@"xiaoli retainCount: %ld",[xiaoli retainCount]);
        
        Person *laowang = [Person alloc];
        NSLog(@"laowang retainCount: %ld",[laowang retainCount]);
        [laowang setName:@"laowang"];
        [laowang setDog:dog1];
        [dog1 addHost:laowang];
        NSLog(@"dog1 retainCount: %ld",[dog1 retainCount]);
        NSLog(@"laowang retainCount: %ld",[laowang retainCount]);
        
        [dog1 printHosts];
        
        [dog1 deleteHost:xiaoli];
        [xiaoli release];
        NSLog(@"xiaoli retainCount: %ld",[xiaoli retainCount]);
        [dog1 printHosts];
        NSLog(@"dog1 retainCount: %ld",[dog1 retainCount]);
        [dog1 deleteHost:laowang];
        [laowang release];
        [dog1 release];
    }
    return 0;
}//main

결과:
2013-01-31 18:12:12.782 HelloDog[3603:303] dog1 retainCount: 1
2013-01-31 18:12:12.784 HelloDog[3603:303] xiaoli retainCount: 1
2013-01-31 18:12:12.794 HelloDog[3603:303] dog1 retainCount: 2
2013-01-31 18:12:12.798 HelloDog[3603:303] xiaoli retainCount: 2
2013-01-31 18:12:12.803 HelloDog[3603:303] laowang retainCount: 1
2013-01-31 18:12:12.805 HelloDog[3603:303] dog1 retainCount: 3
2013-01-31 18:12:12.806 HelloDog[3603:303] laowang retainCount: 2
2013-01-31 18:12:12.807 HelloDog[3603:303] xiaoli
2013-01-31 18:12:12.807 HelloDog[3603:303] laowang
2013-01-31 18:12:12.808 HelloDog[3603:303] totalHost: 2
2013-01-31 18:12:12.809 HelloDog[3603:303] xiaoli dealloc!
2013-01-31 18:12:12.809 HelloDog[3603:303] xiaoli retainCount: 1
2013-01-31 18:12:12.809 HelloDog[3603:303] laowang
2013-01-31 18:12:12.810 HelloDog[3603:303] totalHost: 1
2013-01-31 18:12:12.810 HelloDog[3603:303] dog1 retainCount: 2
2013-01-31 18:12:12.811 HelloDog[3603:303] laowang dealloc!
2013-01-31 18:12:12.811 HelloDog[3603:303] good looking has been killed!

좋은 웹페이지 즐겨찾기