Objective-C 内存管理奇怪的结果

发布于 2024-08-19 20:29:13 字数 729 浏览 7 评论 0原文

我使用循环重复调用函数,并且循环在线程内运行。该线程有一个自动释放池。

我在该函数中有以下代码:

NSXMLDocument* undoXML;
NSData* undoData = [NSData dataWithContentsOfFile:undoFilePath];
undoXML = [[NSXMLDocument alloc] initWithData:undoData 选项:NSXMLDocumentTidyXML 错误:&err];

NSData* undoData2;
undoData2 = [undoXML XMLData];

[undoData2 发布];
[undoXML 发布];

我得到以下奇怪的结果:

  • 每次循环调用此函数时,我的程序都会泄漏内存。
  • 当我将以下代码添加到函数中时:

NSData* undoData3;
undoData3 = [undoXML XMLData];
[undoData3释放];

我的程序泄漏的内存比以前更多。

我真的很困惑,我非常需要帮助弄清楚发生了什么事。也许我的自动释放池无法正常工作?为什么会发生这种情况?

I am calling a function repeatedly with a loop, and the loop runs inside of a thread. The thread has an autorelease pool.

I have the following code inside that function:

NSXMLDocument* undoXML;
NSData* undoData = [NSData dataWithContentsOfFile:undoFilePath];
undoXML = [[NSXMLDocument alloc] initWithData:undoData options:NSXMLDocumentTidyXML error:&err];

NSData* undoData2;
undoData2 = [undoXML XMLData];

[undoData2 release];
[undoXML release];

I'm getting the following strange results:

  • My program is leaking memory every time this function is called by the loop.
  • When I add the following code to the function:

NSData* undoData3;
undoData3 = [undoXML XMLData];
[undoData3 release];

My program leaks even more memory than before.

I'm really confused and I badly need help figuring out what's going on. Maybe my autorelease pool isn't working correctly? Why is this happening?

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

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

发布评论

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

评论(4

维持三分热 2024-08-26 20:29:13

你确定它漏水了吗?或者只是尺寸变大了?

您的循环是什么样的?自动释放池是如何集成到其中的?

自动释放池必须位于循环内部,否则循环将随着时间的推移积累大量内存。泄漏工具没有显示泄漏表明您违反了内存管理规则或者您的循环不正确。

Are you sure it is leaking? Or is it simply growing in size?

What does your loop look like and how is the autorelease pool integrated into it?

The autorelease pool must be inside the loop or your loop will just build up tons of memory over time. That the leaks instrument doesn't show leaks indicates that you have violated the memory management rules or your loop is incorrect.

怀念你的温柔 2024-08-26 20:29:13

尝试在您的项目上运行带有泄漏检测设置的 Instruments。这应该准确识别泄漏发生的位置(甚至在系统库中)。

运行->使用性能工具运行 ->泄漏

Try running Instruments on your project w/ the leak detection settings. This should identify exactly where your leak is occurring (even in the system libraries).

Run -> Run With Performance Tool -> Leaks

夏九 2024-08-26 20:29:13

undoData 应预设为自动释放(根据命名约定 dataWithContentsOfFile: 返回一个自动释放对象)。但除非您有自己的自动释放池,否则在活动池耗尽之前(即函数返回后的某个时间),设置为自动释放的任何内容都不会真正被释放。

您的线程可能有自己的自动释放池,但除非您在自己的函数内创建自动释放池,否则在函数退出之前不会释放任何内容。

如果您想在函数中间触发自动释放对象的耗尽(例如每个循环一次),您需要管理自己的自动释放池。

while(looping) {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    // do stuff that produces autoreleased objects

    [pool drain];
}

另外,根据 cobbal 对您问题的评论,看来您不应该发布 undoData2(或undoData3)。根据命名约定 -[NSXMLDocument XMLData] 应该返回一个自动释放的对象。

undoData should be preset to be autoreleased (according to naming convention dataWithContentsOfFile: returns an autoreleased object). But unless you have your own autorelease pool, nothing that is set to autorelease will actually be deallocated until the active pool is drained (i.e. sometime after your function returns).

Your thread may have its own autorelease pool, but unless you are creating one inside your own function, nothing will be deallocated until after your function exits.

If you want to trigger the draining of autoreleased objects in the middle of a function (say once per loop), you need to manage your own autorelease pool.

while(looping) {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    // do stuff that produces autoreleased objects

    [pool drain];
}

Also, per cobbal's comment on your question, it looks like you should not be releasing undoData2 (or undoData3). Based on the naming convention -[NSXMLDocument XMLData] should be returning an autoreleased object.

救赎№ 2024-08-26 20:29:13

如果您有权访问 NSData 类的源代码,您应该查看创建 undoData3 时实例化了哪些对象。我这样说是因为你创建了对象并立即销毁它。问题肯定是内存是在类内部分配的,但没有在其析构函数中释放。

If you have access to the source code of the NSData class you should look at what objects are being instantiated when undoData3 is created. I say this because you create the object and immediately destroy it. The issue must be that memory is being allocated inside the class but not being deallocated in it's destructor.

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