检查SDL_FreeSurface之前是否已释放表面

发布于 2024-12-11 03:08:55 字数 283 浏览 2 评论 0原文

当使用 SDL_Surface 处理图像时,我遇到了运行 SDL_FreeSurface(SDL_Surface *) (doc) 在同一指针上两次会产生分段错误。

我理解为什么会发生这种情况,但我需要知道如何避免这种情况。我想检查指针的状态(查明它是否指向现有表面),然后仅在必要时释放表面。

我怎样才能做到这一点?

When using SDL_Surfaces to handle images, I run into the problem that running SDL_FreeSurface(SDL_Surface *) (doc) twice on the same pointer yields a segmentation fault.

I understand why that happens, but I need to know how I can avoid that. I'd like to check the state of the pointer (find out if its pointing to an existing surface) and then Free the surface only if necessary.

How can I do that?

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

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

发布评论

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

评论(1

oО清风挽发oО 2024-12-18 03:08:55

您编写一个封装 SDL_Surface 指针的类,该指针在其析构函数中释放表面。另外,请确保正确实现或禁用复制构造函数和赋值运算符。 (请参阅3的规则)然后,您永远不会直接使用SDL_Surface指针再次。

这是我不久前正在开发的 SDL 包装器示例。

我当我了解到 SFML 后,我就停止了工作。

You write a class that encapsulates an SDL_Surface pointer which frees the surface in it's destructor. Also, make sure you properly implement or disable the copy constructor and the assignment operator. (see The rule of 3)Then, you never work directly with SDL_Surface pointers again.

Here's an example of a wrapper around SDL that I was working on a while ago.

I stopped work once I learned about SFML.

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