SDL2 SIGSEGV从曲面创建纹理

SDL2 SIGSEGV in creating texture from surface

本文关键字:创建 纹理 曲面 SIGSEGV SDL2      更新时间:2023-10-16

我正在尝试使用SDL_CreateTextureFromSurface创建SDL_Texture。我已经多次成功地实现了这个功能,没有出现任何问题。目前我得到以下回溯信息:

#0  0xb7ef1e80 in ?? () from /usr/lib/libSDL2-2.0.so.0
#1  0xb7edf19c in ?? () from /usr/lib/libSDL2-2.0.so.0
#2  0xb7f12e1d in ?? () from /usr/lib/libSDL2-2.0.so.0
#3  0xb7f13ee7 in ?? () from /usr/lib/libSDL2-2.0.so.0
#4  0xb7eb2c08 in ?? () from /usr/lib/libSDL2-2.0.so.0
#5  0xb7e9f474 in SDL_CreateTextureFromSurface () from /usr/lib/libSDL2-2.0.so.0
#6  0x0805999e in Building::draw (this=0xbfffe9a8, Render=@0xbffff6d8: 0x81bd1b8)
    at /code/directory/LIBRARY.h:194
#7  0x08063df7 in main (argc=1, args=0xbffffac4) at Program.cpp:1151

我的其余代码和这个特定实现之间唯一不同的是,我的SDL_Surface是一个指针数组,定义如下:

//<Initialize window and renderer (not included here)>
SDL_Surface** Surf;
SDL_Surface* SomeOtherSurface;
int SurfSize = 0;
SomeOtherSurface = LoadBMP("Filename.bmp");
for (i = 0; i < 2; i++)
{
    Surf = (SDL_Surface**) realloc(Surf,SurfSize+1)*sizeof(SDL_Surface*));
    SDL_LockSurface(SomeOtherSurface);
    Surf[i] = SDL_CreateRGBSurfaceFrom(SomeOtherSurface->pixels,SomeOtherSurface->w,SomeOtherSurface->h,32,SomeOtherSurface->pitch,0xFF000000,0x00FF0000,0x0000FF00,0x000000FF); //Create a surface in Surf based on SomeOtherSurface
    SDL_UnlockSurface(SomeOtherSurface);
    SurfSize++;
}
SDL_FreeSurface(SomeOtherSurface);
SDL_Texture* Tex1;
Tex1 = SDL_CreateTextureFromSurface(Render,Surf[0]); //Create the texture; this throws the SIGSEGV

我在程序的其他地方写过类似的代码,它没有失败;有人能弄清楚这里出了什么问题吗?这个错误似乎表明内存有问题,但如果我尝试访问Surf[0]->w,它会按预期返回值。

编辑:一些额外的信息可以帮助解决这个问题:在我的代码中,我在定义纹理之前释放了"SomeOtherSurface"曲面。如果在释放曲面之前定义纹理,则一切都可以正常工作。这两个表面是否可能共享像素数据地址?

解决:我需要使用SDL_BlitSurface来复制数据并对其进行修改。如果我在使用SDL_CreateRGBSurfaceFrom后继续释放曲面,则当程序试图访问该内存地址时,现有的像素数据也会被删除,从而导致segfault。

此代码的问题是SDL_CreateRGBSurfaceFrom使用现有像素数据;因此,当您调用SDL_FreeSurface时,像素数据会丢失。现在,Surf[i]在调用SDL_CreateTextureFromSurface时尝试查找像素数据,但内存不可访问。

解决方案:使用SDL_BlitSurface制作像素数据的副本以供进一步使用。