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
georgetso
V2EX  ›  iDev

ARC 下出现 msg sent to deallocated instance?

  •  
  •   georgetso · 2015 年 5 月 5 日 · 3233 次点击
    这是一个创建于 3906 天前的主题,其中的信息可能已经有所发展或是发生改变。
    http://pan.baidu.com/s/1c0jMBny

    打开 zombie check 可以看到崩溃原因是
    2015-05-05 10:41:37.672 test[1710:65851] *** -[GTCollectionViewTableManager respondsToSelector:]: message sent to deallocated instance 0x7c142e10

    项目开启了 ARC,为什么会出现 sent to deallocated instance,如何解决这个问题(请不要使用 cv.datasource = self)?
    13 条回复    2015-05-05 16:02:07 +08:00
    blank_dlh
        1
    blank_dlh  
       2015 年 5 月 5 日
    出现 sent to deallocated instance 和 ARC 没什么关系。
    是不是有 assign 的弱引用没有手动置空?
    railgun
        2
    railgun  
       2015 年 5 月 5 日
    用 Instruments 开 zombie 查一下。
    多半是1楼说的原因,delegate 销毁掉了,但是CollectionView还没有释放
    georgetso
        3
    georgetso  
    OP
       2015 年 5 月 5 日
    @blank_dlh 只有 collectionView.dataSource = manager; collectionView.delegate = manager; 两句。
    dataSource 和 delegate 都是 assign,所以才会出现崩溃。
    问题在于,应该怎么解决这个问题?非得让 dataSource 和 delegate 指向 viewController 吗?
    bzmario
        4
    bzmario  
       2015 年 5 月 5 日
    弱引用使用 weak
    georgetso
        5
    georgetso  
    OP
       2015 年 5 月 5 日
    @bzmario 这个怎么使用weak?
    datasource和delegate都是SDK定义的属性
    blank_dlh
        6
    blank_dlh  
       2015 年 5 月 5 日
    @georgetso GTCollectionViewTableManager dealloc 的时候,把 dataSource 合 delegate 设为 nil 。
    georgetso
        7
    georgetso  
    OP
       2015 年 5 月 5 日
    @blank_dlh 不要在意这些细节。我的主要问题是,为什么 manager 被释放了
    black
        8
    black  
       2015 年 5 月 5 日   ❤️ 1
    你的manager是局部变量,没有对象持有它,当然被释放了。
    black
        9
    black  
       2015 年 5 月 5 日
    把manager声明成ViewController的property,就可以了。
    @property (nonatomic, strong) GTCollectionViewTableManager *manager;
    georgetso
        10
    georgetso  
    OP
       2015 年 5 月 5 日
    @black 嗯,这个解释靠谱。赞你一个
    blank_dlh
        11
    blank_dlh  
       2015 年 5 月 5 日
    @georgetso 你的 GTCollectionViewTableManager 是个局部变量,出了 viewDidLoad 就没了。。
    georgetso
        12
    georgetso  
    OP
       2015 年 5 月 5 日
    @blank_dlh 捂脸奔过,请不要继续戳我的伤口。。。
    blank_dlh
        13
    blank_dlh  
       2015 年 5 月 5 日
    @georgetso 哈哈 之前没看到有贴了代码地址
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   3340 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 110ms · UTC 12:27 · PVG 20:27 · LAX 04:27 · JFK 07:27
    ♥ Do have faith in what you're doing.