ios - Add allocated buffer to NSMutableDictionay -
is possible add dynamically allocated buffer using malloc nsmutabledictionary , free after add directly. correct?
no, very, bad. call free, os can re-use memory, use of buffer bad.
if want put data in mutable dictionary, use nsdata instead.
if reason need store malloc'd buffer ( e.g. got library), you'll need wrap in object class.
you can call free wrapper object's dealloc, free won't called until wrapper object released ( handled arc once dictionary released.
@interface mallocedmemorywrapper @property ( nonatomic, assign) void* wrappedmemory; @end @implementation mallocedmemorywrapper - (mallocedmemorywrapper*) initwithmemory:(void*) memory { self.wrappedmemory = memory; } - (void) dealloc { free( self.memory); } @end }