LHF Objective-C语法(9)内存管理

时间:2022-05-03 19:44:38

1、Objc在使用alloc  new  copy 时为内存分配内存,然后返回分配的内存首地址存入指针变量,使用dealloc释放内存

2、[[Fraction alloc] init]  [Fraction new]是相同的

3、

main(){

      A *a = [A new];

      B *b = [B new];

      c *c  = [C new];

      [b m:a];

      [c m:a];

}

上述代码把a传递给了b 、c两个实例,而a实例本身又是在main函数中,因此一共有main函数、b 、c三个地方引用了a实例

引出个问题,在alloc一个对象之后,什么时候dealloc它那?回收早了,可能导致有些还在引用的地方会报错,不回收自然会导致内存泄漏

Objec解决办法是采用一个引用计数retainCount来表示还有多少地方在引用这个对象,一个对象在被alloc之后,retainCount就是1,之后每调用一次retain方法都会是retainCount加1,调用release都会使retainCount减1,当一个对象的引用计数为0时,系统就会调用dealloc回收内存

例1:

int main(){
Fraction *frac = [[Fraction alloc] initWithNumerator:3 denominator:5];
NSLog(@"%d",[frac retainCount]); //retainCount=1

[frac retain];
NSLog(@"%d",[frac retainCount]); //retainCount=2

[frac retain];
NSLog(@"%d",[frac retainCount]); //retainCount=3

[frac release];
NSLog(@"%d",[frac retainCount]); //retainCount=2

[frac release];
NSLog(@"%d",[frac retainCount]); //retainCount=1

[frac release];//retainCount=0
//[frac print];//这时候调用,会引起程序崩溃,因为frac指向的内存被系统回收了
frac = nil;//这样就不会报错了,当然它也不会打印出东西来
[frac print];
}

例2:

//======================================================
#import <Foundation/Foundation.h>
@interface Address:NSObject{
NSString *city;
NSString *street;
}
-(void) setCity:(NSString*) c;
-(void) setStreet:(NSString*) s;
-(void) setCity:(NSString*) c andStreet:(NSString*) s;
-(NSString*) city;
-(NSString*) street;
@end
//======================================================
#import "Address.h"
@implementation Address
-(void) setCity:(NSString*) c{
[c retain];
[city release];
city = c;
}
-(void) setStreet:(NSString*) s{
[s retain];
[street release];
street = s;
}
-(void) setCity:(NSString*) c andStreet:(NSString*) s{
[self setCity:c];
[self setStreet:s];
}
-(NSString*) city{
return city;
}
-(NSString*) street{
return street;
}
-(void)dealloc{
[city release];
[street release];
[super dealloc];
}
@end

//main======================================================
NSString *city = [[NSString alloc] initWithString:@"Beijing"];
NSString *street = [[NSString alloc] initWithCString:"Jinsongzhongjie"];
Address *address = [[Address alloc] init];
[address setCity:city andStreet:street];

[city release];
[street release];
[address release];
1、在main函数中,创建了city  street两个实例,分配了内存,势必要release它们

2、当release完之后city street的retainCount变成0,这就会导致传到address类里的city、street指针指向的对象被清理掉,而使address出错

3、很显然,你需要在address的setter方法里retain一下,使city street的引用计数为2,这样main函数release后,city street 指向的内存也不会被dealloc掉

-(void) setCity:(NSString*) c{
[c retain];
[city release];
city = c;
}
1、第2行,首先retain一下,使引用计数器+1,(拿到对象的所有权)

2、第3行,防止city可能已经指向了一个对象,如果不先对city进行一次release,而直接把city指向c指向的对象,那么city原来指向的对象可能会出现内存泄漏

3、

-(void)dealloc{
[city release];
[street release];
[super dealloc];
}
1、因为你在settter中持有了city street指向对象的所有权,那么你势必要和main函数一样,release你的所有权

NSAutoreleasePool

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

Fraction *frac = [[Fraction alloc] initWithNumerator:3 denominator:5];

NSLog(@"frac的引用计数 %d",[frac retainCount]);    //1

[frac retain];

NSLog(@"frac的引用计数 %d",[frac retainCount]);    //2

[frac autorelease];

NSLog(@"frac的引用计数 %d",[frac retainCount]);    //2

[frac autorelease];

NSLog(@"frac的引用计数 %d",[frac retainCount]);    //2

[pool release];

autorelease方法并不对引用计数减1,而是将对象压入离他最近的NSAutoreleasePool栈顶,等到NSAutoreleasePool的release方法被调用后,

pool会把对象release掉