关于 obj-c 中的单实例模式,静态分析器总给我报一个奇怪的提示 - V2EX
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

关于 obj-c 中的单实例模式,静态分析器总给我报一个奇怪的提示

  •  
  •   adow
    adow 2014-04-22 16:24:33 +08:00 4918 次点击
    这是一个创建于 4259 天前的主题,其中的信息可能已经有所发展或是发生改变。
    最近我在用Xcode 静态分析器时发现我所有单实例代码中的

    +(id)allocWithZone:(NSZone *)zone

    都有警告:

    Object with a +0 retain count returned to caller where a +1 (owning) retain count is expected

    意思是allocWithZone返回的object必须是retain+1的,而单实例中的-(release)是直接返回self的,所以没有retain。

    我记得以前版本的Xcode中是没有这个提示的,我不知道是不是有别的实现方法了吗?

    我现在的写法类似下面这样:

    @implementation MySingleton

    static MySingleton *sSharedSingleton = nil;

    + (MySingleton *)sharedSingleton
    {
    if (sSharedSingleton == nil)
    {
    sSharedSingleton = [[super allocWithZone:NULL] init];
    }
    return sSharedSingleton;
    }

    + (id)allocWithZone:(NSZone *)zone
    {
    return [[self sharedSingleton] retain];
    }

    - (id)copyWithZone:(NSZone *)zone
    {
    return self;
    }

    - (id)retain
    {
    return self;
    }

    - (unsigned)retainCount
    {
    return NSUIntegerMax; //denotes an object that cannot be released
    }

    - (oneway void)release
    {
    //do nothing
    }

    - (id)autorelease
    {
    return self;
    }

    @end
    17 条回复    1970-01-01 08:00:00 +08:00
    chisj
        1
    chisj  
       2014-04-22 17:48:18 +08:00   1
    我们的单例模式是这么写的,是我最喜欢的写法。给LZ一个参考:

    static id shareInstance = nil;

    +(void)initialize {
    if (self == [EUHttpCache class]) {
    shareInstance = [[self alloc] init];
    }
    }

    +(EUHttpCache *)shareInstance {
    return shareInstance;
    }
    fly2never
        2
    fly2never  
       2014-04-22 17:56:58 +08:00   2
    用 dispatch_once吧,标准写法
    DropperY
        3
    DropperY  
       2014-04-22 18:00:11 +08:00   1
    是因为retaincount 写死了 NSUIntegerMax 的原因吧,预期的retaincount应该比原来大1
    alexrezit
        4
    alexrezit  
       2014-04-22 18:00:59 +08:00 via iPhone   1
    是什鬼法...
    chchwy
        5
    chchwy  
       2014-04-22 19:04:47 +08:00   1
    主的法已了。
    ultragtx
        6
    ultragtx  
       2014-04-22 19:28:53 +08:00 via iPad   1
    不能吐槽更多 这都是在哪学的奇葩写法
    satanwoo
        7
    satanwoo  
       2014-04-22 20:08:14 +08:00   1
    @ultragtx objetive-c设计模式里面好像就是这么写的。。。。
    adow
        8
    adow  
    OP
       2014-04-22 20:17:27 +08:00 via iPhone
    这样的写法我是从一本obj-c设计模式书上看来的,今天我google了一圈,现在的确都是用 dispatch_once 的写法了,我也改成了这样。不过这个问题在Xcode5.2下依旧。我想应该不是初始化时的问题吧。
    ylovesy
        9
    ylovesy  
       2014-04-22 20:28:39 +08:00   1
    上面的单例不好,应该这样:

    +(instancetype)sharedInstance
    {
    static dispatch_once_t once;
    static MyObject *sharedInstance = nil;

    dispatch_once(&once,^{
    sharedInstance = [[MyObject alloc] init];
    });

    return sharedInstance;
    }
    ultragtx
        10
    ultragtx  
       2014-04-22 20:39:08 +08:00
    这不只是单例的问题 你这里面除了sharedSingleton以外的所有method都不需要
    jesse0628
        11
    jesse0628  
       2014-04-22 20:48:18 +08:00   1
    @ultragtx 他想达到的目的是,把单例当做正常非单例使用,但不管你alloc还是retain、release,永远只存在一份真正的内存对象。
    satanwoo
        12
    satanwoo  
       2014-04-22 20:54:21 +08:00   1
    楼上正解。而且这样的写法还要考虑到单例中的继承问题。。
    ultragtx
        13
    ultragtx  
       2014-04-22 21:27:26 +08:00
    @jesse0628 按我理解这么写的动机是为了弥补语法层面不支持而加上的保护措施吧

    但这么做也就违反了内存管理的规则 而且在ARC下也没法这么写
    hengxin196
        14
    hengxin196  
       2014-04-22 23:13:24 +08:00   1
    `#import "__class__.h"

    @implementation __class__

    + (__class__ *)__accessor__
    {
    static dispatch_once_t once;
    static __class__ *__singleton__;
    dispatch_once(&once, ^ { __singleton__ = [[__class__ alloc] init]; });
    return __singleton__;
    }

    @cursor

    @end`
    wangccddaa
        15
    wangccddaa  
       2014-04-23 15:38:49 +08:00   1
    + (id)sharedList
    {
    static id _sharedInstance = nil;
    static dispatch_once_t once;

    dispatch_once(&once, ^{
    _sharedInstance = [[self alloc] init];
    });
    return _sharedInstance;
    }

    上一份我们的代码 把类名改成 self 是不是就是万能单例了,哈哈~
    krafttuc
        16
    krafttuc  
       2014-04-23 16:45:10 +08:00   1
    https://github.com/mattt/Xcode-Snippets/blob/master/singleton.m

    目前大家都趋向用 `dispatch_onc` 了
    msching
        17
    msching  
       2014-04-24 09:47:22 +08:00
    lz这个写法多线程环境下会生成多个实例。。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2813 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 31ms UTC 02:58 PVG 10:58 LAX 18:58 JFK 21:58
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86