UISearchDisplayController 打字时内存泄漏堆积
我最近开始学习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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此行泄漏:
根据内存管理规则,您拥有自己创建的任何对象,并且使用名称以“alloc”、“new”、“copy”或“mutableCopy”开头的方法创建对象。
因此,
mutableCopy
返回一个保留的对象,并且searchResults
属性设置器也保留该对象,导致该对象过度保留。解决方案是通过向对象发送release
... 或autorelease
消息来放弃对象的所有权。
This line leaks:
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 thesearchResults
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 arelease
......or
autorelease
message.