我正在尝试创建一个显示图像大约2秒然后退出的程序,但问题是我在结束时遇到了分段错误(程序执行正常)。以下观察告诉我在调用close()
函数时堆栈溢出:
1)Valgrind说存在堆栈溢出。
2)close()
功能中的消息显示很多次,甚至在Loading Media...
消息之前
#include <SDL2/SDL.h>
#include <assert.h>
#include <stdio.h>
int init()
{
int success = 1;
if(SDL_Init(SDL_INIT_VIDEO) > 0) {
leave("Cannot initialize SDL.");
success = 0;
}
gWindow = SDL_CreateWindow("Show an image",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
640, 480, SDL_WINDOW_SHOWN);
if(gWindow == NULL) {
leave("Window could not be created.");
success = 0;
}
gScreenSurface = SDL_GetWindowSurface(gWindow);
return success;
}
int loadMedia() {
int success = 1;
gHelloWorld = SDL_LoadBMP("hello.bmp");
if(gHelloWorld == NULL) {
leave("Error while loading image");
success = 0;
}
return success;
}
void close()
{
printf("Releasing image...\n");
SDL_FreeSurface(gHelloWorld);
gHelloWorld = NULL;
printf("Released image.\n");
printf("Releasing Window...\n");
SDL_DestroyWindow(gWindow);
gWindow = NULL;
printf("Released Window\n");
printf("Shutting systems...\n");
SDL_Quit();
printf("Done.\n\n");
}
int main(int argc, char *argv[])
{
printf("Initializing...\n-----\n\n");
int rc = init();
assert(rc = 1 && "Error while initializing.");
printf("Loading Media...\n\n");
rc = loadMedia();
assert(rc = 1 && "Error while loading image.");
SDL_BlitSurface(gHelloWorld, NULL, gScreenSurface, NULL);
SDL_UpdateWindowSurface(gWindow);
SDL_Delay(2000);
printf("-------\nclosing...\n");
close();
return 0;
}
我认为我在某处覆盖了一个内部函数,因为当我更改函数名称时只显示错误或者直接插入代码而不是将其放在单独的函数中但是AFAIK所有SDL函数都以'SDL_'开头前缀,我在源代码中找不到任何close()
定义。
Valgrind输出:
==4420== Stack overflow in thread 1: can't grow stack to 0xffe801ff8
==4420==
==4420== Process terminating with default action of signal 11 (SIGSEGV)
==4420== Access not within mapped region at address 0xFFE801FF8
==4420== at 0x4E48A4A: ??? (in /usr/local/lib/libSDL2-2.0.so.0.2.1)
==4420== If you believe this happened as a result of a stack
==4420== overflow in your program's main thread (unlikely but
==4420== possible), you can try to increase the size of the
==4420== main thread stack using the --main-stacksize= flag.
==4420== The main thread stack size used in this run was 8388608.
==4420== Stack overflow in thread 1: can't grow stack to 0xffe801ff0
==4420==
==4420== Process terminating with default action of signal 11 (SIGSEGV)
==4420== Access not within mapped region at address 0xFFE801FF0
==4420== at 0x4A256A5: _vgnU_freeres (vg_preloaded.c:58)
==4420== If you believe this happened as a result of a stack
==4420== overflow in your program's main thread (unlikely but
==4420== possible), you can try to increase the size of the
==4420== main thread stack using the --main-stacksize= flag.
==4420== The main thread stack size used in this run was 8388608.
==4420==
==4420== HEAP SUMMARY:
==4420== in use at exit: 272,392 bytes in 1,013 blocks
==4420== total heap usage: 21,466 allocs, 107,735 frees, 46,031,777 bytes allocated
==4420==
==4420== LEAK SUMMARY:
==4420== definitely lost: 41,000 bytes in 8 blocks
==4420== indirectly lost: 176 bytes in 4 blocks
==4420== possibly lost: 8,211 bytes in 126 blocks
==4420== still reachable: 223,005 bytes in 875 blocks
==4420== suppressed: 0 bytes in 0 blocks
==4420== Rerun with --leak-check=full to see details of leaked memory
==4420==
==4420== For counts of detected and suppressed errors, rerun with: -v
==4420== ERROR SUMMARY: 1047384 errors from 24 contexts (suppressed: 43647 from 3)
Segmentation fault
答案 0 :(得分:1)
似乎我覆盖了close()
中可能被SDL内部函数使用的标准unistd.h
函数。更改功能名称会有所帮助。