我可以返回分配的对象并在函数外释放它吗?
- (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你所做的将会起作用。您可以从您选择的任何范围内
保留
和释放
对象,只要一切都平衡即可(并且当任何其他对象不再需要该对象时,该对象将被释放) 。但你所做的并不是传统的。如果您想让对象在方法的生命周期结束后继续存在(以便您可以
返回
它并让方法调用者拾取它),那么您应该使用autorelease
代码>.在您的示例中,第一个方法可能不应该保留字符串的所有权。它应该释放它并让呼叫者接听它。但是,当然,如果您在方法中
release
它,它将立即被释放。因此,您可以autorelease
它。通过自动释放它,您的第一个方法是说“我不再需要这个对象并放弃我的所有权,但请将其在内存中保留一段时间,以便我的调用者可以在需要时保留它”。下面是一个示例:
整个 Cocoa API 都使用这种行为。当您看到返回新对象的类方法时,该对象将被自动释放。例如,当您调用 [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil] 时,您正在要求 NSArray 类为您创建一个数组。由于其自动释放状态,如果存在于
arrayWithObjects:
方法之外。如果您不保留
它,它将在当前运行循环结束时释放。What you have done will work. You can
retain
andrelease
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 useautorelease
.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 canautorelease
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:
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 thearrayWithObjects:
method thanks to its autoreleased status. If you do notretain
it, it will be released at the end of the current run loop.如果您只需将“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.
是的,你拥有的一切都会正常工作。
Yes, everything you have there will work just fine.