检查SDL_FreeSurface之前是否已释放表面
当使用 SDL_Surface
处理图像时,我遇到了运行 SDL_FreeSurface(SDL_Surface *)
(doc) 在同一指针上两次会产生分段错误。
我理解为什么会发生这种情况,但我需要知道如何避免这种情况。我想检查指针的状态(查明它是否指向现有表面),然后仅在必要时释放表面。
我怎样才能做到这一点?
When using SDL_Surface
s 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您编写一个封装 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.