Note that there are some explanatory texts on larger screens.

plurals
  1. POiPhone - Another Objective-C Memory leak with SBJsonParser
    text
    copied!<p>I'm really new to iPhone development and Stack Overflow questions. I've been doing my first app since January.</p> <p>My app has a memory leak related to SBJsonParser. After some googling I found another <a href="https://stackoverflow.com/questions/4983534/iphone-objective-c-memory-leak-with-sbjsonparser">post</a> here on stackoverflow. Thanks to the function that Konrad77 posted on his <a href="https://stackoverflow.com/questions/4983534/iphone-objective-c-memory-leak-with-sbjsonparser/4983610#4983610">answer</a>, I changed some lines of my app. But I'm still getting memory leaks. I would appreciate some help. I'm using AsiHttpRequest 1.8 and JSONframework 3.0beta1.</p> <p>Instruments tell me that the leak is on the following line of MyLists.m for 99.2%:</p> <pre><code>resultObject = [self.model JSONObjectForRequest:request]; </code></pre> <p>The other 0.8% goes to the following line of MyLists.m:</p> <pre><code>[self.model.myLists addObject:userData]; </code></pre> <p>Both of previous lines are inside listaGetRequestOnResult function. Here you have all related code:</p> <p><strong>-MyLists.h:</strong></p> <pre><code> #import &lt;UIKit/UIKit.h&gt; #import "Model.h" #import "ASIFormDataRequest.h" @interface MyLists : UITableViewController { Model *model; NSObject *resultObject; } @property (assign) Model *model; @property (nonatomic,assign) NSObject *resultObject; @end </code></pre> <p><strong>-MyLists.m:</strong></p> <pre><code>#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 successfully         NSMutableArray *userData = [data valueForKey:@"data"];         //Set list into model For now, only one component for the 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 but returned and 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 </code></pre> <p><strong>-Model.h:</strong></p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #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 </code></pre> <p><strong>-Model.m:</strong></p> <pre><code>#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 </code></pre> <p>Here you have screenshots of Instruments:</p> <p><img src="https://i.stack.imgur.com/KSx7c.png" alt="Instruments leaks 1"></p> <p><img src="https://i.stack.imgur.com/dZ46F.png" alt="Instruments leaks 2"></p> <p>Thanks in advance!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload