【问题标题】:Can't figure out this leak无法弄清楚这个泄漏
【发布时间】:2010-07-22 16:27:35
【问题描述】:
SettingsView *settings = [[SettingsView alloc] initWithNibName:@"SettingsView" bundle:[NSBundle mainBundle]];
settings.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self.navigationController presentModalViewController:settings animated:YES];
settings = nil;
[settings release];

仪器声称以下行泄漏

[self.navigationController presentModalViewController:settings animated:YES];

【问题讨论】:

    标签: objective-c iphone memory-leaks


    【解决方案1】:

    您需要在将settings 设置为nil 之前释放settings,而不是之后!

    你现在做的和:

    settings = nil;
    [nil release];
    

    所以-release 被发送到nil,而不是你的SettingsView 对象。 (并且向nil 发送任何消息都是NOOP)。

    【讨论】:

      【解决方案2】:

      没错。由于从未使用@property 和@synthesize 设置'settings',将其设置为nil 只会擦除它所持有的内存地址。

      如果你设置了 @property (nonatomic, retain) SettingsView *settings;

      然后再致电settings = nil;

      还会发送[settings release] 消息。

      【讨论】:

      • FWIW,即使 settings 是声明的属性,语句settings = nil 也不会调用访问器设置器方法;它只会将 ivar 设置为 nil。您需要执行 self.settings = nil; 才能获得您所描述的行为。
      猜你喜欢
      • 2011-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多