自从升级到Lion并因此升级到XCode 4.1
运行分析器时,我收到了数十个"潜在的内存泄漏".
我通常会使用如下属性列表:
@synthesize indexPath = _indexPath;
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle {
self = [super initWithNibName:nibName bundle:nibBundle];
self.indexPath = [[NSIndexPath alloc] init];
[_indexPath release];
return self;
}
Run Code Online (Sandbox Code Playgroud)
并在dealloc()方法中:
- (void)dealloc {
[_indexPath release];
[super dealloc];
}
Run Code Online (Sandbox Code Playgroud)
现在,分析会告诉我self.indexPath上可怕的蓝色消息,告诉我有泄漏.什么时候显然没有.
你如何分配和格式化代码,以便XCode不相信它的泄漏?(同时保留属性别名self.var vs _var)
谢谢...
其他答案已经深入解释了这个问题,无论如何,这些是一些可用于避免此错误的常见模式:
NSIndexPath *ip = [[NSIndexPath alloc] init];
self.indexPath = ip;
/* ... */
[ip release];
Run Code Online (Sandbox Code Playgroud)
indexPath = [[NSIndexPath alloc] init];
Run Code Online (Sandbox Code Playgroud)
self.indexPath = [[[NSIndexPath alloc] init] autorelease];
Run Code Online (Sandbox Code Playgroud)
self.indexPath = [NSIndexPath indexPathWithIndex:...];
Run Code Online (Sandbox Code Playgroud)
在init中你真的应该使用ivars直接设置:
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle {
self = [super initWithNibName:nibName bundle:nibBundle];
_indexPath = [[NSIndexPath alloc] init];
return self;
}
Run Code Online (Sandbox Code Playgroud)
也许这可以解决这个问题?它将遵循惯例.
| 归档时间: |
|
| 查看次数: |
3997 次 |
| 最近记录: |