第二次调用时使用JSONKit进行内存泄漏

时间:2011-07-01 14:06:18

标签: iphone objective-c ios json memory-leaks

我已经阅读了关于内存管理的Apples文档并且觉得我理解它们但是我不能让它不泄漏。在这个例子中,我在主线程上运行进程以保持简单。点击第一次搜索按钮一切正常,没有泄漏。点击/执行第二次搜索,一切正常,但仪器显示以下泄漏:

Leaked Object   #   Address Size    Responsible Library Responsible Frame
NSCFString,42   < multiple >    1.30 KB CTContacts  jk_cachedObjects
NSCFString,16   < multiple >    464 Bytes   CTContacts  jk_cachedObjects
JKDictionary,7  < multiple >    224 Bytes   CTContacts  jk_object_for_token
Malloc 288 Bytes,7  < multiple >    1.97 KB CTContacts  jk_object_for_token
Malloc 32 Bytes,    0x7859a30   32 Bytes    CTContacts  jk_object_for_token
JKArray,    0x78599f0   32 Bytes    CTContacts  jk_object_for_token

它似乎指向这一行:(列为%100)

NSDictionary *resultsDictionary = [jsonData objectFromJSONDataWithParseOptions:JKParseOptionStrict error:(NSError **)error];

我试过NSDictionary * resultsDictionary = [[[NSDictionary alloc] init] autorelease];但结果相同。

以下是涉及的两种方法:

- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar {    
pickerView.hidden=YES;
searchBar.showsScopeBar=YES;
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];

[self queryWebService];
}

-(void) queryWebService{

NSString *urlAddress = [NSString stringWithFormat:@"http://myweb.com/json.php?lname=%@&searchType=%@",searchBar.text,currentSearchCategory];

NSURL *url = [NSURL URLWithString:urlAddress];

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startSynchronous];
NSError *error = [request error];
if (!error){
    NSString *responseString = [request responseString];
    //NSLog(@"Response: %@", responseString);

    NSData *jsonData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error = nil;

    NSDictionary *resultsDictionary = [jsonData objectFromJSONDataWithParseOptions:JKParseOptionStrict error:(NSError **)error];

    if (resultsDictionary)
    {
        rows = [[resultsDictionary objectForKey:@"contacts"] retain];
        resultsDictionary=nil;
    }
}        
[myTableView reloadData];
}

NSArray“rows”用作tableView dataSource。任何帮助将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:3)

我认为rows是原因。每次运行循环时,都会向其添加另一个retain。摆脱retain应该做的伎俩,摆脱内存泄漏。如果出于某种原因,那里需要retain,您只需要在其他地方找到一个地方来释放它并将保留计数保持在适当的值