cocoa 内存管理笔记

本文介绍了如何通过正确管理Objective-C对象的引用计数来避免EXC_BAD_ACCESS错误。包括使用new、alloc或copy创建的对象应适当释放,通过其他方式获取的对象视为已自动释放,并需在长时间持有对象时保留并最终释放。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

经常碰到 EXC_BAD_ACCESS 调试程序十分麻烦,只好回头看书

 

Here are the rules:
 ■ When you create an object using new, alloc, or copy, the object has a retain count
of 1. You are responsible for sending the object a release or autorelease message
when you’re done with it. That way, it gets cleaned up when its useful life is over.

 

 


 ■ When you get hold of an object via any other mechanism, assume it has a retain
count of 1 and that it has already been autoreleased. You don’t need to do any fur-
ther work to make sure it gets cleaned up. If you’re going to hang on to the object for
any length of time, retain it and make sure to release it when you’re done.


 ■ If you retain an object, you need to (eventually) release or autorelease it. Balance
these retains and releases.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值