【问题标题】:iphone dealloc propertyiphone dealloc 属性
【发布时间】:2011-10-03 00:16:50
【问题描述】:

我在 Xcode 上运行分析构建,并因为一个对象是属性和实例 var 而收到泄漏警告

.h

UIView *_transparentView; }
    @property (nonatomic, retain) UIView *transparentView;

.m

@synthesize transparentView = _transparentView;


self.transparentView = [[UIView alloc] initWithFrame:transparentViewFrame];

- (void)dealloc {
[_transparentView release];

所以我在dealloc上释放了ivar,但是如何释放属性?,[self.transparentview release] ??

【问题讨论】:

  • 您负责 2 次保留:1 次用于分配,1 次用于属性(保留属性),但您只执行 1 次发布。你可以添加一个 autorelease 到 self.transparentView = ... 是干净的。
  • 或者只是将分配的对象直接分配给_transparentView。

标签: ios instance dealloc


【解决方案1】:

正如汤姆回答的那样,将分配“transparentView”的行替换为:

self.transparentView = [[[UIView alloc] initWithFrame:transparentViewFrame] autorelease];

当你对一个保留的属性有任何值时,你应该释放分配的值,如果你完成了它,并在释放类时释放属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-21
    • 2010-11-30
    • 1970-01-01
    • 2012-04-10
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多