iphone - I have to retain a NSMutableArray although it's a property -


i'm new, read lot memory management, , tried find answer myself. sounds basic , yet apparently don't it. have nsmutablearray property in .h:

@property (nonatomic, retain) nsmutablearray *documentlistarray; 

i synthesize , release in (void) dealloc. populate array, have method - (void)updaterecordlist , in there:

self.documentlistarray=[documentdatabase getdocumentlistsortedbydate]; 

edit:next line:

[[self recordlisttableview] reloaddata]; 

where documentdatabase different class class methods getdocumentlistsortedbydate , getdocumentlist. here happening in getdocumentlistsortedbydate:

nsmutablearray *returnarray = [documentdatabase getdocumentlist]; //sorting... nslog("array count:%i",[returnarray count]); //returns correct numbers first , second time return returnarray; 

and in getdocumentlist

nsmutablearray *returnarray = [nsmutablearray arraywithcapacity:files.count]; //add file data array... return returnarray; 

this works first time call updaterecordlist, after adding file , calling updaterecordlist second time, crashes (using nszombies): * -[nscfnumber dealloc]: message sent deallocated instance 0x7504c90. lot of logging narrowed problem down line above in updaterecordlist , works if change to:

self.documentlistarray=[[documentdatabase getdocumentlistsortedbydate] retain]; 

my conclusion array down in getdocumentlist has been autoreleased before arrives. questions are:
1. why have retain there? shouldn't happen declaring property (retain)? or, in other words, why array autoreleased (assuming happening)?
2. when assign new array self.documentlistarray, old array automatically released? if try release myself before getting new documentlist, crashes too.

thanks in advance reply.

edit: maybe i'm idiot: failed mention documentlistarray data source uitableview (see added line on top). suspect doing wrong populating table view, , array gets retained...? crash on assigning property, not on reloaddata. go study if use uitableviewdatasource protocol properly. everybody, answers brought me on right track. update when solved.

edit2: works without retaining, , think understand why: debugged extensively , found objects contained in objects added array nil. particularly, deep down in encodewithcoder did not use "self" when assigning values. when decoding, values nil. since changed that, seems work. suspect not assigning new array caused crash, tableview read new array-even before call reloaddata. lead davids question of synchroneous access. thank help.

  1. the code you've shown appears correct; should not necessary (or correct) call retain yourself, long assigning value property retain semantics before autorelease pool drained. calls (getdocumentlistsortedbydate, getdocumentlist) happening synchronously, or doing of in background? double-check you're assigning using "self." ("self.documentlistarray =") instead of assigning directly instance var ("documentlistarray ="); if omit "self.", setter bypassed.

  2. no, don't free old value before assigning; that's setter's job.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -