liuzhiyi1992 / ZYCornerRadius

一句代码,圆角风雨无阻。A Category to make cornerRadius for UIImageView have no Offscreen-Rendered, be more efficiency.
http://zyden.vicp.cc/zycornerradius/
1.45k stars 230 forks source link

在设置圆角的界面返回上一层,偶尔会crash #19

Closed ljhang closed 7 years ago

ljhang commented 8 years ago

crash reason : An instance 0x7fc32379a330 of class UIImageView was deallocated while key value observers were still registered with it. Current observation info: <NSKeyValueObservationInfo 0x7fc3235c6330> ( <NSKeyValueObservance 0x7fc3235b0910: Observer: 0x7fc32379a330, Key path: image, Options: <New: YES, Old: NO, Prior: NO> Context: 0x0, Property: 0x7fc323794190> ) 有时会crash掉,貌似是没有是没有释放掉...我的项目环境是Xcode7.3.1,iOS8.0

liuzhiyi1992 commented 8 years ago

@ljhang 旧版本有这个问题,请问目前使用的是什么版本

heyode commented 8 years ago

我使用的是0.9.4,返回控制器的时候还是crash,打断点发现:每次崩溃的时候不执行你的交换方法- (void)zy_dealloc,直接崩,*\ Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'An instance 0x78724f80 of class UIImageView was deallocated while key value observers were still registered with it. Current observation info: <NSKeyValueObservationInfo 0x78772640> ( <NSKeyValueObservance 0x78702f60: Observer: 0x78724f80, Key path: image, Options: <New: YES, Old: NO, Prior: NO> Context: 0x0, Property: 0x791be040> )'

ljhang commented 8 years ago

@liuzhiyi1992 之前是0.9.1,现在好像是0.9.4....

heyode commented 8 years ago

@liuzhiyi1992 我小白,想问下为啥不直接在dealloc方法中写这句呢?交换方法到底有啥好处?如果直接在dealloc里面调这句就不会出现我上面说的问题。 if (self.hadAddObserver) { [self removeObserver:self forKeyPath:@"image"]; }

liuzhiyi1992 commented 8 years ago

@ljhang 0.9.4还存在这个问题,1.0.1修复了哦

liuzhiyi1992 commented 8 years ago

@heyode 因为category的侵略性太强,直接在dealloc中写会导致整个项目所有UIImageView的dealloc都被改写,加多一个zy_dealloc可控性比较好,在需要的时候再加入特定代码

liuzhiyi1992 commented 8 years ago

@heyode 1.01修复了这个crash

ljhang commented 8 years ago

@liuzhiyi1992 赞👍

heyode commented 8 years ago

@liuzhiyi1992 恩恩,用1.0.1试了,可以了,多谢指导

liuzhiyi1992 commented 8 years ago

@ljhang 谢谢荣哥

liuzhiyi1992 commented 8 years ago

@heyode 谢谢支持

heyode commented 8 years ago

@liuzhiyi1992 “因为category的侵略性太强,直接在dealloc中写会导致整个项目所有UIImageView的dealloc都被改写,加多一个zy_dealloc可控性比较好,在需要的时候再加入特定代码” 哈哈,我有回想了下,对这句还是有些疑问:加上个zy_dealloc,实际上也就是系统原本调用delloc的时候会调一个zy_dealloc,在zy_dealloc里面写东西,还是相当于整个项目的UIImageView的dealloc都被改写了啊,不知道理解对不对。

liuzhiyi1992 commented 8 years ago

@heyode 是的没错,一旦swizzle成功,项目里的UIImageView class对象方法列表里的dealloc方法就被改写了。 说回swizzle的作用: 1.逻辑分明,hook的逻辑在-zy_dealloc里,编码者写的在-dealloc里 2.懒加载思想,当用到这个库时,才出发swizzle 3.不覆盖编码者重写的dealloc.

1和2都是编码技巧或者说规范,3是必须要的

heyode commented 8 years ago

@liuzhiyi1992 赞,这回真明白了!感谢感谢!

meilbn commented 8 years ago

@liuzhiyi1992 1.0.1 还是会 crash,添加4个圆角,方式类似 demo 里面的第三种图片创建方式。imageView 在 UICollectionView 的 cell 里面,界面返回之后就 crash 了。。。

liuzhiyi1992 commented 8 years ago

@meilbn crash的原因是什么?log是?

meilbn commented 8 years ago

@liuzhiyi1992 跟上面一样,也是 KVO 的原因。imageView dealloc 了,但是没有移除 keypath 为 image 的 KVO

coryil commented 8 years ago

@liuzhiyi1992 1.0.1 在UITableViewCell上添加了UIImageView 返回上一页面crash An instance 0x14f77fe00 of class UIImageView was deallocated while key value observers were still registered with it. Current observation info: ( Context: 0x0, Property: 0x14f7800c0>

liuzhiyi1992 commented 8 years ago

@coryil @meilbn 我重现不出来,可以帮我在这个demo上改改看能不能重现吗? TestForCornerRadius.zip

meilbn commented 8 years ago

@liuzhiyi1992 很奇怪,我自己的项目里面没走 zy_dealloc 就 crash 了,在你的项目的基础中把我原来项目的代码复制过来,基本上是一样的,但是没有 crash。。。

cnxbox360 commented 8 years ago

@liuzhiyi1992 AddObserver也要有线程保护啊,不然多线程会导致多次添加Observer,但是dealloc只remove了一次,最后肯定会崩!

liuzhiyi1992 commented 8 years ago

@cnxbox360 朋友提醒的是。多线程KVO一直忽略了,我们应该尽量避免在后台线程触发和响应kvo @meilbn @coryil 检查下是否在非主线程上多次初始化UIImageView?

italianshen commented 8 years ago

怎么比使用cornersRadius内存还要高 使用这个分类 [_iconView zy_cornerRadiusAdvance:20.f rectCornerType:UIRectCornerAllCorners];设置滚动的时候内存是106M 然而使用cornerRadius内存是50多? 怎么回事

liuzhiyi1992 commented 8 years ago

@italianshen 看看cell是否没有正常重用