使用工厂方法设置__weak变量似乎会使对象存活的时间过长 [英] Using a factory method to set a __weak variable seems to keep the object alive too long

查看:98
本文介绍了使用工厂方法设置__weak变量似乎会使对象存活的时间过长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Person类,实例化了两个对象:

I've created a Person class, of which I instantiate two objects:

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        Person * __weak pweak = [Person new];
        Person *p = [Person personWithName:@"Strong" lastName:nil dateOfBirth:nil];
    }
    return 0;
}

Person类将覆盖其dealloc方法,以便打印正在被释放的Person的名称.

The Person class overrides its dealloc method, so that it prints the name of the Person being deallocated.

一切都按预期进行,弱变量不能使Person实例保持活动状态,我在日志中看到了这一点("John"是Person对象的默认名称):

Everything goes as expected, the weak variable doesn't keep the Person instance alive, I see this in the log ("John" is the default name of a Person object):

2013-01-23 17:36:51.333 Basics[6555:303] John is being deallocated
2013-01-23 17:36:51.335 Basics[6555:303] Strong is being deallocated

但是,如果我在分配给弱变量的过程中使用了工厂方法:

However, if I use the factory method in the assignation to the weak variable:

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        Person * __weak pweak = [Person personWithName:@"Weak" lastName:nil dateOfBirth:nil];
        Person *p = [Person personWithName:@"Strong" lastName:nil dateOfBirth:nil];
    }
    return 0;
}

这是我看到的记录:

2013-01-23 17:44:16.260 Basics[6719:303] Strong is being deallocated
2013-01-23 17:44:16.262 Basics[6719:303] Weak is being deallocated

我做错什么了吗?

有关Person类的这些方法:

These methods of the Person class may be concerned:

- (id)initWithName:(NSString *)name lastName:(NSString *)lastName dateOfBirth:(NSDate *)birth {
    self = [super init];
    if (self) {
        _name = name;
        _lastName = lastName;
        _dateOfBirth = birth;
    }
    return self;
}

+ (id)personWithName:(NSString *)name lastName:(NSString *)lastName dateOfBirth:(NSDate *)birth {
    return [[self alloc] initWithName:name lastName:lastName dateOfBirth:birth];
}

推荐答案

通过alloc/init方法分配对象时,ARC赋予对象创建者负责稍后释放它的责任(因此,当您将对象存储为__strong它会一直存活,直到有人拥有它为止;当您将其存储为__weak时,它会被释放,因为没有人拥有它.)

When you allocate an object via the alloc/init method ARC gives to the creator of the object the responsability to release it later (so when you store an object as __strong it will stay alive until somebody owns it and when you store it as __weak it gets deallocated because nobody owns it).

从Apple文档(

您拥有创建的任何对象您可以使用名称以"alloc","new","copy"或 "mutableCopy"(例如alloc,newObject或mutableCopy).

You own any object you create You create an object using a method whose name begins with "alloc", "new", "copy", or "mutableCopy" (for example, alloc, newObject, or mutableCopy).

在处理工厂方法时,ARC将返回变量视为自动释放,因此在清空池时将其释放.实际上,ARC会将您的工厂方法转换为:

When dealing with factory methods ARC treats returning variables as autoreleasing so they are released when the pool will be drained. ARC, in facts, converts your factory method to this:

+ (id)personWithName:(NSString *)name lastName:(NSString *)lastName dateOfBirth:(NSDate *)birth 
{
   return [[[self alloc] initWithName:name lastName:lastName dateOfBirth:birth] autorelease];
}

这篇关于使用工厂方法设置__weak变量似乎会使对象存活的时间过长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆