SDL + OSX 上的 OpenGL:NSAutoreleaseNoPool()

发布于 2024-10-18 01:45:16 字数 1257 浏览 2 评论 0原文

我正在使用 OpenGL 和 SDL 编写一些跨平台代码,但在 Mac OSX 上立即遇到了问题。

作为参考,这里是 GitHub 上的代码库: https://github.com/GrooveStomp/platformer

我已经还将我看到的错误推送到同一个存储库:https://github。 com/GrooveStomp/platformer/blob/master/errors.txt

现在,从我读过的内容来看,SDL 似乎只是简单地封装了 Mac OSX 的 Objective-C Cocoa 层,我需要声明我自己的 NSAutoreleasePool来包装我的整个程序。这是正确的吗?

我刚刚发现这个链接: http://sourceforge .net/apps/wordpress/paintown/2010/12/26/sdl-and-osx/,其中作者从源代码安装并且没有任何问题。我使用 Homebrew 安装,我认为这相当于作者的步骤 #3,因为我在构建时必须指定“-framework OpenGL”。

[编辑]

因此,事实证明 NSAutoreleaseNoPool() 问题应该通过以下三个步骤来解决:

  1. main() 应该具有以下签名: int main(int argc, char * argv[])
  2. #include在 main() 所在的源文件中。
  3. 与 -lSDLmain 链接

结果是 SDL 将用 NSAutoreleasePool 包装它自己的 main() 并围绕我的 main.但是,当我这样做时,我收到此处显示的错误:

https://github .com/GrooveStomp/platformer/blob/master/make_errors.txt

I'm working on some cross-platform code using OpenGL and SDL, but have immediately run into issues on Mac OSX.

For reference, here's the codebase on GitHub: https://github.com/GrooveStomp/platformer

I've also pushed the errors I'm seeing to that same repo: https://github.com/GrooveStomp/platformer/blob/master/errors.txt

Now, from the reading I've done, It seems as though SDL simply wraps around Mac OSX's Objective-C Cocoa layer and I need to declare my own NSAutoreleasePool to wrap my entire program. Is that correct?

I just came across this link: http://sourceforge.net/apps/wordpress/paintown/2010/12/26/sdl-and-osx/ in which the author installs from source and has no issues. I installed using Homebrew, which I assume is equivalent to the author's step #3, as I have to specify "-framework OpenGL" when building.

[EDIT]

So, it turns out that the NSAutoreleaseNoPool() issue should be taken care of by following these three steps:

  1. main() should have this signature: int main(int argc, char * argv[])
  2. #include <SDL.h> in the source file where main() is.
  3. link with -lSDLmain

The result is that SDL will wrap it's own main() with NSAutoreleasePool and all around my main. However, when I do this, I get the errors shown here:

https://github.com/GrooveStomp/platformer/blob/master/make_errors.txt

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

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

发布评论

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

评论(1

心的位置 2024-10-25 01:45:16

当您在没有 NSAutoreleasePool 的线程上发送 Objective-C 消息时,您会收到有关内存“Just Leaking”的消息。将整个程序包装在自动释放池中的问题是它永远不会被耗尽,并且您可能会面临内存不足的风险。相反,在事件循环开始时创建池,并在每次迭代结束时耗尽。这样做是正确的,应该可以解决所有“Just Leaking”错误。

由于 NSAutorelasePool 对象不是普通对象,因此它们的正确使用与几乎所有其他 Cocoa 对象有点不同。这是一个示例:


while(1)
{
    NSAutoreleasePool* pool = [NSAutoreleasePool new];

    // Do your event processing

    [pool drain];
}

You get the message about memory "Just Leaking" when you send an Objective-C message on a thread without an NSAutoreleasePool in place. The problem with wrapping the whole program in the autorelease pool is it never gets drained and you risk running out of memory. Instead create the pool at the beginning of an event loop and drain at the end of each iteration. Doing this is the correct place should fix all of the "Just Leaking" errors.

Since NSAutorelasePool objects aren't normal objects, their proper use is a bit different from almost all other Cocoa objects. Here's a sample:


while(1)
{
    NSAutoreleasePool* pool = [NSAutoreleasePool new];

    // Do your event processing

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