ios - iPhone NSMutableArray and NSKeyedUnarchiver unarchiveObjectWithFile release oddity -
i archive array (nsmutablearray) of custom objects implement . once load froma file retaining property @property (nonatomic, retain) nsmutablearray *buddies; release count of object 2 (correct, it's 1of autorelease + 1 of retain of property) noone releases , retain count becames 1, when release -[__nsarraym retaincount]: message sent deallocated instance (i think because 1 retain count autorelease)
here's full code:
buddielistviewcontroller.h
#import <uikit/uikit.h> #import "buddie.h" @interface buddielistviewcontroller : uitableviewcontroller { iboutlet nsmutablearray *buddies; [...] } [...] @property (nonatomic, retain) nsmutablearray *buddies; [...] @end
buddielistviewcontroller.m
#import "buddielistviewcontroller.h" #import "buddie.h" #import "previewviewcontroller.h" @implementation buddielistviewcontroller @synthesize buddies; [...] - (id)initwithstyle:(uitableviewstyle)style { self = [super initwithstyle:style]; if (self) { [self loadfromdisk]; } return self; } - (void)loadfromdisk { nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentspath = [paths objectatindex:0]; nsstring *appfile = [documentspath stringbyappendingpathcomponent:@"buddiearchive.ark"]; nsfilemanager *filemanager = [nsfilemanager defaultmanager]; if ([filemanager fileexistsatpath:appfile]) { self.buddies = [nskeyedunarchiver unarchiveobjectwithfile:appfile]; nslog(@"1- buddies retain count %d (should 2, 1 + 1autorelease)", [buddies retaincount]); } else { self.buddies = [nsmutablearray arraywithcapacity:1]; } } [...] - (ibaction)cancelledbuddie:(nsnotification *)notification { [editingbuddie release]; nslog(@"2- buddies retain count %d (should 2, 1 + 1autorelease)", [buddies retaincount]); [buddies release]; [self loadfromdisk]; [self.tableview reloaddata]; }
has idea of why happens?
if need nullify array, use property accessor set nil
:
self.buddies = nil;
the synthesized implementation takes care of memory management issues. try avoid sending -retain/-release
messages directly instance variables wherever possible , instead allow property accessors take care of things you. it'll save lot of trouble.
Comments
Post a Comment