iPhone - SBJsonParser 的另一个 Objective-C 内存泄漏
我对 iPhone 开发和 Stack Overflow 问题非常陌生。自一月份以来我一直在开发我的第一个应用程序。
我的应用程序存在与 SBJsonParser 相关的内存泄漏。经过一番谷歌搜索后,我在 stackoverflow 上找到了另一篇帖子。感谢 Konrad77 在他的答案上发布的功能,我更改了应用程序的一些行。但我仍然遇到内存泄漏。我希望得到一些帮助。我正在使用 AsiHttpRequest 1.8 和 JSONframework 3.0beta1。
仪器告诉我,99.2% 的泄漏位于 MyLists.m 的以下行:
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
这里有 Instruments 的屏幕截图:
提前致谢!
I'm really new to iPhone development and Stack Overflow questions. I've been doing my first app since January.
My app has a memory leak related to SBJsonParser. After some googling I found another post here on stackoverflow. Thanks to the function that Konrad77 posted on his answer, 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.
Instruments tell me that the leak is on the following line of MyLists.m for 99.2%:
resultObject = [self.model JSONObjectForRequest:request];
The other 0.8% goes to the following line of MyLists.m:
[self.model.myLists addObject:userData];
Both of previous lines are inside listaGetRequestOnResult function. Here you have all 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 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
Here you have screenshots of Instruments:
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的泄漏(它实际上有两个额外的保留):
你可能想要这样的东西:
我也不会像你所做的那样使用
assign
属性。Your leak (it actually has two extra retains):
You probably want something like this:
I also wouldn't use
assign
properties like you have done.很棒的职位发布代码,以及使用 Instruments 的高五。我一直对有多少开发人员不使用它感到惊讶。
我知道您可能已经读过本文,但请重新阅读;
http ://developer.apple.com/library/ios/#referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/_index.html%23//apple_ref/doc/uid/TP40007594
代码如下;
说明你还没有掌握封装的概念。
模型负责该列表而不是外部对象,这一点非常重要。在模型类中添加代码以在
init
方法中创建该列表,然后添加可以调用的方法,以将项目添加到该列表。调用
alloc
意味着您在此范围内有一个retain,然后再次调用retain
意味着您有两个。我确信还有其他类似的问题。为了帮助理解内存管理规则,请阅读此内容;
http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html%23//apple_ref/doc/uid/20000994-BAJHFBGH
TC的建议上面是黄金,不要使用
asign
除非你明白为什么要使用它。http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html%23//apple_ref/doc/uid/TP30001163-CH17-SW1
有关各种选项含义的更多详细信息。
Great job posting code, and big high five for using Instruments. I am constantly amazed how many developers don't use it.
I know you probably have already read this, but please re-read it;
http://developer.apple.com/library/ios/#referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/_index.html%23//apple_ref/doc/uid/TP40007594
Code like this;
shows that you have not grasped the concept of encapsulation.
It is really important that
model
be in charge of that list rather than an external object. Add code in your model class to create that list in theinit
method then add methods that you can call that will add items to that list.Calling
alloc
means that you have one retain in this scope, then callingretain
again means you have two. I'm sure there are other similar problems.To help understand the memory management rules please read this;
http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html%23//apple_ref/doc/uid/20000994-BAJHFBGH
tc's advice above is golden, don't use
asign
unless you understand why you are using it.http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html%23//apple_ref/doc/uid/TP30001163-CH17-SW1
has tons more detail on what the various options mean.