Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does ARC release malloc'ed memory for you?

Automatic Reference Counting releases Objective-C objects that have been allocated. What about primitive variables like char *?

like image 361
hollow7 Avatar asked Nov 27 '11 09:11

hollow7


2 Answers

No as per the llvm document on ARC

Automatic Reference Counting implements automatic memory management for Objective-C objects and blocks, freeing the programmer from the need explicitly insert retains and releases. It does not provide a cycle collector; users must explicitly manage lifetime instead.

and

A retainable object pointer (or retainable pointer) is a value of a retainable object pointer type (retainable type). There are three kinds of retainable object pointer types:

  • block pointers (formed by applying the caret (^) declarator sigil to a function type)
  • Objective-C object pointers (id, Class, NSFoo*, etc.)
  • typedefs marked with __attribute__((NSObject))

Other pointer types, such as int* and CFStringRef, are not subject to ARC's semantics and restrictions.

like image 190
mmmmmm Avatar answered Oct 11 '22 07:10

mmmmmm


ARC directly release only ObjC objects (not char*, void*, int* ...). However ARC still calls the dealloc method of objects. So if you have an ObjC object that malloc's up some memory and points a char* at it, and the dealloc method calls free that memory will still get properly disposed of.

Same for Core Foundation objects, you still need to CFRelease them. Even the toll free bridged CF types need manual calls to CFRelease.

like image 27
Stripes Avatar answered Oct 11 '22 08:10

Stripes