简体   繁体   English

目标C:发布对象基本问题(非ARC)

[英]Objective C: releasing objects basic questions (non-ARC)

I have two rookie questions about memory management in non-ARC project without GC: in Objective C,when I 'autorelease' object and return it from method, should I 'release' it in the "parent method" calling the method retuning the object? 在没有GC的非ARC项目中,我有两个关于内存管理的新手问题:在Objective C中,当我'自动释放'对象并从方法返回它时,我应该在“父方法”中释放它,调用方法重新调整对象?

The second question is, that I returned object (fe user ) from method (instance method on another object, fe userService ) and saved it to property ( self.userProp , self instantiated the object userService and called his method). 第二个问题是,我从方法返回对象(fe user )(另一个对象的实例方法,fe userService )并将其保存到属性( self.userProp自我实例化对象userService并调用他的方法)。 When I released the object userService (whose method returned the object user , stored in property self.userProp ), it released also the object stored in property (object user stored in property self.userProp ). 当我发布对象userService (其方法返回对象用户 ,存储在属性self.userProp中 )时,它还释放了存储在property中的对象(对象用户存储在属性self.userProp中 )。 Why? 为什么? Who was the owner of the object in property? 谁是物业中物体的拥有者?

Thank you 谢谢

when I 'autorelease' object and return it from method, should I 'release' it in the "parent method" calling the method retuning the object? 当我'自动释放'对象并从方法返回它时,我应该在“父方法”中“释放”它,调用方法重新调整对象吗?

No. Here's why. 不,这就是原因。 Let's layout an example exactly as you've said: 让我们按照你的说法完全布局一个例子:

-(NSObject*)someMethodThatReturnsAnAutoreleasedObject {
    return [[[NSObject alloc]init]autorelease];
}

-(void)myMethod {
    NSObject *obj = [self someMethodThatReturnsAnAutoreleasedObject];
    [obj doSomething];
    [obj release] //PROBLEM!
}

Look at the retain count of the objects step by step, and you'll see that while the -autorelease may not immediately cause an issue for you, it will at some later date (because -autorelease 'd objects aren't "auto-released", rather they are deallocated en masse when their owning pool is drained or deallocated). 一步一步地查看对象的保留计数,你会看到虽然-autorelease可能不会立即给你带来问题,但它会在以后某个时候(因为-autorelease对象不是“自动 -释放“,而不是当他们的拥有池被排空或解除分配时,他们被大量释放)。 The method that returns an autoreleased object returns it with an eventual retain count of 0 (0 (start) + 1 (alloc) - 1 (autorelease)), so you releasing it is not only unnecessary, but will cause a crash. 返回自动释放对象的方法返回它,最终保留计数为0(0(开始)+ 1(alloc) - 1(自动释放)),因此释放它不仅不必要,而且会导致崩溃。

The second question is, that I returned object (fe user) from method (instance method on another object, fe userService) and saved it to property (self.userProp, self instantiated the object userService and called his method). 第二个问题是,我从方法返回对象(fe user)(另一个对象的实例方法,fe userService)并将其保存到属性(self.userProp,自我实例化对象userService并调用他的方法)。 When I released the object userService (whose method returned the object user, stored in property self.userProp), it released also the object stored in property (object user stored in property self.userProp). 当我发布对象userService(其方法返回对象用户,存储在属性self.userProp中)时,它还释放了存储在property中的对象(对象用户存储在属性self.userProp中)。 Why? 为什么? Who was the owner of the object in property? 谁是物业中物体的拥有者?

Again, a little setup first (this is rather convoluted at first, but makes complete sense afte you see what you're doing). 再次,先进行一点设置(这首先是相当复杂的,但是在你看到你正在做的事情之后完全有意义)。

-(NSUserService*)someMethodThatReturnsAnAutoreleasedObject {
    return [[[NSUserService alloc]init]autorelease];
}

-(void)myMethod {
    self.userservice = [self someMethodThatReturnsAnAutoreleasedObject];
    [obj doSomething];
    [obj release] //No problem, assuming userService is declared retain or strong.
}

Well, why is this different? 那么,为什么会有所不同呢? Memory qualifiers, that's why! 记忆限定符,这就是原因! When you declare a property, you declare a set of memory qualifiers to tell the compiler what kind of modifier to append to the assignment sign. 声明属性时,声明一组内存限定符以告诉编译器要将哪种修饰符附加到赋值符号。 Most properties are declared retain or strong ((under ARC), and hopefully your userService one is declared as such, as well) if they are object types, which makes the compiler interpret 大多数属性被声明为retain或strong(在ARC下),如果它们是对象类型,希望你的userService被声明为这样,这使编译器解释

self.userservice = [self someMethodThatReturnsAnAutoreleasedObject];

as

self.userservice = [[self someMethodThatReturnsAnAutoreleasedObject]retain];

Therefore, you must release it, or you have a leak on your hands (0 (start) + 1 (alloc) - 1 (autorelease) + 1 (retain) = +1). 因此,您必须释放它,否则您的手上有泄漏(0(开始)+ 1(分配) - 1(自动释放)+ 1(保留)= +1)。

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

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