【问题标题】:Are synthesized properties automatically released in dealloc? [duplicate]合成的属性会在dealloc中自动释放吗? [复制]
【发布时间】:2012-01-08 11:34:38
【问题描述】:

可能重复:
How is release handled for @synthesized retain properties?

我看到有人在某处(我不记得是谁或在哪里)提到合成的属性在 dealloc 中自动设置为 nil 并因此被释放。然而 Instruments 的想法不同,只是将所有内容都标记为内存泄漏。
哪一个是正确的?也许我错过了使这种情况发生的功能? (这绝对不是我在 iOS 4.1 发布时看到的弧线)

【问题讨论】:

    标签: objective-c memory-management properties


    【解决方案1】:

    看看这个问题:How is release handled for @synthesized retain properties?

    所以几乎你必须自己发布它。它不会自动为您完成。

    【讨论】:

      【解决方案2】:

      不,他们不是。您必须在 dealloc 上释放它。

      如果属性配置了retain 选项,则无论何时调用其设置器,它都会收到retain 消息,因此需要未来的release

      【讨论】:

        【解决方案3】:

        如果您使用 ARC 并且您的 ivars 是objective-c 对象,那么它们会为您释放:)

        示例:

        @interface Car : NSObject {
        }
        @property (nonatomic, retain) NSString *modelName;
        @property (nonatomic) char *moreInfo;
        @end
        
        @implementation Car
        @synthesize modeName;
        @synthesize moreInfo;
        - (id)init{
            self = [super init];
            if (self) {
                moreInfo = (char *)malloc(128*sizeof(char)); // malloc'ed, 
            }
            return self;
        }
        - (void)dealloc{
            free(moreInfo); // malloc'ed vars are not automatically released
            //[modelName release]; // Since we are using ARC this is provided by the compiler 
            //[super dealloc]; // Since we are using ARC this is provided by the compiler 
        }
        

        @结束

         

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-02
          • 2014-08-25
          • 2013-01-08
          • 1970-01-01
          • 2011-06-13
          • 1970-01-01
          相关资源
          最近更新 更多