UISearchDisplayController 打字时内存泄漏堆积

发布于 2024-11-30 20:11:29 字数 1593 浏览 3 评论 0原文

我最近开始学习objective-C和cocoa。我慢慢开始学习内存管理。通常我有点掌握它的逻辑。但现在我有点困惑。这是我的问题:

我正在尝试制作一个带有搜索功能的简单ios应用程序。我有一个 UISearchDisplayController,其 UITableViewController 内有一个 UISearchbar,当我在其中输入内容时,它会像地狱一样泄漏(仪器..)。编译器和分析器都没有显示任何警告或错误。只是乐器。

我从handleSearchTerm 方法中的这一行收到错误:

self.searchResults = [[datenSort filteredArrayUsingPredicate:predicate] mutableCopy];

这是我的handleSearchTerm 方法:

- (void)handleSearchForTerm:(NSString *)searchTerm
{   
    self.savedSearchTerm = searchTerm;
    if ([self searchResults] == nil)
    {    

       NSMutableArray *array = [[NSMutableArray alloc] init];       
       self.searchResults = array;
       [array release], array = nil;
    }

    [[self searchResults] removeAllObjects];
    if ([[self savedSearchTerm] length] != 0)
    {

       NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(Name contains[cd] %@)", searchTerm];
       self.searchResults = [[datenSort filteredArrayUsingPredicate:predicate] mutableCopy];

      }


}

属性全部在dealloc 方法中释放。它们是:

@property (nonatomic, copy) NSString *savedSearchTerm;
@property (nonatomic, retain) NSMutableArray *datenSort;
@property (nonatomic, retain) NSMutableArray *searchResults;

这就是我将它们放入 tableview 中的方式:cellForRowAtIndexPath:

    if(tableView == self.searchDisplayController.searchResultsTableView){

        dataItem = [searchResults objectAtIndex:indexPath.row];

    }else {

        dataItem = [datenSort objectAtIndex:indexPath.row];

    }

非常感谢您的回答!

I recently started learning objective-C and cocoa. I'm slowly starting to learn about memory management. Usually i kinda grasp the logic of it. But right now i'm kinda confused. Here's my problem:

I am trying to make a simple ios app with search function. I have a UISearchDisplayController with a UISearchbar inside a UITableViewController that leaks (instruments..) like hell when I type something in it. Compiler and Analyzer both show no warnings or errors. Just instruments.

I get the error from this line inside the handleSearchTerm method:

self.searchResults = [[datenSort filteredArrayUsingPredicate:predicate] mutableCopy];

Here's my handleSearchTerm method:

- (void)handleSearchForTerm:(NSString *)searchTerm
{   
    self.savedSearchTerm = searchTerm;
    if ([self searchResults] == nil)
    {    

       NSMutableArray *array = [[NSMutableArray alloc] init];       
       self.searchResults = array;
       [array release], array = nil;
    }

    [[self searchResults] removeAllObjects];
    if ([[self savedSearchTerm] length] != 0)
    {

       NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(Name contains[cd] %@)", searchTerm];
       self.searchResults = [[datenSort filteredArrayUsingPredicate:predicate] mutableCopy];

      }


}

the properties are all released in the dealloc method. and they are:

@property (nonatomic, copy) NSString *savedSearchTerm;
@property (nonatomic, retain) NSMutableArray *datenSort;
@property (nonatomic, retain) NSMutableArray *searchResults;

and this is how i put them inside tableview:cellForRowAtIndexPath:

    if(tableView == self.searchDisplayController.searchResultsTableView){

        dataItem = [searchResults objectAtIndex:indexPath.row];

    }else {

        dataItem = [datenSort objectAtIndex:indexPath.row];

    }

your answers are VERY much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独守阴晴ぅ圆缺 2024-12-07 20:11:29

此行泄漏:

self.searchResults = [[datenSort filteredArrayUsingPredicate:predicate] mutableCopy];

根据内存管理规则,您拥有自己创建的任何对象,并且使用名称以“alloc”、“new”、“copy”或“mutableCopy”开头的方法创建对象。

因此,mutableCopy 返回一个保留的对象,并且 searchResults 属性设置器也保留该对象,导致该对象过度保留。解决方案是通过向对象发送 release... 或 autorelease 消息来放弃对象的所有权

NSArray *temp = [[datenSort filteredArrayUsingPredicate:predicate] mutableCopy];
self.searchResults = temp;
[temp release];

self.searchResults = [[[datenSort filteredArrayUsingPredicate:predicate] mutableCopy] autorelease];

This line leaks:

self.searchResults = [[datenSort filteredArrayUsingPredicate:predicate] mutableCopy];

According to the Memory Management rules, you own any object you create and you create an object using a method whose name begins with "alloc", "new", "copy", or "mutableCopy".

Therefore, mutableCopy returns a retained object and the searchResults property setter also retains the object, resulting in the object being over-retained. The solution is to relinquish ownership of the object by sending it a release...

NSArray *temp = [[datenSort filteredArrayUsingPredicate:predicate] mutableCopy];
self.searchResults = temp;
[temp release];

...or autorelease message.

self.searchResults = [[[datenSort filteredArrayUsingPredicate:predicate] mutableCopy] autorelease];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文