NSAutoreleaseNoPool 在进行 NSLog - Mac 应用程序开发时

发布于 2024-10-18 07:12:18 字数 552 浏览 2 评论 0原文

我正在 Xcode 中尝试一个程序,并且创建了一个新的 Mac Cocoa 应用程序。我有一个名为 Photo 的类,它有两个实例变量 Caption、摄影师。在主函数中,我按如下方式向它们提供值。

Photo *obj = [[Photo alloc]init];
obj.caption=@"Something";
obj.photographer=@"Hari";
NSLog(@"Name: '%@'",[obj caption]);
[obj release];

我得到名为 Name: 'Something' 的输出,但同时我得到了这一行

2011-02-22 11:56:03 test_1[1402:a0f] * __NSAutoreleaseNoPool(): Object 0x100002078 of class NSCFString autoreleased with no pool in place - just leaking

有人可以解释一下为什么这一行出现在控制台中吗?

谢谢,

哈里哈兰

I am trying out a program in Xcode, and I have created a new Mac Cocoa Application. I have a class called Photo with two instance variables Caption, Photographer. In the main function, I supply values to them as follows.

Photo *obj = [[Photo alloc]init];
obj.caption=@"Something";
obj.photographer=@"Hari";
NSLog(@"Name: '%@'",[obj caption]);
[obj release];

I get the output called Name: 'Something', but along with that I get this line

2011-02-22 11:56:03 test_1[1402:a0f] * __NSAutoreleaseNoPool(): Object 0x100002078 of class NSCFString autoreleased with no pool in place - just leaking

Can someone please explain why this line is appearing in the console?

Thanks,

Hariharan

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

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

发布评论

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

评论(1

感悟人生的甜 2024-10-25 07:12:18

Cocoa 使用自动释放池 。自动释放池保留对已自动释放的对象的引用,当池耗尽时,它会向这些自动释放的对象发送一条 -release 消息。

在绝大多数情况下,Cocoa 应用程序应该至少有一个自动释放池,因为 Cocoa 大量使用自动释放对象。当 Cocoa 应用程序运行时,Cocoa 会自动为应用程序创建自动释放池。

就您而言,您的应用程序似乎是一个 Foundation 程序,而不是一个完整的 Cocoa 应用程序。因此,您有责任为您的程序设置自动释放池。例如,

int main() {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    Photo *obj = [[Photo alloc] init];
    // …

    [pool drain];
    return 0;
}

请注意,自动释放池并不是绝对必要的。如果没有自动释放池,您的程序将输出该警告,并且根据程序的具体情况,您可能会泄漏对象。由于内存泄漏在程序中是一个不好的功能,特别是在长期存在的程序或分配大量内存的程序中,因此建议始终设置自动释放池。

也就是说,您自己在那段代码中没有发送 -autorelease ,那么为什么会有一个自动释放的字符串呢?我假设您已经合成了 caption 属性,并且编译器已创建一个访问器方法,该方法返回包含照片标题的自动释放字符串。当您发送 [obj title] 时,您会收到一个自动释放的字符串,并因此收到一条警告,因为您尚未设置自动释放池。

Cocoa makes use of autorelease pools. An autorelease pool keeps references to objects that have been autoreleased and, when the pool is drained, it sends a -release message to those autoreleased objects.

In the vast majority of cases, Cocoa applications should have at least one autorelease pool in place because Cocoa makes heavy use of autoreleased objects. When a Cocoa application is run, Cocoa automatically creates autorelease pools for the application.

In your case, it seems that your application is a Foundation program instead of a full blown Cocoa application. As such, it is your responsibility to set up an autorelease pool for your program. For instance,

int main() {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    Photo *obj = [[Photo alloc] init];
    // …

    [pool drain];
    return 0;
}

Note that the autorelease pool is not strictly necessary. If there’s no autorelease pool, your program will output that warning and, depending on the specifics of your program, you might leak objects. As leaking memory is a bad feature in a program, especially in long-lived programs or programs that allocate a lot of memory, it is advisable to always set up an autorelease pool.

That said, you haven’t sent -autorelease yourself in that piece of code, so why is there an autoreleased string? I’m assuming you’ve synthesised the caption property, and the compiler has created an accessor method that returns an autoreleased string containing the photo caption. When you send [obj caption], you get an autoreleased string, and consequently a warning since you haven’t set up an autorelease pool.

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