UIButton release的问题 About UIButton release problem

一位新同事的代码,老是内存泄露,找不到问题,我就帮忙看了一下,最后找到问题是在dealloc 中将UIButton的对象release了。

一般情况,UIButton初始化一般使用静态方法+ (id)buttonWithType:(UIButtonType)buttonType; 而非alloc或者create方法。只有在使用alloc与create关键字的方法下创建对象才会使此对象的retain值+1,而release是使retain-1,所以在创建UIButton之后是不需要release的。

所以,通过静态方法初始化而非alloc的对象一般可以认为是autorelease的。


One new guy told me his codes always leaked, and he could not find the problems. The problem was he released a UIButton object in the dealloc method, after I checked his codes. Generally, we use the static method '+ (id)buttonWithType:(UIButtonType)buttonType;' to initialise a UIButton, not alloc or create. The alloc method makes retainCount +1, while 'release' makes it -1. Therefore, the new guy's UIButton is no need to release.

About all, we can treat the object, created by static methods not the alloc, as autoreleased one.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值