#import <Foundation/Foundation.h>
//.txt
//.data
//.bss
//.heap
//.stack
//引用计数
//count = 10
//count --;
//count = 0
int main(int argc, const char * argv[]) {
@autoreleasepool {
char *pstr = malloc(1000);
char *pstr1 = pstr;
char *pstr2 = pstr;
scanf("%s", pstr);
printf("%s\n", pstr);
free(pstr);
pstr = NULL;
}
return 0;
}
————————————————————————————————————分割————————————————————————————
#import <Foundation/Foundation.h>
@interface Dog : NSObject
@end
#import "Dog.h"
@implementation Dog
- (void)dealloc
{
NSLog(@"对象被释放");
[super dealloc];
}
@end
#import <Foundation/Foundation.h>
#import "Dog.h"
//ARC automatic reference counting //自动引用计数
//MRC manaul reference counting //手动引用计数
//对于引用计数来说,有一套内存管理的黄金法则:
//The basic rule to apply is everything that increases the reference counter with alloc, [mutable]copy[withZone:] or retain is in charge of the corresponding [auto]release.
//如果对一个对象使用了alloc、[mutable]copy、retain,那么你必须使用相应的release或者autorelease。
//通俗一点的说法就是谁污染谁治理,谁杀的谁埋。
int main(int argc, const char * argv[]) {
@autoreleasepool {
Dog *xiaoHei = [[Dog alloc] init];
//[xiaoHei release];//release 计数器-1 自动调用dealloc
}
NSLog(@"程序退出");
return 0;
}
————————————————————————————————————分割————————————————————————————
#import <Foundation/Foundation.h>
@interface Dog : NSObject
@end
#import "Dog.h"
@implementation Dog
@end
//对于引用计数来说,有一套内存管理的黄金法则:
//The basic rule to apply is everything that increases the reference counter with alloc, [mutable]copy[withZone:] or retain is in charge of the corresponding [auto]release.
//如果对一个对象使用了alloc、[mutable]copy、retain,那么你必须使用相应的release或者autorelease。
//通俗一点的说法就是谁污染谁治理,谁杀的谁埋。
//创建一个对象, 对应的引用计数 retainCount
#import <Foundation/Foundation.h>
#import "Dog.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Dog *xiaoHei = [[Dog alloc] init];
NSLog(@"xiaoHei = %ld", xiaoHei.retainCount);//获取引用计数值
//retain : 引用计数进项保留(增加对对象的拥有权), 引用计数加1
Dog *xiaoBai = [xiaoHei retain];
NSLog(@"xiaoBai = %ld", xiaoBai.retainCount);
Dog *xiaoHua = [xiaoHei retain];
NSLog(@"xiaoHua = %ld", xiaoHua.retainCount);
[xiaoHei release];//使引用计数减1
xiaoHei = nil;
NSLog(@"retainCount = %ld", xiaoBai.retainCount);
[xiaoBai release];
NSLog(@"retainCount = %ld", xiaoHua.retainCount);
[xiaoHua release];//dealloc
xiaoHua = nil;
NSLog(@"今天天气不错, 是出游的日子");
NSLog(@"今天天气不错, 是出游的日子");
NSLog(@"今天天气不错, 是出游的日子");
NSLog(@"今天天气不错, 是出游的日子");
NSLog(@"今天天气不错, 是出游的日子");
NSLog(@"今天天气不错, 是出游的日子");
NSLog(@"今天天气不错, 是出游的日子");
NSLog(@"今天天气不错, 是出游的日子");
NSLog(@"今天天气不错, 是出游的日子");
sleep(1);
//NSLog(@"retainCount = %ld", xiaoHua.retainCount);//当一个对象被释放后, 不应该再给它发送消息
}
return 0;
}
————————————————————————————————————分割————————————————————————————
#import <Foundation/Foundation.h>
#import "Dog.h"
@interface Person : NSObject
{
Dog *_dog;
}
- (void)setDog:(Dog *)newDog;
- (Dog *)dog;
- (void)dealloc;
@end
#import "Person.h"
@implementation Person
//写法一 不OK
//- (void)setDog:(Dog *)dog
//{
// _dog = dog;
//}
//写法二
//- (void)setDog:(Dog *)dog
//{
// _dog = [dog retain];
//}
//写法三
//- (void)setDog:(Dog *)dog
//{
// [_dog release];
// _dog = [dog retain];
//}
//标准写法
- (void)setDog:(Dog *)dog
{
if (_dog!=dog) {
[_dog release];
_dog =[dog retain];
}
}
- (Dog *)dog
{
return _dog;
}
- (void)dealloc
{
NSLog(@"dog dealloc");
[super dealloc];
}
@end
#import <Foundation/Foundation.h>
@interface Dog : NSObject
@end
#import "Dog.h"
@implementation Dog
@end
#import <Foundation/Foundation.h>
#import "Dog.h"
#import "Person.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
//写法一
//xiaohei 被释放, 那么per.dog被释放
// Dog *xiaoHei = [[Dog alloc] init];
// Person *per = [[Person alloc] init];
// per.dog = xiaoHei;
//
// [xiaoHei release];
//
// [per release];
//写法二, 如果人再持有别的狗,就会造成第一条狗得不到释放,内存泄露
// Dog *xiaoHei = [[Dog alloc] init];
// Dog *xiaoBai = [[Dog alloc] init];
//
// Person *per = [[Person alloc] init];
// per.dog = xiaoHei ;
// NSLog(@"count = %ld", xiaoHei.retainCount);
// [xiaoHei release];//count =1;
// per.dog = xiaoBai;
//
// [xiaoBai release];
// [per release];
//写法三
//如果本来持有一条狗,又重新设置这条狗,先进行release,这个时候,很可能dog就销毁了,然后,就没法再次retain了
// Dog *xiaoHei = [[Dog alloc] init];
// Dog *xiaoBai = xiaoHei;
// Person *per = [[Person alloc] init];
// per.dog = xiaoHei;
// per.dog = xiaoBai;
}
return 0;
}
————————————————————————————————————分割————————————————————————————
#import <Foundation/Foundation.h>
#import "Dog.h"
@interface Person : NSObject
{
NSString *_name;
int _age;
Dog *_dog;
}
//assign 直接赋值 int float long long long (基本数据类型)
//@property (nonatomic, assign) Dog *dog;
@property (nonatomic, assign)int age;
@property (nonatomic, retain)Dog *dog;
@property (nonatomic, copy)NSString *name;
@end
#import "Person.h"
@implementation Person
//assgin
- (void)setAge:(int)age
{
_age = age;
}
//retain (自定类 类定义的变量)
- (void)setDog:(Dog *)dog
{
if (_dog!=dog) {
[_dog release];
_dog =[dog retain];
}
}
//copy NSString NSArray NSDirctionary
- (void)setName:(NSString *)name
{
if (_name != name) {
[_name release];
_name = [name copy];
}
}
@end
#import <Foundation/Foundation.h>
@interface Dog : NSObject
@end
#import "Dog.h"
@implementation Dog
@end
#import <Foundation/Foundation.h>
#import "Dog.h"
#import "Person.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
//NSString *str= @"xiaoHei";
//NSMutableString *str= [NSMutableString stringWithFormat:@"xiaoHei"];
char *pstr = malloc(100);
strcpy(pstr, "xiaoHei");
NSString *str = [NSString stringWithUTF8String:pstr];
//NSString *str = [NSString stringWithFormat:@"%@",str];
Person *per = [[Person alloc] init];
per.name = str;
NSLog(@"str = %p", str);
NSLog(@"name = %p", per.name);
}
return 0;
}
————————————————————————————————————分割————————————————————————————
#import <Foundation/Foundation.h>
//copy
//mutableCopy
int main(int argc, const char * argv[]) {
@autoreleasepool {
/*
//copy
//copy 把可变对象或者不可变对象, 拷贝生成不可变对象
NSString *str = @"helloworld";
NSMutableString *mulStr = [NSMutableString stringWithString:str];
id obj1 = [str copy];
id obj2 = [mulStr copy];
//NSLog(@"str = %p", str);
//NSLog(@"obj = %p", obj1);
NSLog(@"mulStr = %p", mulStr);
NSLog(@"obj2 = %p", obj2);
NSLog(@"obj2 = %@", obj2);
//[obj appendString:@"zhongguo"];
//[obj2 appendString:@"zhongguo"] ;
//NSLog(@"%@", obj);
//[obj release];
[obj2 release];
*/
//multableCopy
//把可变对象或者不可变对象, 拷贝生成可变对象
NSString *str = @"qiangfeng";
NSMutableString *mulStr = [NSMutableString stringWithString:str];
id obj = [str mutableCopy];
id obj2 = [mulStr mutableCopy];
NSLog(@"%@", obj);
NSLog(@"obj2 = %@", obj2);
[obj appendString:@"helloworld"];
NSLog(@"%@", obj);
[obj2 appendString:@"1000phone"];
NSLog(@"obj2 = %@", obj2);
}
return 0;
}
————————————————————————————————————分割————————————————————————————
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//字符串内存管理, retainCount 的值不定, 只管遵守内存管理法则
NSString *str = @"qianfeng";//常量区
NSLog(@"str = %ld", str.retainCount);
NSString *str1 = [[NSString alloc] initWithString:str];
NSLog(@"%p %p", str, str1);
NSLog(@"str1 = %ld", str1.retainCount);
NSString *str2 = [[NSString alloc] initWithFormat:@"%@,%d", str, 12345];
//str2 对象存储在堆中
NSLog(@"str2 = %ld", str2.retainCount);
NSString *str3 = [str retain];
NSLog(@"str3 = %ld", str3.retainCount);
NSString *str4 = [str1 retain];
NSLog(@"str4 = %ld", str4.retainCount);
NSString *str5 = [str2 retain];
NSLog(@"str5 = %ld", str5.retainCount);
NSMutableString *str6 = [[NSMutableString alloc] initWithString:str];
NSLog(@"str6 = %ld", str6.retainCount);
NSMutableString *str7 = [str6 retain];
NSLog(@"str7 = %ld", str7.retainCount);
[str1 release];
[str2 release];
[str3 release];
[str4 release];
[str5 release];
[str6 release];
[str7 release];
}
return 0;
}
————————————————————————————————————分割————————————————————————————
#import <Foundation/Foundation.h>
@interface Dog : NSObject
@end
#import "Dog.h"
@implementation Dog
@end
#import <Foundation/Foundation.h>
#import "Dog.h"
//数组
int main(int argc, const char * argv[]) {
@autoreleasepool {
Dog *xiaoHei = [[Dog alloc] init];
Dog *xiaoBai = [[Dog alloc] init];
Dog *xiaoHua = [[Dog alloc] init];
Dog *xiaoHuang = [[Dog alloc] init];
NSLog(@"xiaoHei = %ld", xiaoHei.retainCount);
NSLog(@"xiaoBai = %ld", xiaoBai.retainCount);
NSLog(@"xiaoHua = %ld", xiaoHua.retainCount);
NSLog(@"xiaoHuang = %ld", xiaoHuang.retainCount);
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:xiaoBai,xiaoHei,xiaoHua,xiaoHuang, nil];
NSLog(@"xiaoHei = %ld", xiaoHei.retainCount);
NSLog(@"xiaoBai = %ld", xiaoBai.retainCount);
NSLog(@"xiaoHua = %ld", xiaoHua.retainCount);
NSLog(@"xiaoHuang = %ld", xiaoHuang.retainCount);
[arr addObject:xiaoBai];
NSLog(@"xiaoBai = %ld", xiaoBai.retainCount);
[arr addObject:xiaoBai];
NSLog(@"xiaoBai = %ld", xiaoBai.retainCount);
//当往数组中添加对象, 对应的对象的引用计数加1
NSLog(@"len = %ld", [arr count]);
[xiaoBai release];
[xiaoHei release];
[xiaoHua release];
[xiaoHuang release];
NSLog(@"xiaoHei = %ld", xiaoHei.retainCount);
NSLog(@"xiaoBai = %ld", xiaoBai.retainCount);
NSLog(@"xiaoHua = %ld", xiaoHua.retainCount);
NSLog(@"xiaoHuang = %ld", xiaoHuang.retainCount);
[arr release];//把数组中的每个元素,引用计数减1
//NSLog(@"xiaoHei = %ld", xiaoHei.retainCount);
//NSLog(@"xiaoBai = %ld", xiaoBai.retainCount);
//NSLog(@"xiaoHua = %ld", xiaoHua.retainCount);
//NSLog(@"xiaoHuang = %ld", xiaoHuang.retainCount);
}
return 0;
}
————————————————————————————————————分割————————————————————————————
#import <Foundation/Foundation.h>
@interface Dog : NSObject
@end
#import "Dog.h"
@implementation Dog
@end
#import <Foundation/Foundation.h>
#import "Dog.h"
//autorelease autoreleasepool
int main(int argc, const char * argv[]) {
@autoreleasepool {//自动释放池
Dog *xiaoHei = [[Dog alloc] init];
//[xiaoHei release];
[xiaoHei autorelease];//把xiaoHei的拥有权给自动释放池, 自动池帮我们释放对象内存
//自动释放池类似数组,自动释放池把其中的每个对象都release一次
NSLog(@"count = %ld", xiaoHei.retainCount);
}
@autoreleasepool {
}
@autoreleasepool {
}
return 0;
}
————————————————————————————————————分割————————————————————————————
#import <Foundation/Foundation.h>
@interface Dog : NSObject
+(Dog *)DogCreat;
@end
#import "Dog.h"
@implementation Dog
//在类方法里面也要遵守内存管理法则
+ (Dog *)DogCreat
{
Dog *dog = [[self alloc] init];
//[dog release]; 不能调用
[dog autorelease];
return dog;
}
@end
#import <Foundation/Foundation.h>
#import "Dog.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Dog *dog = [Dog DogCreat];
}
return 0;
}