我可以返回分配的对象并在函数外释放它吗?

发布于 2024-10-27 04:43:45 字数 546 浏览 0 评论 0原文

- (NSString *)allocString{
    NSString *str = [[NSString alloc] init];
    return str;
}

- (void)viewDidLoad{
    NSString *name = [self allocString];

    [name release]; // Can I release an object here?
}

你好,我刚刚使用 NSString 写了一个简单的例子。在我的实际程序中,我使用了自定义 UIView 类,而不是 NSString。

allocString 创建一个 NSString 实例,其保留计数为 1。如果没有 autoreleasing,它只是返回一个实例。

在 viewDidLoad 方法中,变量“name”只是指向 allocString 方法返回的对象,因此实例的保留计数仍然为 1。

因此,[name release] 会减少其保留计数。

这是对的吗?或者我是否必须在 allocString 方法中自动释放它?谢谢你!

- (NSString *)allocString{
    NSString *str = [[NSString alloc] init];
    return str;
}

- (void)viewDidLoad{
    NSString *name = [self allocString];

    [name release]; // Can I release an object here?
}

Hi, I just wrote a simple example using NSString. In my real program, instead of NSString, I used a custom UIView class.

allocString creates one NSString instance and its retain count is 1. Without autoreleasing, it just returns an instance.

In viewDidLoad method, a variable "name" just points to an object returned by allocString method, so an instance's retain count is still 1.

Thus, [name release] decreases its retain count.

Is this right? Or do I have to autorelease it in allocString method? Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

刘备忘录 2024-11-03 04:43:45

你所做的将会起作用。您可以从您选择的任何范围内保留释放对象,只要一切都平衡即可(并且当任何其他对象不再需要该对象时,该对象将被释放) 。

但你所做的并不是传统的。如果您想让对象在方法的生命周期结束后继续存在(以便您可以返回它并让方法调用者拾取它),那么您应该使用autorelease代码>.

在您的示例中,第一个方法可能不应该保留字符串的所有权。它应该释放它并让呼叫者接听它。但是,当然,如果您在方法中release它,它将立即被释放。因此,您可以autorelease它。通过自动释放它,您的第一个方法是说“我不再需要这个对象并放弃我的所有权,但请将其在内存中保留一段时间,以便我的调用者可以在需要时保留它”。

下面是一个示例:

- (NSString *)createString {
    /* Create a string. This method owns it. */
    NSString *str = [[NSString alloc] init];

    /* Autorelease it. This method does not own it, but wants it to stay in memory temporarily. */
    [str autorelease]

    /* Return it. It will stay in memory till the end of the current run loop */
    return str;
}

- (void)viewDidLoad{
    NSString *name = [self createString];
    /* We now have a copy of the string. Nobody owns it. */
    /* It is due to be released at the end of the current run loop. */

    /* If we want to take ownership of it and prevent deallocation, we should retain it. */
    [name retain];
}

整个 Cocoa API 都使用这种行为。当您看到返回新对象的类方法时,该对象将被自动释放。例如,当您调用 [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil] 时,您正在要求 NSArray 类为您创建一个数组。由于其自动释放状态,如果存在于 arrayWithObjects: 方法之外。如果您不保留它,它将在当前运行循环结束时释放。

What you have done will work. You can retain and release objects from any scope you choose, so long as it all evens out (and the object is deallocated when it is no longer needed by any other object).

But what you are doing is not conventional. If you want to allow the object to live on past the life of a method (so that you can return it and have it picked up by the method caller) then you should use autorelease.

In your example, the first method should probably not retain ownership of the string. It should release it and let the caller pick it up. But, of course, if you release it in the method it will be instantly deallocated. So instead you can autorelease it. By autoreleasing it, your first method is saying "I no longer need this object and relinquish my ownership, but please keep it in memory a little while longer so my caller can retain it if it needs to".

Here's an example:

- (NSString *)createString {
    /* Create a string. This method owns it. */
    NSString *str = [[NSString alloc] init];

    /* Autorelease it. This method does not own it, but wants it to stay in memory temporarily. */
    [str autorelease]

    /* Return it. It will stay in memory till the end of the current run loop */
    return str;
}

- (void)viewDidLoad{
    NSString *name = [self createString];
    /* We now have a copy of the string. Nobody owns it. */
    /* It is due to be released at the end of the current run loop. */

    /* If we want to take ownership of it and prevent deallocation, we should retain it. */
    [name retain];
}

This behaviour is used all throughout the Cocoa APIs. When you see class methods that return a new object, that object will be autoreleased. For example, when you call [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil] you are asking the NSArray class to create an array for you. If exists outside of the arrayWithObjects: method thanks to its autoreleased status. If you do not retain it, it will be released at the end of the current run loop.

小…楫夜泊 2024-11-03 04:43:45

如果您只需将“allocString”方法名称替换为“newString”,它就可以正常工作。
Objective C 中有一个约定来处理此类方法。该约定是方法名称应以“new*”开头,并且不会引发任何内存泄漏。

If you just replace "allocString" method name to "newString", it works fine.
There's a convention in objective C to handle such methods. That convention is that the method name should start with "new*" and it will not throw any memory leak.

月朦胧 2024-11-03 04:43:45

是的,你拥有的一切都会正常工作。

Yes, everything you have there will work just fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文