如果没有声明 nsautoreleasepool,自动释放调用是否会崩溃?

发布于 2024-12-25 16:04:37 字数 747 浏览 6 评论 0原文

抱歉,我是可可编程的新手,我不确定我是否真正理解 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 技术交流群。

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

发布评论

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

评论(4

夏雨凉 2025-01-01 16:04:38

您只会在控制台中收到一条警告,指出没有自动释放池并且对象已泄漏。

如果您确实想了解自动释放池,请阅读 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.

蛮可爱 2025-01-01 16:04:38

来自 的文档>NSAutoreleasePool...

在引用计数环境中,Cocoa 期望有一个
自动释放池始终可用。如果没有泳池,
自动释放的对象不会被释放,并且会泄漏内存。在这个
在这种情况下,您的程序通常会记录适当的警告消息。

这是非常不言自明的。您在控制台中是否收到与此相关的任何消息?

From the documentation for NSAutoreleasePool...

In a reference counted environment, Cocoa expects there to be an
autorelease pool always available. If a pool is not available,
autoreleased objects do not get released and you leak memory. In this
situation, your program will typically log suitable warning messages.

That's pretty self explanatory. Are you getting any messages related to this logged in the console?

虐人心 2025-01-01 16:04:38

直接来自苹果文档:

NSApplication 类设置自动释放池(
NSAutoreleasePool 类)在初始化期间和事件内部
循环——具体来说,在其初始化(或共享应用程序)内
和运行方法。同样,应用程序工具包添加的方法
NSBundle 在加载 nib 文件期间使用自动释放池。
这些自动释放池在范围之外不可访问
各自的 NSApplication 和 NSBundle 方法。通常,一个
应用程序在事件循环运行时创建对象,或者
通过从 ni​​b 文件加载对象,因此通常缺乏访问权限
不是问题。但是,如果您确实需要在其中使用 Cocoa 类
main() 函数本身(除了加载 nib 文件或
实例化 NSApplication),您应该创建一个自动释放池
在使用类之前,然后在完成后释放池。
有关详细信息,请参阅 基金会中的 NSAutoreleasePool
框架参考
.

Directly from apple documentation:

the NSApplication class sets up autorelease pools (instances of the
NSAutoreleasePool class) during initialization and inside the event
loop—specifically, within its initialization (or sharedApplication)
and run methods. Similarly, the methods the Application Kit adds to
NSBundle employ autorelease pools during the loading of nib files.
These autorelease pools aren’t accessible outside the scope of the
respective NSApplication and NSBundle methods. Typically, an
application creates objects either while the event loop is running or
by loading objects from nib files, so this lack of access usually
isn’t a problem. However, if you do need to use Cocoa classes within
the main() function itself (other than to load nib files or to
instantiate NSApplication), you should create an autorelease pool
before using the classes and then release the pool when you’re done.
For more information, see NSAutoreleasePool in the Foundation
Framework Reference
.

﹎☆浅夏丿初晴 2025-01-01 16:04:38

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 call autorelease on an object you own and this only applies to non-ARC code as with ARC the compiler will decide that for you automatically.

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