简体   繁体   English

ARC下未释放的Objective-C属性

[英]Objective-c property not released under the ARC

I want to test properties attributes under the arc. 我想测试弧形下的属性属性。 I have created two NSString properties under Class1.h (interface file): 我在Class1.h (接口文件)下创建了两个NSString属性:

@interface Class1 : NSObject
@property (nonatomic, strong) NSString *str1;
@property (nonatomic, weak) NSString *str2;
@end

Then I have created a test method under Class1.m (implementation file): 然后,我在Class1.m (实现文件)下创建了一个测试方法:

@implementation Class1

- (void)testMethod {
    NSString *strt1 = @"exampleString1";
    NSString *strt2 = @"exampleString2";

    self.str1 = strt1;
    self.str2 = strt2;

    strt1 = nil;
    strt2 = nil;

    dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, QOS_CLASS_BACKGROUND);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), aQueue, ^{
        [self testMethod2];
    });
}

- (void)testMethod2 {
    NSLog(self.str1);
    NSLog(self.str2);
}
@end

I put a break point in testMethod2 method then check my NSString properties. 我在testMethod2方法中放置一个断点,然后检查我的NSString属性。 I assume str2 set nil but after I run the code the result is; 我假设str2设置为nil,但是运行代码后的结果是:

2015-07-03 14:14:04.412 ARCTEST[12303:6239959] exampleString1 2015-07-03 14:14:04.412 ARCTEST [12303:6239959] exampleString1

2015-07-03 14:14:04.412 ARCTEST[12303:6239959] exampleString2 2015-07-03 14:14:04.412 ARCTEST [12303:6239959] exampleString2

Can someone explain me why str2 property not released? 有人可以解释一下为什么str2属性未释放吗?

Objective-c uses a string literal pool. Objective-c使用字符串文字池。 That is all strings literals that are the same(text) point to the same object(works since strings are immutable). 那就是所有相同(文本)的字符串文字都指向同一个对象(因为字符串是不可变的,所以可以工作)。 These strings are never deallocated. 这些字符串永远不会释放。 It will work as expected if you change your assignment of your strt2 to. 如果将strt2的分配更改为预期,它将按预期工作。

NSString *strt2 = [NSString stringWithFormat:@"%@", @"exampleString2"];

Because it's a string literal. 因为它是字符串文字。 It will stay in memory throughout all the application's lifetime. 它将在应用程序的整个生命周期中一直保留在内存中。 Try another class and you'll see it will be released. 尝试其他课程,您将看到它会被释放。 And by the way, when dealing with strings you better use copy than strong or weak 顺便说一句,在处理字符串时,最好使用copy不是strongweak

Beside the correct answers of Peter and Andrey, I want to add that you can never be sure that an object is deallocated and a weak reference is set to nil . 除了彼得和安德烈(Peter and Andrey)的正确答案之外,我还要补充一点,您永远无法确定是否已释放对象并将弱引用设置为nil

  • ARC does not promise that an object is released as early as possible. ARC不保证尽快释放对象。
  • An object could be created in the ARP. 可以在ARP中创建一个对象。 In such a case it is not released before returning to the run loop. 在这种情况下,它不会在返回运行循环之前释放。
  • An object can be cached for re-use. 可以缓存一个对象以供重用。

It is simply the wrong point of view to expect a deallocation. 期望解除分配只是错误的观点。 You give up a reference, nothing else. 您放弃参考,仅此而已。 Think in references, not in life time. 在参考中思考,而不是在生活中思考。

If you want a reference to be nil , nil it out. 如果您希望引用为nil ,请取消引用。 Don't rely on MM, because it is not made for this. 不要依赖MM,因为它不是为此而设计的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM