Segmentation fault using SDL with C++, trying to Blit images

瘦欲@ 提交于 2019-12-06 13:01:40

问题


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...


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/341192/segmentation-fault-using-sdl-with-c-trying-to-blit-images

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!