简体   繁体   English

Objective-C:对象初始化和内存管理

[英]Objective-C: object init and memory management

Given the next code snippet: 给出下一个代码段:

... ...

- (void) setTotalAmount: (NSNumber*)input
{
    [totalAmount autorelease];
    totalAmount = [input retain];
}

- (void) dealloc
{
    [totalAmount release];
    [super dealloc];
}

... ...

What I want to understand how really we set value. 我想了解我们如何真正地创造价值。 We allocate local (instance) var and "retain" to input var. 我们分配本地(实例)var和“保留”到输入var。 But what is "input"? 但是什么是“输入”? Is it a pointer to real value? 它是指向真实价值的指针吗? Or is it value itself? 还是它本身有价值? When we "retain" do we get a pointer to "input" or pointer to value or just the value? 当我们“保留”时,会得到指向“输入”的指针还是指向值的指针,或者仅仅是值?

And pretty same questions with dealloc and release. 还有关于dealloc和release的相同问题。 What actually "dies" here? 到底是什么“死了”?

Thank you! 谢谢!

It's clear that input is a pointer. 显然, input是一个指针。 You have declared its type in the argument list as NSNumber* . 您已在参数列表中将其类型声明为NSNumber*

input is a pointer to an object of type NSNumber . input是指向NSNumber类型的对象的指针。 Internally, the object has an integer variable that holds the number of external references to it. 在内部,该对象具有一个整数变量,该变量保存对其的外部引用的数量。 Sending the retain message to an object will increment its reference count. retain消息发送到对象将增加其引用计数。 Sending the release message will decrement the count. 发送release消息将减少计数。 Sending the autorelease message will adds the object to the local autorelease pool which will keep track of autoreleased objects and sends the release message to them next time it drains. 发送autorelease消息会将对象添加到本地自动释放池,该池将跟踪自动释放的对象,并在下次耗尽时将release消息发送给它们。 An object with reference count of 1 that receives a release message will get deallocated and its dealloc method will get called. 收到release消息的引用计数为1的对象将被释放,并且其dealloc方法将被调用。 You should release all the resources you hold when you are deallocated. 释放后,应释放所有资源。

When you are setting a property, you want to release the old value and make sure the new value is kept around as long as the object itself is alive. 设置属性时,要释放旧值,并确保只要对象本身还处于活动状态,新值就保持不变。 To make sure the new value is kept around, you increment its reference count by 1 by sending it the retain message. 为确保新值保持不变,您可以通过向其发送retain消息来将其参考计数增加1。 To release the old object, you'll send it the release message. 要释放旧对象,您将向其发送释放消息。 There's one subtle issue here. 这里有一个微妙的问题。 If the old value is the same as the new value, if you release the old value first and its retain count was 1, it'll get destroyed before you can increment it. 如果旧值与新值相同,则如果您首先释放旧值并且其保留计数为1,则在将其递增之前会被销毁。 That's why you should retain the new value before releasing the old one. 这就是为什么您应该在释放旧值之前保留新值。

Assuming that you have instantiated an object of the above class named "myObject", you could set its totalAmount like this: 假设您已实例化了一个名为“ myObject”的上述类的对象,则可以像这样设置其totalAmount:

... ...

// get a new NSNumber object which will be autoreleased
NSNumber *amount = [NSNumber numberWithInteger:10];

// call the setter which will autorelease the previous value of
// totalAmount (if any), retain the object referenced by "amount"
// (so that object will no longer be deallocated when it's autoreleased),
// and reference that object from the totalAmount instance variable
myObject.totalAmount = amount;

... ...

Later, when "myObject" is deallocated, the object referenced by the totalAmount instance variable will be released (and deallocated if it was not retained anywhere else). 稍后,当释放“ myObject”时,由totalAmount实例变量引用的对象将被释放(如果未在其他任何地方保留,则被释放)。

input is the NSNumber 输入的是NSNumber

- (void) setTotalAmount: (NSNumber*)**input**

Which is called by doing something like this: 通过执行以下操作可以调用该方法:

[object setTotalAmount:number];

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

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