When and why do i have to use release?
I have开发者_开发百科 learnes objective-c and xcode by watching tutorials and trying out, so just by myself. I never really had a real lesson with teachers or anything like that. And I never learnes what release
is used for. I have often seen it, of course but i want to know a few things about it. Do I have to use release for every object that i habe declared in the header file in - (void)dealloc
? If i create a temporary NSString in an IBAction do i have to release it at the end of the action? What happens if i dont use release? What does release really mean in programming language?
The rule is quite simple: you only need to release an object if you obtained it by using new
, alloc
, retain
or copy
(NARC for short). That's it. As soon as you're done with a NARC object and you don't need it anymore, you must release
it. If you obtained the object though any other means, then you must not release
it.
If you obtained an object by NARC somewhere in the class initialization and you need to keep this object during the lifetime of the class instance, then release
it in the dealloc
method. This will make sure the object lives as much as you need and dies after the instance of the class dies.
In Objective-C, each instance contains a reference count property. When you create an object with NARC, this reference count is 1. Each successive retain
will increase the reference count by 1. Each successive release
will decrease the reference count by 1. When the reference count reaches 0, the object is deallocated.
So, if you don't release NARC objects, then you will have a memory leak (because the reference count will remain above 0 and the object will never be deallocated, even if you're done with it). What this means is that your app will own a space in memory that it will never use. If these add up, then you'll be wasting lots of valuable system resources. On iOS this will likely get your app killed.
If you need to return an object created with NARC, then you should autorelease
it. This basically means "the object will have release
called on it just a little later". So if the method calling your method needs to hold this object you're returning, it can retain
it, and now the object has the reference count set to 2. release
will be called a little later, as scheduled, but the object was retain
ed, its reference count will go to 1, and it will not die. Now it's the caller's responsibility to call release
on it when it's done with it.
You would probably benefit from reading up on the basic concepts of memory management in Objective-C (allocation, reference counting, retain/release/autorelease...).
Apple provides a nice introduction here.
It is a topic that can easily confuse both new and experienced programmers, and you would do yourself a great favor by getting a solid understanding of it.
It's really not something that can be explained in 10 lines. But start reading the above document and you will be on your way to a better understanding of memory management in Cocoa.
精彩评论