2011-08-03 3 views
1

私は非同期要求を行っていると私はコードがある[responseData setLength:0];[responseData setLength:0]でクラッシュします。

にEXC_BAD_ACCESSを取得しています:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    [responseData setLength:0]; 
} 

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    [responseData appendData:data]; 
} 

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    NSLog(@"Connection failed: %@", [error description]); 
} 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
    [responseData release]; 

    luckyNumbers1 = [responseString JSONValue]; 

    NSUserDefaults *information = [NSUserDefaults standardUserDefaults]; 
    /*NSArray *luckyNumbers = [json objectWithString:responseString error:&error];*/ 
    NSLog(@"luckyNumbers1: %@", luckyNumbers1); 
    NSLog(@"user Info array is: %@", luckyNumbers1); 
    // [information setObject:[NSString stringWithFormat:@"%@", (NSString *)[luckyNumbers1 objectForKey:@"session"]] forKey:@"sessionID"]; 
    NSDictionary *array = (NSDictionary *)[luckyNumbers1 objectForKey:@"data"]; 
    NSEnumerator *inner = [luckyNumbers1 objectEnumerator]; 
    id value; 
    while((value = [inner nextObject])) { 
     NSLog(@"user Info array is: %@", value); 
     if ([value isKindOfClass:[NSDictionary class]]) { 
      [information setObject:[value objectForKey:@"id"] forKey:@"userID"]; 
      NSLog(@"uid is: %@", [value objectForKey:@"id"]); 
      [information setObject:[NSString stringWithFormat:@"%@", (NSString *)[value objectForKey:@"points_all"]] forKey:@"pointString"]; 
      [information setObject:[NSString stringWithFormat:@"%@", (NSString *)[[[value objectForKey:@"leaderboards"] objectForKey:@"all"] objectForKey:@"position"]] forKey:@"rankString"]; 
      if ((NSNull *)[value objectForKey:@"display_name"] == [NSNull null]) { 
       [information setObject:@"No Display Name" forKey:@"displayNameString"]; 
      } else { 
       [information setObject:[NSString stringWithFormat:@"%@", (NSString *)[value objectForKey:@"display_name"]] forKey:@"displayNameString"]; 
      } 
      if ((NSNull *)[value objectForKey:@"level"] == [NSNull null]) {} else { 
       [information setObject:[NSString stringWithFormat:@"%@", (NSString *)[[[value objectForKey:@"level"] objectForKey:@"definition"] objectForKey:@"name"]] forKey:@"levelString"]; 
      } 
      pointsTV = [[UILabel alloc] initWithFrame:CGRectMake(222, 294, 441, 22)]; 
      pointsTV.text = [NSString stringWithFormat:@"Points: %@", [information stringForKey:@"pointString"]]; 
      pointsTV.backgroundColor = [UIColor clearColor]; 
      pointsTV.textColor = [UIColor whiteColor]; 
      [TVWindow addSubview:pointsTV]; 

      rankLabelTV = [[UILabel alloc] initWithFrame:CGRectMake(222, 269, 441, 22)]; 
      rankLabelTV.backgroundColor = [UIColor clearColor]; 
      rankLabelTV.textColor = [UIColor whiteColor]; 
      rankLabelTV.text = [NSString stringWithFormat:@"Rank: %@", (NSString *)[[[array objectForKey:@"leaderboards"] objectForKey:@"all"] objectForKey:@"position"]];    
      [TVWindow addSubview:rankLabelTV]; 

      levelNameLabelTV = [[UILabel alloc] initWithFrame:CGRectMake(222, 244, 441, 22)]; 
      levelNameLabelTV.backgroundColor = [UIColor clearColor]; 
      levelNameLabelTV.textColor = [UIColor whiteColor]; 
      if ((NSNull *)[value objectForKey:@"level"] == [NSNull null]) { 
       levelNameLabelTV.text = @"No Level"; 
      } else { 
       levelNameLabelTV.text = [NSString stringWithFormat:@"Level: %@", (NSString *)[[[array objectForKey:@"level"] objectForKey:@"definition"] objectForKey:@"name"]]; 
      } 
      [TVWindow addSubview:levelNameLabelTV]; 

      pointLabel.text = [information stringForKey:@"pointString"]; 
      pointLabel.textAlignment = UITextAlignmentLeft; 
      displayNameLabelTV.text = [information stringForKey:@"displayNameString"]; 
      displayNameLabel. text = [information stringForKey:@"displayNameString"]; 
      rankLabel.text = [information stringForKey:@"rankString"]; 
      levelLabel.text = [information stringForKey:@"levelString"]; 

      NSURL *url1 = [NSURL URLWithString: [NSString stringWithFormat:@"%@", (NSString *)[array objectForKey:@"picture_url"]]]; 
      NSData *urlData1 = [NSData dataWithContentsOfURL:url1]; 
      UIImage *image1 = [UIImage imageWithData:urlData1]; 
      profilePicture.image = image1; 
      profilePictureTV.image = image1;  

      [information setObject:[NSString stringWithFormat:@"%@", (NSString *)[luckyNumbers1 objectForKey:@"session"]] forKey:@"sessionID"]; 
     }} 

    NSURLRequest *request2; 
    request2 = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@/api/widgets/%@/%@/players/%@/rewards.json", [information stringForKey:@"host"], [information objectForKey:@"apiKey"], [information objectForKey:@"URL"], [information objectForKey:@"userID"]]]]; 
    NSLog(@"Badges Request is: %@", request2); 
    NSURLConnection *connection2; 
    connection2 = [[NSURLConnection alloc] initWithRequest:request2 delegate:self startImmediately:YES]; 
    NSURLResponse *resp3; 
    NSData *cData2 = [NSURLConnection sendSynchronousRequest:request2 returningResponse:&resp3 error:nil]; 
    NSString *cDataString2 = [[NSString alloc] initWithData:cData2 encoding:NSUTF8StringEncoding]; 
    [self getUsersBadges: cDataString2];  
    [[NSURLConnection alloc] initWithRequest:request2 delegate:self]; 
    // pass cDataString into the JSON parser and update points 

    [connection release]; 
} 
+1

ここで、 'responseData'をどこで初期化しますか? – highlycaffeinated

答えて

2

reponseDataのあなたの初期化を見ることなく、それは確かに難しいですが、私はあることを推測します-connection:didReceiveResponse:になる前に[NSMutableData data](自動リリースされます)またはrelease/autoreleaseと宣告しています。解決策は、retainを呼び出すか、または[[NSMutableData alloc] init]で初期化することによって、早期にリリースされないことを確認することです。いずれの場合でも、完了したらすぐに解除する必要がありますが、すでにあなたの-connectionDidFinishLoading:にあるように見えるので、変更は必要ありません。

関連する問題