开发者

ASIHTTPRequest problems on iPhone (memory?)

开发者 https://www.devze.com 2023-02-06 04:59 出处:网络
I am using ASIHTTPRequest on the iPhone to make a call to the server, which then sends a response containing a JSON file.I got it working on a basic level, but when I added it to my project, and made

I am using ASIHTTPRequest on the iPhone to make a call to the server, which then sends a response containing a JSON file. I got it working on a basic level, but when I added it to my project, and made a call to the server, my app started crashing with bad_access.

I make the call from viewDidLoad as such:

requestModel = [[RequestModel alloc]init];
NSURL *url = [[NSURL URLWithString:@"myURL"]retain];
[requestModel eventsRequestFor:@"myUsername" password:@"myPassword" forURL:url];

This calls a function in another class called RequestModel, and I go through and try to unpack the returned array as such (a lot of code):

-(void)eventsRequestFor:(NSString*)username password:(NSString*)password forURL:(NSURL*)forURL  {
//use ASIHTTPRequest to post to sever.  Here you authenticate and recieve answer
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:forURL];
[request setUsername:username]; 
[request setPassword:password]; 
[request setDelegate:self];

[request setDidFailSelector:@selector(uploadFailed:)];
[request setDidFinishSelector:@selector(uploadFinished:)];
[request startAsynchronous];

[request release];
NSLog(@"Model called");
}

#pragma mark Model Delegates
- (void)uploadFinished:(ASIHTTPRequest *)request  {
// Use when fetching text data

NSLog(@"upload start");
NSString *stringFromRequest = [[NSString alloc]init];
stringFromRequest = [request responseString];

[self buildArrayFromRequest:stringFromRequest];

//[stringFromRequest release];
}

- (void)uploadFailed:(ASIHTTPRequest *)request  {
NSString *statusMessage = [request responseStatusMessage];
NSLog(@"%@",statusMessage);
NSError *error = [request error];
NSLog(@"%@",error);

}   

-(void)buildArrayFromRequest:(NSString*)string  {

NSArray *arrayFromData = [[NSArray alloc]init];
arrayFromData = [string yajl_JSON];
NSLog(@"This is the array from the JSON  %@",[arrayFromData objectAtIndex:0]);
NSMutableArray *events = [[NSMutableArray alloc] init]; 

for开发者_如何转开发(int i= 0; i < [arrayFromData count];i++)
{
    /////code to unpack array

}

       ///here i try to populate table with unpacked array
FirstViewController *firstVC = [[FirstViewController alloc]init];
[firstVC createTableWithArray:events];

[firstVC release];
[arrayFromData release];
[events release];

}

Any ideas on where I am going wrong? It seems like I am releasing an object that has already been released, but I can't find it.


EDIT: You're releasing an NSData that's not yours.

You don't need this line :

[arrayFromData release];

because of this mistake :

You have put :

NSArray *arrayFromData = [[NSArray alloc]init];
arrayFromData = [string yajl_JSON];

when all you need is this :

NSArray *arrayFromData = [string yajl_JSON]; // This doesn't need releasing

You're releasing a request that's already autoreleased :(

// Create an autoreleased request object
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:forURL];

...

[request release]; // fail here - this line is not needed

Just FYI :

This is unneeded code :

NSString *stringFromRequest = [[NSString alloc]init];
stringFromRequest = [request responseString];

You are making a string object and then getting a different one. The first one you're making will be leaked. This code will do what you want :

NSString *stringFromRequest = [request responseString]; // This doesn't need releasing :)

However, it's the cause of a leak, not the crash that you are reporting.

(You've got this pattern in other places in your code, not just here.)

0

精彩评论

暂无评论...
验证码 换一张
取 消