Search code examples
objective-ccocoa-touchmemory-managementfree

Difference between free() and release


Possible Duplicate:
What are the differences between free, dealloc, release, and autorelease?

I want to get rid of my allocated memory used in my app. I know I need to use release but what is the difference between free() and release? Are they the same?


Solution

  • free() is part of the C standard library, so it's a function. It immediately frees the allocated memory obtained using malloc(), so it must be passed a pointer that is allocated by malloc(), else it invokes undefined behavior.

    - release is a method (as opposed to a function) of the NSObject class. It does not immediately free memory; it only decrements an object's reference count by one. It then also checks for it being 0 - if it is zero, it invokes - dealloc (which is usually overridden by a subclass to free memory allocated by the constructor method, - init or free() memory allocated by malloc()).

    So they are not the same at all, do not even attempt to use them interchangeably!