如果没有声明 nsautoreleasepool,自动释放调用是否会崩溃?
抱歉,我是可可编程的新手,我不确定我是否真正理解 nsautoreleasepool 的工作原理。
我读到的所有地方都说 NSAutoreleasePool 负责所有自动释放调用(谈论最后声明的 NSAutoreleasePool)。
考虑以下代码:
int main(int argc, char *argv[]) {
//NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
//[pool release];
return retVal;
}
在我的应用程序的某个时刻,我也会有类似的东西:
NSString* b = [[NSString alloc] initWithFormat:@"%d", 10];
[b autorelease];
考虑到我在任何地方都没有 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
... [b autorelease];
不应该使应用程序崩溃吗?但该应用程序显然运行良好。
观察:我无意编写没有 NSAutoreleasePool 的应用程序,我只是想了解它是如何工作的。这个事实让我对我自以为知道的事情产生了怀疑。
I am sorry, I am new with cocoa programming and I am not sure if I really understand how nsautoreleasepool works.
Everywhere I read says something about the NSAutoreleasePool are responsable for all autorelease calls (talking about the last NSAutoreleasePool declared).
Considering the follow code:
int main(int argc, char *argv[]) {
//NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
//[pool release];
return retVal;
}
at some point of my application I would also have something close to this:
NSString* b = [[NSString alloc] initWithFormat:@"%d", 10];
[b autorelease];
Considering that I don't have NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
anywhere... shouldnt the [b autorelease];
crashs the application? But the application seems to work fine apparently.
Observation: I have no intention of writing an application without NSAutoreleasePool, I just want to get well the idea of how that works. This fact made doubt about what I thought I knew.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您只会在控制台中收到一条警告,指出没有自动释放池并且对象已泄漏。
如果您确实想了解自动释放池,请阅读 Mike Ash 的让我们构建 NSAutoreleasePool。
You'll simply get a warning in the console that there's no autorelease pool and that the object was leaked.
If you really want to understand autorelease pools, read Mike Ash's Let's build NSAutoreleasePool.
来自
的文档>NSAutoreleasePool
...这是非常不言自明的。您在控制台中是否收到与此相关的任何消息?
From the documentation for
NSAutoreleasePool
...That's pretty self explanatory. Are you getting any messages related to this logged in the console?
直接来自苹果文档:
Directly from apple documentation:
UIApplicationMain()
确保主线程运行事件循环,以便您的应用程序可以处理事件(例如用户输入、计时器事件等),并且事件循环在开始迭代和释放之前创建一个自动释放池一旦它完成了它。从主线程的偶循环调用的所有代码(例如所有 UI 代码)始终都有一个自动释放池,您无需执行任何操作。仅当使用与主线程不同的线程时才需要管理自动释放池(但仅当实际使用线程时!使用 DispatchQueues 或 OperationQueues 时,它们也会为您管理自动释放池),并且在某些罕见的情况下您想要使用它们围绕循环以保持内存占用较小(自动释放池收集对象以供以后释放,但有时您不希望它收集太多对象并因此变得太大,而是希望在您自己的循环的每次迭代中清除它,在那种情况您需要自己管理一个)。在大多数情况下,您不需要了解有关自动释放池的任何信息,您可以编写一个完整的、功能齐全的应用程序,而无需每个人都听说过自动释放池。您只需要知道何时必须对您拥有的对象调用autorelease
,这仅适用于非 ARC 代码,因为对于 ARC,编译器会自动为您决定。UIApplicationMain()
makes sure the main thread runs an event loop, so your app can process events (e.g. user input, timer events, etc.) and the event loop creates an autoreleasepool before starting its iteration and releases it once its done wit it. All code that is called from the even loop of the main thread (e.g. all UI code) always has an autoreleasepool in place, there is nothing you would have to do. You only need to manage the autoreleasepool when using a thread different than the main thread (but only when actually using a thread! When using DispatchQueues or OperationQueues, these will also manage the autoreleasepool for you) and in some rare cases you want to use them around loops to keep your memory footprint small (an autoreleasepool collects objects for later release but sometimes you don't want it to collect too many objects and thus become too big, instead you want to clear it on every iteration of your own loop, in that case you'd need to manage one yourself). In most cases you don't need to know anything about autoreleasepools, you can write an entire, full features app without every having heard of autoreleasepools. You only need to know when you must callautorelease
on an object you own and this only applies to non-ARC code as with ARC the compiler will decide that for you automatically.