使用 SDL 和 C++ 尝试 Blit 图像时出现分段错误

发布于 2024-07-09 11:48:01 字数 520 浏览 6 评论 0原文

好的 - 我这里有一个有趣的。 我正在开发俄罗斯方块克隆(基本上是为了“升级”我的技能)。 我试图重构我的代码,使其按照我想要的方式进行抽象。 虽然之前工作得很好,但现在在任何图像被位块传输之前我遇到了分割错误。 我已经尝试调试它没有成功。

我已在此处发布了该项目的 SVN 工作副本。

这只是一个小项目,比我有更多知识的人和一个好的调试器可能会很快就弄清楚。 唯一的依赖项是 SDL。 感谢能够告诉我我做错了什么的人。

编辑:据我所知,我现在所拥有的和以前所拥有的在逻辑上是相同的,所以我不认为我现在所拥有的会导致分段错误。 只需在工作副本上运行 svn revert,重新编译,您就可以看到它正在工作......

OK - I have an interesting one here. I'm working on a tetris clone (basically to "level-up" my skills). I was trying to refactor my code to get it abstracted the way I wanted it. While it was working just fine before, now I get a segmentation fault before any images can be blitted. I've tried debugging it to no avail.

I have posted my SVN working copy of the project here.

It's just a small project and someone with more knowledge than me and a good debugger will probably figure it out in a snap. The only dependency is SDL. Kudos to the person that can tell me what I'm doing wrong.

Edit: As far as I can tell, what I have now and what I had before are logically the same, so I wouldn't think that what I have now would cause a segmentation fault. Just run an svn revert on the working copy, recompile and you can see that it was working...

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

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

发布评论

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

评论(3

三生一梦 2024-07-16 11:48:01

查看 Surface.cpp 的第 15 至 18 行:

    surface = SDL_DisplayFormatAlpha( tempSurface );
    surface = tempSurface;
}
SDL_FreeSurface( tempSurface );

我认为它存在段错误,因为当您稍后使用此表面时,您实际上是在 tempSurface 上操作,因为这一行:

surface = tempSurface;

而不是 SDL_DisplayFormatAlpha() 返回的表面。 由于您释放了 tempSurface,表面现在指向无效内存。 要修复此问题,只需删除 else 块中的第二行即可。

Look at line 15 to 18 of Surface.cpp:

    surface = SDL_DisplayFormatAlpha( tempSurface );
    surface = tempSurface;
}
SDL_FreeSurface( tempSurface );

I assume it segfaults because when you use this surface later, you are actually operating on tempSurface because of this line:

surface = tempSurface;

and not the surface returned by SDL_DisplayFormatAlpha(). Since you free tempSurface, surface is now pointing to invalid memory. To fix, simply remove the second line in the else block.

赢得她心 2024-07-16 11:48:01

我的机器上没有安装SDL,但是在查看了代码之后。

我在 Output.cpp 文件中注意到这一点:

display = new Surface();

你什么都不做。 它的构造函数是空的。 (表面未初始化)。

然后在 Output::initalize() 中,您执行以下操作:

display->surface = SDL_SetVideoMode( 800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF );

这看起来像是问题 Surface::surface 从未真正初始化。 如果您还没有找到解决方案,当我回家时我会深入研究它。

I don't have SDL installed on my machine, but after looking through the code.

I noticed this in the Output.cpp file:

display = new Surface();

You do nothing. The constructor for this is empty. (surface is not initialized).

Then in Output::initalize() you do:

display->surface = SDL_SetVideoMode( 800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF );

This looks like the issue Surface::surface was never actually initialized. If you haven't found the solution, when i get home i'll digg into it.

蘸点软妹酱 2024-07-16 11:48:01

据我了解,当您尝试操作不再可用的ponter,或者尝试更改常量的值时,就会发生分段错误。

As far as I understand, a segmentation fault happens when you are trying to mnaipulate a ponter which is no longer available, or you are trying to change a constant's value.

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