iPhone - Another Objective-C Memory leak with SBJsonParser -
i'm new iphone development , stack overflow questions. i've been doing first app since january.
my app has memory leak related sbjsonparser. after googling found post here on stackoverflow. function konrad77 posted on answer, changed lines of app. i'm still getting memory leaks. appreciate help. i'm using asihttprequest 1.8 , jsonframework 3.0beta1.
instruments tell me leak on following line of mylists.m 99.2%:
resultobject = [self.model jsonobjectforrequest:request]; the other 0.8% goes following line of mylists.m:
[self.model.mylists addobject:userdata]; both of previous lines inside listagetrequestonresult function. here have related code:
-mylists.h:
#import <uikit/uikit.h> #import "model.h" #import "asiformdatarequest.h" @interface mylists : uitableviewcontroller { model *model; nsobject *resultobject; } @property (assign) model *model; @property (nonatomic,assign) nsobject *resultobject; @end -mylists.m:
#import "mylists.h" #import "asiformdatarequest.h" @implementation mylists @synthesize model; @synthesize resultobject; -(void)loadlistdata { [self showwaitpopup:cargando]; //remote listaget operation nsurl *url = [nsurl urlwithstring:self.model.operationsurl]; asiformdatarequest *postrequest = [asiformdatarequest requestwithurl:url]; [postrequest setpostvalue:@"listaget" forkey:@"action"]; [postrequest setpostvalue:@"json" forkey:@"format"]; [postrequest setdelegate:self]; [postrequest setdidfinishselector:@selector(listagetrequestonresult:)]; [postrequest setdidfailselector:@selector(listagetrequestonfault:)]; [postrequest startasynchronous]; } - (void)listagetrequestonresult:(asiformdatarequest *)request { [self hidewaitpopup:cargando]; resultobject = [self.model jsonobjectforrequest:request]; nsdictionary *data = (nsdictionary *)resultobject; nsnumber *errorcode = [data valueforkey:@"errorcode"]; if ([errorcode intvalue] == 0) { //remote operation did end nsmutablearray *userdata = [data valueforkey:@"data"]; //set list model now, 1 component table [self reinitializetablelist:false]; self.model.mylists = [[nsmutablearray alloc] init]; [self.model.mylists addobject:userdata]; [self.model.mylists retain]; } else { //remote operation did end succesfully returned , error [model reporterror:[data valueforkey:@"errortext"] withtitle:@"error"]; [self reinitializetablelist:false]; } [self.tableview reloaddata]; } - (void)listagetrequestonfault:(asiformdatarequest *)request { [self hidewaitpopup:cargando]; nserror *error = [request error]; [model reporterror:[error localizeddescription] withtitle:@"error de conexión"]; [self reinitializetablelist:true]; } -(void)reinitializetablelist:(bool)reloadtabledata { if (self.model.mylists) { [self.model.mylists release]; } self.model.mylists = nil; if (reloadtabledata) { [self.tableview reloaddata]; } } - (void)viewdidload { self.model = [model getmodel]; [super viewdidload]; } - (void)viewwillappear:(bool)animated { [self loadlistdata]; [super viewwillappear:animated]; } - (void)dealloc { model = nil; resultobject = nil; [super dealloc]; } @end -model.h:
#import <foundation/foundation.h> #import "asihttprequest.h" @interface model : nsobject { nsstring *operationsurl; nsstring *imagesbaseurl; nsmutablearray *mylists; } @property (retain) nsstring *operationsurl; @property (retain) nsstring *imagesbaseurl; @property (retain) nsmutablearray *mylists; + (model*) getmodel; //+ (id) allocwithzone:(nszone *) zone; + (void) initmodel; - (void)reporterror:(nsstring*)mensaje withtitle:(nsstring*)withtitle; - (nsobject*)jsonobjectforrequest:(asiformdatarequest *)request; @end -model.m:
#import "model.h" #import "asihttprequest.h" #import "json.h" @implementation model static model *uniqueinstance = nil; @synthesize operationsurl; @synthesize imagesbaseurl; @synthesize mylists; + (model*) getmodel { @synchronized(self) { if (uniqueinstance == nil) { uniqueinstance = [[model alloc] init]; [self initmodel]; } } return uniqueinstance; } /*+ (id) allocwithzone:(nszone *) zone { @synchronized(self) { if (uniqueinstance == nil) { uniqueinstance = [super allocwithzone:zone]; return uniqueinstance; } } return nil; }*/ + (void) initmodel { //url uniqueinstance.operationsurl=[nsstring stringwithformat:@"some_url"]; uniqueinstance.imagesbaseurl=[nsstring stringwithformat:@"some_url"]; } -(void)reporterror:(nsstring*)mensaje withtitle:(nsstring*)withtitle { uialertview *alertdialog; alertdialog = [[uialertview alloc] initwithtitle:withtitle message:[nsstring stringwithformat:@"%@",mensaje] delegate: nil cancelbuttontitle: @"aceptar" otherbuttontitles:nil]; [alertdialog show]; [alertdialog release]; } - (nsobject*)jsonobjectforrequest:(asiformdatarequest *)request { sbjsonparser *jsonparser = [sbjsonparser new]; nsobject *object=[jsonparser objectwithstring:[request responsestring] error:nil]; if (object == nil) { [self reporterror:[jsonparser error] withtitle:@"error librerÃa json"]; } [jsonparser release], jsonparser = nil; return object; } - (void)dealloc { [operationsurl release]; [imagesbaseurl release]; [mylists release]; [super dealloc]; } @end here have screenshots of instruments:


thanks in advance!
your leak (it has 2 retains):
self.model.mylists = [[nsmutablearray alloc] init]; [self.model.mylists addobject:userdata]; [self.model.mylists retain]; you want this:
self.model.mylists = [nsmutablearray arraywithobject:userdata]; i wouldn't use assign properties have done.
Comments
Post a Comment