分段故障使用SDL与C ++,试图Blit图像 [英] Segmentation fault using SDL with C++, trying to Blit images
问题描述
确定 - 我在这里有一个有趣的。我正在做一个tetris克隆(基本上是提高我的技能)。我试图重构我的代码,让它以我想要的方式抽象。虽然它以前工作很好,现在我得到一个分割错误,任何图像可以blitted。我尝试调试它没有效果。
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.
我已经发布了我的SVN工作副本的项目这里。
I have posted my SVN working copy of the project here.
这只是一个小项目,比我和一个好的调试器可能会搞清楚。唯一的依赖关系是 SDL 。 Kudos给可以告诉我我做错了的人。
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.
编辑:据我所知,我现在和我以前的是逻辑相同,所以我不会认为我现在会导致分段错误。只需在工作副本上运行svn revert,重新编译,你可以看到它正在工作...
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...
推荐答案
Surface.cpp的15到18:
Look at line 15 to 18 of Surface.cpp:
surface = SDL_DisplayFormatAlpha( tempSurface );
surface = tempSurface;
}
SDL_FreeSurface( tempSurface );
我假设它是segfaults,因为当你以后使用这个表面时,你实际上是操作tempSurface行:
I assume it segfaults because when you use this surface later, you are actually operating on tempSurface because of this line:
surface = tempSurface;
,而不是由SDL_DisplayFormatAlpha因为你自由tempSurface,surface现在指向无效的内存。要修复,只需删除else块中的第二行。
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.
这篇关于分段故障使用SDL与C ++,试图Blit图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!