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-05-05 10:50:20 +08:00 · 2552 次点击
    这是一个创建于 3250 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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-05-05 10:59:55 +08:00
    出现 sent to deallocated instance 和 ARC 没什么关系。
    是不是有 assign 的弱引用没有手动置空?
    railgun
        2
    railgun  
       2015-05-05 11:29:25 +08:00
    用 Instruments 开 zombie 查一下。
    多半是1楼说的原因,delegate 销毁掉了,但是CollectionView还没有释放
    georgetso
        3
    georgetso  
    OP
       2015-05-05 11:34:23 +08:00
    @blank_dlh 只有 collectionView.dataSource = manager; collectionView.delegate = manager; 两句。
    dataSource 和 delegate 都是 assign,所以才会出现崩溃。
    问题在于,应该怎么解决这个问题?非得让 dataSource 和 delegate 指向 viewController 吗?
    bzmario
        4
    bzmario  
       2015-05-05 14:58:45 +08:00
    弱引用使用 weak
    georgetso
        5
    georgetso  
    OP
       2015-05-05 15:26:59 +08:00
    @bzmario 这个怎么使用weak?
    datasource和delegate都是SDK定义的属性
    blank_dlh
        6
    blank_dlh  
       2015-05-05 15:28:47 +08:00
    @georgetso GTCollectionViewTableManager dealloc 的时候,把 dataSource 合 delegate 设为 nil 。
    georgetso
        7
    georgetso  
    OP
       2015-05-05 15:30:00 +08:00
    @blank_dlh 不要在意这些细节。我的主要问题是,为什么 manager 被释放了
    black
        8
    black  
       2015-05-05 15:42:35 +08:00   ❤️ 1
    你的manager是局部变量,没有对象持有它,当然被释放了。
    black
        9
    black  
       2015-05-05 15:43:36 +08:00
    把manager声明成ViewController的property,就可以了。
    @property (nonatomic, strong) GTCollectionViewTableManager *manager;
    georgetso
        10
    georgetso  
    OP
       2015-05-05 15:44:12 +08:00
    @black 嗯,这个解释靠谱。赞你一个
    blank_dlh
        11
    blank_dlh  
       2015-05-05 15:46:28 +08:00
    @georgetso 你的 GTCollectionViewTableManager 是个局部变量,出了 viewDidLoad 就没了。。
    georgetso
        12
    georgetso  
    OP
       2015-05-05 15:47:12 +08:00
    @blank_dlh 捂脸奔过,请不要继续戳我的伤口。。。
    blank_dlh
        13
    blank_dlh  
       2015-05-05 16:02:07 +08:00
    @georgetso 哈哈 之前没看到有贴了代码地址
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2014 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 16:18 · PVG 00:18 · LAX 09:18 · JFK 12:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.