Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CFSTR memory management

I am using CFSTR function for creating CFString from constant c string and i am calling this function very frequently in my Daemon.

From documentation:
A value returned by CFSTR has the following semantics:

  1. Values returned from CFSTR are not released by CFString—they are guaranteed to be valid until the program terminates.
  2. You can retain and release values returned from CFSTR in a balanced fashion, like any other CFString, but you are not required to do so.

Should i use retain and release ?

like image 720
Parag Bafna Avatar asked Feb 13 '12 16:02

Parag Bafna


1 Answers

As the documentation states, CFSTR() created strings remain valid until the program terminates. You can release them all day long, but they won't actually be deallocated. For that reason, there's no need to explicitly retain/release them. It's valid to retain/release them because otherwise, you couldn't pass them around through other code that retains/releases them (framework methods, etc). Treat them like you would NSString literals created using @"", that is to say, no need to retain or release them after creation, but if you're writing code that can take any CFString, you need to follow normal memory management rules, including using CFRetain() and CFRelease().

like image 75
Andrew Madsen Avatar answered Sep 27 '22 17:09

Andrew Madsen