我對iPhone開發和堆棧溢出問題確實很陌生。自1月份以來,我一直在做我的第一個應用。iPhone - 使用SBJsonParser的另一個Objective-C內存泄漏
我的應用程序有一個與SBJsonParser相關的內存泄漏。一些谷歌搜索後,我發現另一個post這裏在stackoverflow。由於Konrad77發佈在他的answer上的功能,我改變了我的應用程序的一些行。但我仍然有內存泄漏。我希望得到一些幫助。我使用的是AsiHttpRequest 1.8和JSONframework 3.0beta1。
儀器告訴我,泄漏是以下行MyLists.m爲99.2%:
resultObject = [self.model JSONObjectForRequest:request];
其他0.8%到以下行MyLists.m的:
[self.model.myLists addObject:userData];
以上兩行都在listaGetRequestOnResult函數內。在這裏,你都相關的代碼:
-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 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
-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
這裏有工具的截圖:
提前感謝!
這也許是在一個單獨的線程? – 2011-04-11 18:19:54
你可以嘗試[[SBJsonParser alloc] init]而不是[SBJsonParser new]? – 2011-04-11 18:21:36
** @理查德J.羅斯三世:**我沒有在我的應用程序上編程線程我不知道這是否有幫助但我第一次調用'model = [Model getModel];'函數'didFinishLaunchingWithOptions'我的AppDelegate ** @ Zaky德語:**我試過'[[SBJsonParser alloc] init]',我仍然得到相同的內存泄漏我也嘗試將jsonParser設置爲模型中的保留屬性然後我如果:if(!jsonParser){ \t \t jsonParser = [[SBJsonParser alloc] init]; '但我不工作 – Roger 2011-04-11 19:06:04