首页 > 代码库 > copy与内存管理

copy与内存管理

1.copy与内存管理

  • 浅拷贝

    • 原对象引用计数器+1
    • 必须对原对象进行释放
char *cstr = "this is a c string";NSString *str1 = [[NSString alloc] initWithUTF8String:cstr];NSLog(@"str = %lu", [str1 retainCount]);NSString *str2 = [str1 copy];NSLog(@"str = %lu", [str1 retainCount]);[str2 release];必须做一次release
  • 深拷贝

    必须释放新对象

char *cstr = "this is a c string";NSString *str1 = [[NSString alloc] initWithUTF8String:cstr];NSLog(@"str = %lu", [str1 retainCount]);NSMutableString *str2 = [str1 mutableCopy];NSLog(@"str = %lu", [str1 retainCount]);[str2 release]; // 必须做一次release
[str1 release];
 

 

copy与内存管理