ASIFormDataRequest Exc_Bad_Access

时间:2011-11-28 17:01:28

标签: iphone xcode json asiformdatarequest

我正在使用ASIFormDataRequest,它使用以下代码在某处给我EXC_BAD_ACCESS。我启用了NSZombie,据我所知它与ASIFormDataRequest有关,它说负责的库是GraphicsServices。

我有一个单独的类,它包含用于获取数据的不同方法,并且从我的UIViewControllers中可以调用这些方法...

- (Account *) signInAccount:(NSString *)duid user:(NSString *)userName password:    (NSString *)passWord
{
Account *myAccount = [[[Account alloc] init] autorelease];

NSURL *apiURL = [NSURL URLWithString:@"http://xxxxxxxxxxxxxxx.com/api.json"];
ASIFormDataRequest *request = [[ASIFormDataRequest requestWithURL:apiURL] autorelease];
[request addPostValue:@"duid025" forKey:@"duid"]; //todo - add the actual passed in value
[request addPostValue:userName forKey:@"user[email]"];
[request addPostValue:passWord forKey:@"user[password]"];
[request setUsername:@"xxxxxxxx"];
[request setPassword:@"yyyyyyyy"];

[request startSynchronous]; 

int statusCode = request.responseStatusCode;

NSDictionary *json = [request.responseString JSONValue];
NSString *status = [json valueForKeyPath:@"status"];
NSLog(@"API JSON %@", request.responseString);

myAccount.statusCode = statusCode;

if(statusCode == 200)
{
    myAccount.status = status;
    myAccount.error = @"";
    Credentials *myCredentials = [[Credentials alloc] init];
    myCredentials.emailAddress = userName;
    myCredentials.passWord = passWord;
    myAccount.credentials = myCredentials;

    //assign division to default division

}
else if(statusCode==403)
{
    NSString *error = [json valueForKeyPath:@"error"];
    myAccount.status = status;
    myAccount.error = error;
}
else
{
    // major issue, probably connection
}

return myAccount;
}

2 个答案:

答案 0 :(得分:1)

在第

ASIFormDataRequest *request = [[ASIFormDataRequest requestWithURL:apiURL] autorelease];

您正在对您不拥有的对象调用自动释放。您只需要释放您拥有的对象(即使用包含名称new,alloc或copy的方法创建的对象)。

将行更改为

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:apiURL];

请求对象已经自动释放。

答案 1 :(得分:1)

我认为问题出在这一行:

ASIFormDataRequest *request = [[ASIFormDataRequest requestWithURL:apiURL] autorelease];

由于您没有使用alloc / init分配和初始化请求对象,因此该对象已经自动释放。因此,您正在对已经自动释放的对象调用autorelease。试试这个:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:apiURL];