2011-09-06 101 views
1

我的應用正在調用rqst_run方法如下didViewLoad方法,但出現錯誤。調試器報告以下錯誤:[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; 

和我定義了t他的實現方法如下:

- (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必須在使用前正確初始化。在您的代碼中,您將feeds分配給listing_items,然後釋放它。它沒有被保留,所以list_items也被刪除。 嘗試像這樣的初始字典:

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

和所有應該工作正常。

0

這很清楚: *您使用實例變量而不是屬性來指定值listing_items *您在此ivar中放入一個自動發佈的值。

listing_items = (NSMutableDictionary *)feed;顯然是你的錯誤,因爲feed是autorleased變量(然後將在當前runloop的定義月底被釋放。

要麼宣佈@property(retain)listing_items,並使用它,你要分配的各個時間(自動發放)值(以便該屬性將管理分配時的保留/釋放)

或者手動保留存儲值(但這很痛苦,因爲在分配新分配之前不需要忘記釋放先前的值一次到listing_items每次...這是setter方法的作用en直接或通過財產分配)

0

我認爲你需要初始化你的NSMutableDictionary。現在它只是一個指向Feed的指針。當Feed被釋放時,它只是指向零。

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

或者您需要保留這些數據: listing_items = [(NSMutableDictionary *)feed retain];

1

而是這個

listing_items = (NSMutableDictionary *)feed; 

使用此

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

當你鬆開json_parser,其持有的字典被釋放過的。 因此,正如其他人所說,你需要保留從json_parser獲得的字典。

相關問題