views:

119

answers:

3

Hi guys,

I am very new to obective C and Ipad app development. Can someone explain the difference between "free", "release", "autorelease" and "dealloc"? Thank you so much in advance. I realise this might be a simple question for the pros but I genuinely don't know where to use what.

Thank you

+1  A: 

Out of the 4 methods you've mentioned you'll typically only use release and autorelease in objective-c. free maybe used when you've used c and malloc otherwise you should use release and autorelease.

dealloc shouldn't be called by any of your code as it can interfere with the retain/release cycle that objective-c uses to keep track of resources. Using dealloc will almost certainly result in crashes at runtime if you ever use it.

The ultimate resource of the use of the release and autorelease is the Apple memory management documentation

James Raybould
+2  A: 
  • free is the opposite of malloc and is used in C. You'll likely not use it very much programming in Objective C
  • If you own an object, you release it when you're done with it
  • You can also autorelease an object. This automatically releases it at the end of the current run loop iteration
  • When the reference count on an object drops to zero, the runtime calls dealloc. You should not call this method yourself

You "own" an object if you alloc, new, retain or copy it.

Apple provide some good documentation on this.

Stephen Darlington
Point 3: I'd call it "run-loop iteration" instead of "run-loop".Point 4: Certainly, you meant to say "dealloc" instead of "release", don't you? (sorry, nitpicking)
Dirk
Thanks very much. That was definitely helpful.
ar106
@Dirk Good spot. Fixed.
Stephen Darlington
I think you need to read the document you linked to more carefully. You do not own an object you `init`, you own an object you create with `new`, `alloc` or a method containing the word `copy`. You also own an object you retain.
JeremyP
Also, an autoreleased object may not last until the end of the runloop iteration. It will only last until the autorelease pool is drained. *Normally* that would be at the end of the runloop iteration, but you are allowed to create new autorelease pools too.
JeremyP
@Stephen you do **not** own an object if you init or retain it you own it if you new, alloc, retain, or copy it.
Dave DeLong
Actually I was aiming to keep it simple/clear, albeit with a slight loss in accuracy. Apparently that failed so I've corrected my answer.
Stephen Darlington
+4  A: 

free() is a C function that you use to dispose of no longer needed memory you obtained through malloc() or other function that is documented to require free() to deallocate its memory e.g. strdup().

-dealloc is an Objective-C selector that is sent by the Objective-C runtime to an object when the object is no longer owned by any part of the application.

-release is the selector you send to an object to indicate that you are relinquishing ownership of that object. Once an object is not owned by anybody, it is sent the -dealloc message by the runtime.

-autorelease is a selector you send to an object to indicate you are relinquishing ownership of the object. However if nobody now owns the object the final -dealloc will be deferred until some unspecified later point. In fact, what you are really doing is transferring your ownership to an autorelease pool which will then release it when it is itself released (or drained).

You must never send -dealloc to an object except to super in the object's own -dealloc method.

JeremyP
Would whoever just down voted this answer please explain why they did it?
JeremyP