码迷,mamicode.com
首页 > 移动开发 > 详细

iOS中的内存管理精讲

时间:2015-08-13 19:52:53      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

main.m

#import <Foundation/Foundation.h>
#import "Person.h"
#import "Student.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
//        NSString *name = [[NSString alloc] initWithFormat:@"张三"];
//       // NSLog(@"%lu", [name retainCount]);
//        Person *p = [[Person alloc] init];
//        [p setName:name];
//        [p setName:name];
//        //p.name = name;
//        [name release];
//        
//    
//        
//        
//        
//        NSString *name1 = [[NSString alloc] initWithFormat:@"张四"];
//        [p setName:name1];
//        
//        
//        [p sayHi];
        Student *stu = [[Student alloc] initWithName:@"张三" ];
        //stu - 1
        stu.name = [NSString stringWithFormat:@"习大大"];
        //stu.name -2

        [stu release];
        //stu - 0  stu.name 1
        //分析:[NSString stringWithFormat:@"习大大"]计数器为1
        //stu.name
//        NSLog(@"----%@",stu.name);
//        
//       NSString * name = stu.name = [NSString stringWithFormat:@"习大大"];
//        NSLog(@"%lu",name.retainCount);
//         NSLog(@"----%@",stu.name);
        
        
        
        
        
        
//        NSString *str = [NSString stringWithFormat:@"习大大"];
//        NSLog(@"%lu", str.retainCount);
////        [stu setName:str];
////        stu.name = str;
//        [str retain];
//        //这里的name是堆区 有引用计数
//        stu.name = str;
//        
//        NSLog(@"---%lu", str.retainCount);
//        NSLog(@"%lu",  stu.name.retainCount);
//        NSLog(@"=====%lu",  stu.name.retainCount);
//        [stu release];
        
        
        //这里的string是常量区没有引用计数
//        NSString *string = [[NSString alloc] initWithString:@"小芬"];
//        NSLog(@"%lu",[string retainCount]);
       
        
          }
   
    
    return 0;
}
/*
集合的内存管理:
 1.当向集合中添加元素时,元素的引用计数会被 +1;
 2.当元素从集合中移除时,元素的引用计数会被 -1;
 3.当集合销毁时,会将集合中所有元素 -1;

 



*/

Person.h

#import <Foundation/Foundation.h>

@interface Person : NSObject
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *genter;
@property (nonatomic, assign) NSInteger age;


- (void)sayHi;
@end

 

Person.m

#import "Person.h"

@implementation Person
@synthesize name = _name;
- (void)setName:(NSString *)name
{
//    if (_name != name) {  // 解决了野指针问题
//        [_name release]; // 解决内存泄露问题
//        _name = [name retain]; // 解决野指针问题
//    }
    //为什么不直接赋值,堆区对象的直接赋值是地址的赋值,在mian.m中
    //[name release];
    _name = name;
}
- (NSString *)name
{
    //把_name的计数器加1 放到自动释放池
    //苹果的安全处理机制
    return [[_name retain] autorelease];
}

//copy 内部实现
//- (void)setName:(NSString *)name{
//    if(_name != name)
//    {
//        [_name release];
//        _name = [name copy];
//    }
//}



- (void)sayHi
{
    NSLog(@"%@,%@,%ld", _name, _genter, _age);
}
- (void)dealloc
{
    [_name release]; // 在对象销毁之前,将实例变量的引用计数 -1
    [_genter release];
    NSLog(@"person没有了");
    [super dealloc]; // 调用父类
}
@end

 

Student.h

#import <Foundation/Foundation.h>

@interface Student : NSObject
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *gender;
@property (nonatomic, assign) NSInteger age;


- (id)initWithName:(NSString *)name;
+ (id)studentWithName:(NSString *)name;
@end

Student.m

#import "Student.h"

@implementation Student
//copy 内部实现
//- (void)setName:(NSString *)name{
//    if(_name != name)
//    {
//        [_name release];
//        _name = [name copy];
//    }
//}
@synthesize name = _name;
- (void)setName:(NSString *)name
{
    NSLog(@"%@",_name);
    NSLog(@"%lu",_name.retainCount);
    if (_name != name) {  // 解决了野指针问题
        [_name release]; // 解决内存泄露问题
        NSLog(@"%lu",_name.retainCount);
        _name = [name retain]; // 解决野指针问题
         NSLog(@"%lu",_name.retainCount);
    }
}
- (NSString *)name
{
    //把_name的计数器加1 放到自动释放池
    //苹果的安全处理机制
    return [[_name retain] autorelease];
}
- (id)initWithName:(NSString *)name{
    if (self = [super init]) {
        self.name = name;
        //_name = name;
    }
    return self;
}
+ (id)studentWithName:(NSString *)name{
    Student *stu = [[Student alloc] initWithName:name];
    return [stu autorelease];
}
- (void)dealloc
{
    NSLog(@"Student回收了");
    [super dealloc];
}
@end

 

iOS中的内存管理精讲

标签:

原文地址:http://www.cnblogs.com/wohaoxue/p/4728072.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!