2011-09-06 5 views
1

내 응용 프로그램은 rqst_run 메서드를 아래의 ViewLoad 메서드에서 호출했지만 오류가 발생했습니다.[CFDictionary count] : 할당 취소 된 인스턴스로 전송 된 메시지

[CFDictionary count]: message sent to deallocated instance 

및 디버그 마커 (아래있는 tableView numberOfRowsInSection 메소드에서)이 라인에 배치됩니다 :

if([self.listing_items count] > 0) 

나도 몰라이 변수가

을 발표 얻을 디버거는 다음과 같은 오류를보고합니다 헤더 파일 (인터페이스 섹션)에 선언 됨 :

NSMutableString  *rqst_error; 
NSMutableData  *rqst_data; 
NSMutableDictionary *listing_items; 

구현 방법 :

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if([self.listing_items count] > 0) 
    { 
     if([self.listing_items objectForKey:@"items"]) 
     { 
      return [[self.listing_items objectForKey:@"items"] count]; 
     } 
    } 
} 

- (void)rqst_run 
{ 
    rqst_data = [[NSMutableData data] retain]; 
    NSMutableURLRequest *http_request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.feedserver.com/request/"]]; 
    [http_request setHTTPMethod:@"POST"]; 
    NSString *post_data = [[NSString alloc] initwithFormat:@"param1=%@&param2=%@&param3=%@",rqst_param1,rqst_param2,rqst_param3]; 
    [http_request setHTTPBody:[post_data dataUsingEncoding:NSUTF8StringEncoding]]; 
    rqst_finished = NO; 
    [post_data release]; 
    NSURLConnection *http_connection = [[NSURLConnection alloc] initWithRequest:http_request]; 
    [http_request release]; 

    if(http_connection) 
    { 
     [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; 

     if([rqst_data length]>0) 
     { 
      NSString *rqst_data_str = [[NSString alloc] rqst_data encoding:NSUTF8StringEncoding]; 
      SBJsonParser *json_parser = [[SBJsonParse alloc] init]; 
      id feed = [json_parser objectWithString:rqst_data_str error:nil]; 
      listing_items = (NSMutableDictionary *)feed; 
      [json_parser release]; 
      [rqst_data_str release]; 
     } 
     else 
     { 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Feed" message:@"No data returned" delegate:self cancemButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
      [alert show]; 
      [alert release]; 
     } 
    } 
    else 
    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Problem" message:@"Connection to server failed" delegate:self cancemButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
     [alert show]; 
     [alert release]; 
    } 
} 

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

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

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    [rqst_data release]; 
    [connection release]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    [rqst_data release]; 
    [connection release]; 
    rqst_finished = YES; 
} 
+0

여기서'listing_items'을 (를) 할당하고 있습니까? – Mat

답변

3

NSMutableDictionary는 사용 전에 올바르게 초기화해야합니다. 코드에서 listing_items에 피드를 할당 한 다음 릴리스합니다. 그것은 유지되지 않았으므로 listing_items도 제거됩니다.

listing_items = [[NSMutableDictionary alloc] initWithDictionary:feed]; 

을 모두 잘 작동합니다 : 이 같은 초기화 사전에보십시오. 당신은이 바르에서 오토 릴리즈 값을 넣어 * 당신은 재산 대신 인스턴스 변수를 사용 * 이 listing_items 에 값을 할당 :

0

그것은 꽤 분명하다. feed이 autorleased 변수 (다음 정의에 의해 현재 runloop의 끝에서 해제되기 때문에

listing_items = (NSMutableDictionary *)feed;

는 명확하게 오류입니다.

하나가 listing_items에 대한 @property(retain)를 선언하고 그것을 할당 할 때마다 사용

또는 저장된 값을 수동으로 유지합니다 (하지만 새 값을 할당하기 전에 이전 값을 해제하는 것을 잊지 않아도되므로 고통 스럽습니다. 한 번에 listing_items에 ... 세터 메소드가하는 것입니다. 직접 또는 속성 할당을 통해 호출 됨)

0

NSMutableDictionary를 초기화해야한다고 생각합니다. 바로 지금 피드를 가리키는 포인터입니다. 피드가 해제되면 그냥 무시됩니다.

viewDidLoad : listing_items = [[NSMutableDictionary alloc] init];

또는 데이터를 유지해야합니다. listing_items = [(NSMutableDictionary *) feed retain]; 대신 당신이 json_parser을 놓으면이

self.listing_items = [NSMutableDictionary dictionaryWithDictionary:feed]; 
1

. 다른 사람들이 말했듯이 json_parser에서 얻은 사전을 유지해야합니다.

0
SBJsonParser *json_parser = [[SBJsonParse alloc] init]; 
id feed = [json_parser objectWithString:rqst_data_str error:nil]; 
listing_items = (NSMutableDictionary *)feed; 
[json_parser release]; 

,이 보유하고있는 사전도 출시이

listing_items = (NSMutableDictionary *)feed; 

이용

관련 문제