以下代码在我的macbook上运行正常,但在Linux上运行时会出现段错误:
gdb说它发生在这里(第81行):
mBackgroundTexture = SDL_CreateTextureFromSurface(mSDLRenderer, saveSurface);
GDB输出:
Thread 1 "Testapp.bin" received signal SIGSEGV, Segmentation fault.
_int_malloc (av=av@entry=0x7ffff6b9fb20 <main_arena>, bytes=bytes@entry=147479) at malloc.c:3728
3728 malloc.c: No such file or directory.
(gdb) bt
#0 _int_malloc (av=av@entry=0x7ffff6b9fb20 <main_arena>, bytes=bytes@entry=147479) at malloc.c:3728
#1 0x00007ffff685f5a4 in __GI___libc_malloc (bytes=147479) at malloc.c:2914
#2 0x00007fffedb126a9 in ?? () from /usr/lib/nvidia-367/libGLX_nvidia.so.0
#3 0x00007fffecb1eaf7 in ?? () from /usr/lib/nvidia-367/libnvidia-glcore.so.367.57
#4 0x00007fffecb0c30d in ?? () from /usr/lib/nvidia-367/libnvidia-glcore.so.367.57
#5 0x00007fffecbe40c4 in ?? () from /usr/lib/nvidia-367/libnvidia-glcore.so.367.57
#6 0x00007fffecbe5a0b in ?? () from /usr/lib/nvidia-367/libnvidia-glcore.so.367.57
#7 0x00007fffec871799 in ?? () from /usr/lib/nvidia-367/libnvidia-glcore.so.367.57
#8 0x00007fffec87b64c in ?? () from /usr/lib/nvidia-367/libnvidia-glcore.so.367.57
#9 0x00007fffec87f6b1 in ?? () from /usr/lib/nvidia-367/libnvidia-glcore.so.367.57
#10 0x00007fffec899127 in ?? () from /usr/lib/nvidia-367/libnvidia-glcore.so.367.57
#11 0x00007ffff7b22850 in ?? () from /usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0
#12 0x00007ffff7b1bbeb in ?? () from /usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0
#13 0x00007ffff7b1bf0b in ?? () from /usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0
#14 0x000000000043110a in MinimapRenderer::createBGTexture (this=0xc4f300, viewport=...)
at renderers/MinimapRenderer.cpp:81
代码:
void MinimapRenderer::createBGTexture(const Viewport &viewport)
{
SDL_Surface *sshot = SDL_CreateRGBSurface(0, viewport.getRect().w, viewport.getRect().h, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
if (sshot != NULL)
{
if (SDL_RenderReadPixels(mSDLRenderer, NULL, SDL_PIXELFORMAT_ARGB8888, sshot->pixels, sshot->pitch) == 0)
{
SDL_Surface *saveSurface = SDL_CreateRGBSurfaceFrom(sshot->pixels, viewport.getRect().w, viewport.getRect().h, 32, sshot->pitch, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
if (saveSurface != NULL)
{
mBackgroundTexture = SDL_CreateTextureFromSurface(mSDLRenderer, saveSurface);
if (mBackgroundTexture != NULL)
{
SDL_SaveBMP(saveSurface, "winning.bmp");
}
else
{
SDL_Log("MinimapRenderer::createBGTexture -- SDL_CreateTextureFromSurface error: %s \n", SDL_GetError());
}
SDL_FreeSurface(saveSurface);
}
else
{
SDL_Log("MinimapRenderer::createBGTexture -- SDL_CreateRGBSurfaceFrom error: %s \n", SDL_GetError());
}
}
else
{
SDL_Log("MinimapRenderer::createBGTexture -- SDL_RenderReadPixels error: %s \n", SDL_GetError());
}
SDL_FreeSurface(sshot);
}
else
{
SDL_Log("MinimapRenderer::createBGTexture -- SDL_CreateRGBSurface error: %s \n", SDL_GetError());
}
}
感谢任何想要进一步调试的想法,请告诉我是否有其他可以提供帮助的信息
答案 0 :(得分:1)
我能够跟踪问题SDL_RenderReadPixels,而不是传递一个NULL矩形传递一个rect似乎已经解决了问题。简化版本的代码无需错误检查即可运行,希望它可以帮助某人:
SDL_Rect rendRect = { 0, 0, viewport.getRect().w, viewport.getRect().h };
SDL_Surface *sshot = SDL_CreateRGBSurface(0, viewport.getRect().w, viewport.getRect().h, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
SDL_RenderReadPixels(mSDLRenderer, &rendRect, SDL_PIXELFORMAT_ARGB8888, sshot->pixels, sshot->pitch);
mBackgroundTexture = SDL_CreateTextureFromSurface(mSDLRenderer, sshot);
SDL_SaveBMP(sshot, "screenshot.bmp");
SDL_FreeSurface(sshot);