// NSString *string = @"origion";//此时所有的retaincount都为-1,retain和release操作都没有效用
// NSString *string = [NSString stringWithFormat:@"%@",@"origion"];//静态方法。创建对象,内存由系统负责回收
NSString *string = [[NSString alloc]initWithFormat:@"%@",@"origion"];//alloc创建的对象,内存由用户手动回收
NSLog(@"stringP = %p,stringContentP=%p,retainCount = %d",&string,string,[string retainCount]);
/*
2015-04-09 15:06:04.914 BlockDemo[5621:60b] stringP = 0xbfffc930,stringContentP=0x8e6e920,retainCount = 1
*/
//操作一:retain
NSString *retainString = [string retain];//拷贝了指针,没有拷贝内容,两个对象指针 指向同一块内存,retaincount 都为2
NSLog(@"1retain--stringP = %p,stringContentP=%p,retainCount = %d",&string,string,[string retainCount]);
NSLog(@"1retain--retainStringP = %p,retainStringContentP=%p,retainCount = %d",&retainString,retainString,[retainString retainCount]);
/*
2015-04-09 15:06:04.915 BlockDemo[5621:60b] 1retain--stringP = 0xbfffc930,stringContentP=0x8e6e920,retainCount = 2
2015-04-09 15:06:04.916 BlockDemo[5621:60b] 1retain--retainStringP = 0xbfffc92c,retainStringContentP=0x8e6e920,retainCount = 2
*/
//操作二:retain
NSString *retainString2 = [retainString retain];//拷贝了指针,没有拷贝内容,两个对象指针 指向同一块内存,retaincount 都为2
NSLog(@"2retain--stringP = %p,stringContentP=%p,retainCount = %d",&string,string,[string retainCount]);
NSLog(@"2retain--retainStringP = %p,retainStringContentP=%p,retainCount = %d",&retainString,retainString,[retainString retainCount]);
NSLog(@"2retain--retainString2P = %p,retainString2ContentP=%p,retainCount = %d",&retainString2,retainString2,[retainString2 retainCount]);
/*
2015-04-09 15:06:04.916 BlockDemo[5621:60b] 2retain--stringP = 0xbfffc930,stringContentP=0x8e6e920,retainCount = 3
2015-04-09 15:06:04.917 BlockDemo[5621:60b] 2retain--retainStringP = 0xbfffc92c,retainStringContentP=0x8e6e920,retainCount = 3
2015-04-09 15:06:04.917 BlockDemo[5621:60b] 2retain--retainString2P = 0xbfffc928,retainString2ContentP=0x8e6e920,retainCount = 3
*/
//操作三:release
[retainString2 release];//retain后的对象,中得任何一个对象release都会使得retaincount减一
NSLog(@"3release--stringP = %p,stringContentP=%p,retainCount = %d",&string,string,[string retainCount]);
NSLog(@"3release--retainStringP = %p,retainStringContentP=%p,retainCount = %d",&retainString,retainString,[retainString retainCount]);
NSLog(@"3release--retainString2P = %p,retainString2ContentP=%p,retainCount = %d",&retainString2,retainString2,[retainString2 retainCount]);
/*
2015-04-09 15:06:04.918 BlockDemo[5621:60b] 3release--stringP = 0xbfffc930,stringContentP=0x8e6e920,retainCount = 2
2015-04-09 15:06:04.918 BlockDemo[5621:60b] 3release--retainStringP = 0xbfffc92c,retainStringContentP=0x8e6e920,retainCount = 2
2015-04-09 15:06:04.918 BlockDemo[5621:60b] 3release--retainString2P = 0xbfffc928,retainString2ContentP=0x8e6e920,retainCount = 2
*/
//操作四:copy
NSString *retainString3 = [retainString copy];//浅拷贝或者是弱引用,这里只拷贝指针,拷贝后指针和之前的指向的是同一块内存,自然输出的内容也一致
NSLog(@"4copy--stringP = %p,stringContentP=%p,retainCount = %d",&string,string,[string retainCount]);
NSLog(@"4copy--retainStringP = %p,retainStringContentP=%p,retainCount = %d",&retainString,retainString,[retainString retainCount]);
NSLog(@"4copy--retainString2P = %p,retainString2ContentP=%p,retainCount = %d",&retainString2,retainString2,[retainString2 retainCount]);
NSLog(@"4copy--retainString3P = %p,retainString3ContentP=%p,retainCount = %d",&retainString3,retainString3,[retainString3 retainCount]);
/*
2015-04-09 15:06:04.919 BlockDemo[5621:60b] 4copy--stringP = 0xbfffc930,stringContentP=0x8e6e920,retainCount = 3
2015-04-09 15:06:04.919 BlockDemo[5621:60b] 4copy--retainStringP = 0xbfffc92c,retainStringContentP=0x8e6e920,retainCount = 3
2015-04-09 15:06:04.920 BlockDemo[5621:60b] 4copy--retainString2P = 0xbfffc928,retainString2ContentP=0x8e6e920,retainCount = 3
2015-04-09 15:06:04.921 BlockDemo[5621:60b] 4copy--retainString3P = 0xbfffc924,retainString3ContentP=0x8e6e920,retainCount = 3
*/
//操作四:release
[retainString3 release];//因为retain后的对象和原来的对象的内容是同一块内存,所以任何一个对象release一次,都会导致这块内存的引用次数减一
NSLog(@"5release--stringP = %p,stringContentP=%p,retainCount = %d",&string,string,[string retainCount]);
NSLog(@"5release--retainStringP = %p,retainStringContentP=%p,retainCount = %d",&retainString,retainString,[retainString retainCount]);
NSLog(@"5release--retainString2P = %p,retainString2ContentP=%p,retainCount = %d",&retainString2,retainString2,[retainString2 retainCount]);
NSLog(@"5release--retainString3P = %p,retainString3ContentP=%p,retainCount = %d",&retainString3,retainString3,[retainString3 retainCount]);
/*
2015-04-09 15:06:04.921 BlockDemo[5621:60b] 5release--stringP = 0xbfffc930,stringContentP=0x8e6e920,retainCount = 2
2015-04-09 15:06:04.922 BlockDemo[5621:60b] 5release--retainStringP = 0xbfffc92c,retainStringContentP=0x8e6e920,retainCount = 2
2015-04-09 15:06:04.922 BlockDemo[5621:60b] 5release--retainString2P = 0xbfffc928,retainString2ContentP=0x8e6e920,retainCount = 2
2015-04-09 15:06:04.923 BlockDemo[5621:60b] 5release--retainString3P = 0xbfffc924,retainString3ContentP=0x8e6e920,retainCount = 2
*/
//操作五:mutableCopy
NSString *retainString4 = [retainString mutableCopy];//深copy会产生新的对象,指针和存放内容的地址都拷贝了一份儿新的,原来的对象的retaincount不变,新对象的retaincount为1
NSLog(@"6mutableCopy--stringP = %p,stringContentP=%p,retainCount = %d",&string,string,[string retainCount]);
NSLog(@"6mutableCopy--retainStringP = %p,retainStringContentP=%p,retainCount = %d",&retainString,retainString,[retainString retainCount]);
NSLog(@"6mutableCopy--retainString2P = %p,retainString2ContentP=%p,retainCount = %d",&retainString2,retainString2,[retainString2 retainCount]);
NSLog(@"6mutableCopy--retainString3P = %p,retainString3ContentP=%p,retainCount = %d",&retainString3,retainString3,[retainString3 retainCount]);
NSLog(@"6mutableCopy--retainString4P = %p,retainString4ContentP=%p,retainCount = %d",&retainString4,retainString4,[retainString4 retainCount]);
/*
2015-04-09 15:06:04.923 BlockDemo[5621:60b] 6mutableCopy--stringP = 0xbfffc930,stringContentP=0x8e6e920,retainCount = 2
2015-04-09 15:06:04.924 BlockDemo[5621:60b] 6mutableCopy--retainStringP = 0xbfffc92c,retainStringContentP=0x8e6e920,retainCount = 2
2015-04-09 15:06:04.924 BlockDemo[5621:60b] 6mutableCopy--retainString2P = 0xbfffc928,retainString2ContentP=0x8e6e920,retainCount = 2
2015-04-09 15:06:04.924 BlockDemo[5621:60b] 6mutableCopy--retainString3P = 0xbfffc924,retainString3ContentP=0x8e6e920,retainCount = 2
2015-04-09 15:06:04.925 BlockDemo[5621:60b] 6mutableCopy--retainString4P = 0xbfffc920,retainString4ContentP=0x8e6f930,retainCount = 1
*/
//操作六:release
[retainString4 release];
NSLog(@"7release--stringP = %p,stringContentP=%p,retainCount = %d",&string,string,[string retainCount]);
NSLog(@"7release--retainStringP = %p,retainStringContentP=%p,retainCount = %d",&retainString,retainString,[retainString retainCount]);
NSLog(@"7release--retainString2P = %p,retainString2ContentP=%p,retainCount = %d",&retainString2,retainString2,[retainString2 retainCount]);
NSLog(@"7release--retainString3P = %p,retainString3ContentP=%p,retainCount = %d",&retainString3,retainString3,[retainString3 retainCount]);
// NSLog(@"7mutableCopy--retainString4P = %p,retainString4ContentP=%p,retainCount = %d",&retainString4,retainString4,[retainString4 retainCount]);//此时对象已经不存在,release 会导致访问野指针,造成crash
/*
2015-04-09 15:06:04.925 BlockDemo[5621:60b] 7release--stringP = 0xbfffc930,stringContentP=0x8e6e920,retainCount = 2
2015-04-09 15:06:04.926 BlockDemo[5621:60b] 7release--retainStringP = 0xbfffc92c,retainStringContentP=0x8e6e920,retainCount = 2
2015-04-09 15:06:04.926 BlockDemo[5621:60b] 7release--retainString2P = 0xbfffc928,retainString2ContentP=0x8e6e920,retainCount = 2
2015-04-09 15:06:04.926 BlockDemo[5621:60b] 7release--retainString3P = 0xbfffc924,retainString3ContentP=0x8e6e920,retainCount = 2
(lldb)
*/