仪器中的 NSAutoreleasePool 泄漏

发布于 2024-12-16 20:28:29 字数 266 浏览 4 评论 0原文

如果之前有人问过这个问题,我深表歉意,但我还没有找到好的答案。

当我运行 Instruments 时,我在代码中的这一行(而不是主代码中)发现了泄漏:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

我一直在环顾四周并在某处读到这可能是一个错误。有人可以帮助我并分享一些关于此的信息吗?

我确实注意到我在 main 中也有相同的名字“pool”。

I do apologize if this have been asked before but i have not found a good answer.

When i run the Instruments i get a leak on this line in the code, not in main:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

I have been looking around and read somewhere that it may be a bug. Could someone please help me and share some light on this?

I did notice that i have the same name, "pool", in main also.

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

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

发布评论

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

评论(2

影子是时光的心 2024-12-23 20:28:29

名字并不重要;整个代码库中可能有数百个名为 pool 的变量。重要的是范围。

通常,您会看到以下内容:

- someMethod
{
     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
     ... do stuff ...
     id something = [anObject retain];
     [pool drain];
     return [something autorelease];
 }

即您的泳池是否正在排空?

drain 存在于 iOS 上;请参阅 文档。但是,在所有情况下,最好使用 @autoreleasepool{ ... };它适用于 ARC 和MRR。)

The name doesn't matter; there may be hundreds of variables named pool throughout the codebase. What matters is scope.

Typically, you'll see this:

- someMethod
{
     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
     ... do stuff ...
     id something = [anObject retain];
     [pool drain];
     return [something autorelease];
 }

I.e. is your pool being drained?

(drain exists on iOS; see the documentation. However, in all cases, it is better to use @autoreleasepool{ ... }; it works in ARC and MRR.)

终弃我 2024-12-23 20:28:29

我也遇到了这个问题,这是我学习编码第一天的第一个问题!

我所做的就是彻底删除以以下内容开头的整行:

NSAutoreleasepool *pool = [[NSAutoreleasepool alloc] init];

将其替换为:

@autoreleasepool{

然后我在程序底部删除了:

[池释放];

然后用波浪括号封闭。

我不知道它为什么有效,我第一天记得,但它运行示例书中的程序很好。

I had this problem as well, my first problem on the first day learning how to code!

All I did was remove outright the entire line starting with:

NSAutoreleasepool *pool = [[NSAutoreleasepool alloc] init];

replace it with:

@autoreleasepool {

Then I deleted at the bottom of my program:

[pool release];

Then closed off with the squiggly brackets.

I don't know why it works, my first day remember but it runs my program from the example book fine.

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