iphone - Memorymanagement when getting a NSMutableArray from NSObject class to UIViewController class -


i have problem following code leaking memory...

@property (nonatomic, retain) nsmutablearray *childrensarray;   -(void)connectiondidfinishloading:(nsurlconnection *)connection {  nslog(@"connection finished loading.");   // dismiss network indicator when connection finished loading [uiapplication sharedapplication].networkactivityindicatorvisible = no;  // parse responsedata of json objects retrieved service sbjson *parser = [[sbjson alloc] init];  nsstring *jsonstring = [[nsstring alloc] initwithdata:responsedata encoding:nsutf8stringencoding]; nsdictionary *jsondata = [parser objectwithstring:jsonstring error:nil]; childrensarray = [jsondata objectforkey:@"children"];  // callback attendancereportviewcontroller responsedata finished loading [attendancereportviewcontroller loadchildren];  [connection release]; [responsedata release]; [jsonstring release]; [parser release];  }   

in viewcontroller following leaks memory...

@property (nonatomic, retain) nsmutablearray *childrensarray;   - (void)loadchildren {  // retrieve array dictionaries of children servicegetchildren self.childrensarray = [[servicegetchildren.childrensarray copy] autorelease];     int total = [childrensarray count]; totallabel.text = [nsstring stringwithformat:@"%d", total];   [thetableview reloaddata]; }    

you release childrensarray when instance deallocated. should release instance variable before setting it:

- (void)loadchildren {     // retrieve array dictionaries of children servicegetchildren      [childrensarray release];     childrensarray = [servicegetchildren.childrensarray copy];  } 

a better way use property:

- (void)loadchildren {     // retrieve array dictionaries of children servicegetchildren      self.childrensarray = [[servicegetchildren.childrensarray copy] autorelease];  } 

(note autorelease)

this has benefit of triggering kvo-notifications should ever use them.


Comments

Popular posts from this blog

Cursor error with postgresql, pgpool and php -

delphi - ESC/P programming! -

c++ - error: use of deleted function -