简体   繁体   English

memory 在第二次调用时使用 JSONKit 泄漏

[英]memory leak using JSONKit when called a second time

I've read the Apples docs on memory management and feel I understand them but I can't get this to not leak.我已经阅读了有关 memory 管理的 Apples 文档,并觉得我理解它们,但我不能让它不泄漏。 In this example I have the process running on the main thread to keep it simple.在这个例子中,我让进程在主线程上运行以保持简单。 The first time search button is clicked all works fine, no leaks.第一次点击搜索按钮一切正常,没有泄漏。 Second time search is clicked/perfomed everything works find but instruments displays the following leaks:第二次单击/执行搜索,一切正常,但仪器显示以下泄漏:

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

it seems to be pointing to this line: (listed as %100)它似乎指向这一行:(列为 %100)

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

I've tried NSDictionary *resultsDictionary =[ [[NSDictionary alloc]init]autorelease];我试过 NSDictionary *resultsDictionary =[ [[NSDictionary alloc]init]autorelease]; but with same result.但结果相同。

Below are the two methods involved:以下是涉及的两种方法:

- (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" is used as the tableView dataSource. NSArray "rows" 用作 tableView 数据源。 Any help would be appreciated, thanks.任何帮助将不胜感激,谢谢。

I'd imagine that rows is the cause.我想那rows是原因。 Each time you run through the loop, you add another retain to it.每次运行循环时,都会向其中添加另一个retain Getting rid of the retain should do the trick and get rid of the memory leak.摆脱retain应该可以解决问题并摆脱 memory 泄漏。 If for some reason, a retain is necessary there, you'll just have to find a place elsewhere to release it and keep your retain count at the proper value如果由于某种原因,那里需要retain ,您只需在其他地方找到一个地方来释放它并将保留计数保持在适当的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM