記憶體管理:
student *student = [[student alloc] initwithname:@"小王"];
nslog(@"%lu", [student retaincount]);//1
myclass *myclass = [[myclass alloc] init];
nslog(@"%lu", [myclass retaincount]);//1
[myclass setstudent:student];
nslog(@"%lu", [student retaincount]);//2
nslog(@"%lu", [myclass retaincount]);//1
[student release];
nslog(@"%lu", [student retaincount]);//1
nslog(@"%lu", [myclass retaincount]);//1
[myclass setstudent:student];
nslog(@"%lu", [student retaincount]);//1
nslog(@"%lu", [myclass retaincount]);//1
student *stu = [myclass student];
nslog(@"%lu", [student retaincount]);//2
nslog(@"%lu", [myclass retaincount]);//1
[myclass release];
nslog(@"%lu", [student retaincount]);//1
// nslog(@"%lu", [myclass retaincount]);//crash
[stu sayhi];
2015/2/9 19:36:08
兒子 2015/2/9 19:36:08
// 申請記憶體 逐一釋放(每個人控制自己的開關)把下列輸出裡的stu換成stu1或者stu2也一樣
collegestudent *stu = [[collegestudent alloc] init];
nslog(@"retaincount = %lu", [stu retaincount]);//1
collegestudent *stu1 = [stu retain];// 或者
[stu1 retain];//如果有這一步,變成//1 //3 //4 //3 //2 //1
nslog(@"retaincount = %lu", [stu retaincount]);//2
collegestudent *stu2 = [stu retain];
nslog(@"retaincount = %lu", [stu retaincount]);//3
[stu2 release];
nslog(@"retaincount = %lu", [stu retaincount]);//2
[stu1 release];
nslog(@"retaincount = %lu", [stu retaincount]);//1
[stu release];
nslog(@"retaincount = %lu", [stu retaincount]);//1 //理論是0, 實際是1 在未來某一時刻自動釋放
2015/2/9 19:39:44
兒子 2015/2/9 19:39:44
// 申請記憶體 逐一釋放(每個人控制自己的開關)把下列輸出裡的stu換成stu1或者stu2也一樣
collegestudent *stu = [[collegestudent alloc] init];
nslog(@"retaincount = %lu", [stu retaincount]);//1
collegestudent *stu1 = [stu retain];//功能和[stu1 retain]一樣
nslog(@"retaincount = %lu", [stu retaincount]);//2
[stu1 retain];
nslog(@"retaincount = %lu", [stu retaincount]);//3
collegestudent *stu2 = [stu retain];
nslog(@"retaincount = %lu", [stu retaincount]);//4
[stu2 release];
nslog(@"retaincount = %lu", [stu retaincount]);//3
[stu1 release];
nslog(@"retaincount = %lu", [stu retaincount]);//2
[stu release];
nslog(@"retaincount = %lu", [stu retaincount]);//1 //理論是0, 實際是1 在未來某一時刻自動釋放
OC 記憶體管理的重點
3.1.什麼時候為物件傳送retain訊息.當多1個人使用這個物件的時候,應該先為這個物件傳送retain 訊息.1 3.2 什麼時候為物件傳送releaee訊息.當少1個人使用這個物件的時候.應該為這個物件傳送1條release 訊息.1 3.3 在arc機制下,retain,release,de...
Oc 記憶體管理
1 對你自己擁有的物件負責,你只能釋放你擁有的物件 2 凡是你通過 retain alloc copy等手段獲得了所有者的物件,都必須在你不使用的時候來呼叫release autorelease等手段來釋放對他的所有權 3 在一定的 段內,對同乙個物件所作的copy alloc retain的操作次...
OC記憶體管理
1.記憶體管理原則 如果對乙個物件使用了alloc mutable copy retain,那麼你必須使用相應的release或者autorelease。2.管理範圍 任何繼承了nsobject 的物件,對其他基本資料型別 int char float double struct enum等 無效 ...