V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
adow
V2EX  ›  iDev

关于copyWithZone的写法

  •  
  •   adow ·
    adow · 2012-05-04 20:41:32 +08:00 · 8525 次点击
    这是一个创建于 4381 天前的主题,其中的信息可能已经有所发展或是发生改变。
    之前我一直没注意过,今天在instruments中运行发现我写的copyWithZone 都有leaks,比如一个很简单的类:
    .h 里面

    @interface PostImageModal : NSObject{
    NSString *small;
    NSString *medium;
    NSString *big;
    }
    @property (nonatomic,copy) NSString *small;
    @property (nonatomic,copy) NSString *medium;
    @property (nonatomic,copy) NSString *big;
    @end

    .m 里面

    @implementation PostImageModal
    @synthesize small;
    @synthesize medium;
    @synthesize big;
    -(id)copyWithZone:(NSZone*)zone{
    PostImageModal *copy=[[[self class] allocWithZone:zone]init];
    copy.small=small;
    copy.medium=medium;
    copy.big=big;
    return copy;
    }
    -(void)dealloc{
    [small release];
    [medium release];
    [big release];
    [super dealloc];
    }
    @end

    运行的是instruments里提示 [PostImageModal copyWithZone:] leaks, 我不清楚哪里的问题啊,有人遇到么?
    13 条回复    1970-01-01 08:00:00 +08:00
    Kai
        1
    Kai  
    MOD
       2012-05-05 00:58:12 +08:00   ❤️ 1
    试试看 [copy autorelease];
    kejinlu
        2
    kejinlu  
       2012-05-05 01:02:05 +08:00   ❤️ 1
    你这个很明显的内存泄露啊,谁创建谁负责释放,所以 return [copy autorelease]; 或者创建的时候 PostImageModal *copy=[[[[self class] allocWithZone:zone] init] autorelease];
    Smartype
        3
    Smartype  
       2012-05-05 10:34:57 +08:00   ❤️ 1
    @kejinlu 然后assignee to retain????
    这是copyWithZone方法
    adow
        4
    adow  
    OP
       2012-05-05 10:46:21 +08:00
    无论是 PostImageModal *copy=[[[[self class] allocWithZone:zone] init] autorelease]; 还是 retur [copy autorelease];

    在Analyze 的时候都是:Object with a +0 retain count returned to caller where a+1 (owning) retain count is expected

    @Kai @kejinlu
    Smartype
        5
    Smartype  
       2012-05-05 10:52:08 +08:00
    @adow 检查赋值当然地方,自己算算retaincount就明白了
    adow
        6
    adow  
    OP
       2012-05-05 11:11:34 +08:00
    @Smartype 我有点糊涂啊,是说这个里面写的是对的,外面用上copy的地方有问题吗?
    mr_pppoe
        7
    mr_pppoe  
       2012-05-05 11:39:01 +08:00   ❤️ 1
    @kejinlu copyXXX不是应该返回一个retainCount=1的object? 我觉得这段没有问题啊?
    xesique
        8
    xesique  
       2012-05-05 12:04:01 +08:00   ❤️ 1
    依照Objective-C方法命名的惯例,调用以alloc、new、copy、mutableCopy等开头的方法名时,需要由调用方进行release操作。因此,copyWithZone:的实现应该不应返回autorelease的对象。

    参见
    https://discussions.apple.com/thread/296134?threadID=296134
    https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html
    kejinlu
        9
    kejinlu  
       2012-05-05 12:06:19 +08:00   ❤️ 1
    嗯 应该是调用方去释放 我之前说错了
    kejinlu
        10
    kejinlu  
       2012-05-05 12:14:26 +08:00
    @mr_pppoe 哈哈 我看大意了,应该是调用方负责释放。返回不应该autorelease :)
    xesique
        11
    xesique  
       2012-05-05 12:18:28 +08:00
    至于LZ说的leaks我怀疑是其他地方导致的泄漏,分配行为是在copyWithZone:中的,因而Instruments会报告这里泄漏。
    Smartype
        12
    Smartype  
       2012-05-05 12:20:06 +08:00   ❤️ 1
    @adow 这里是对的。机器只能告诉你泄漏的资源是哪里分配的。慢慢找吧。我估计你retain了他
    adow
        13
    adow  
    OP
       2012-05-05 13:41:09 +08:00
    恩的确是在外面泄露了,赋值给别的地方的一个property 的时候,retain 了一下,而那个property 本身应该是retain的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2220 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 05:40 · PVG 13:40 · LAX 22:40 · JFK 01:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.