gpt4 book ai didi

c - 未知的递归调用

转载 作者:行者123 更新时间:2023-11-30 15:33:01 25 4
gpt4 key购买 nike

我正在尝试创建一个程序,该程序显示图像大约 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;
}

我认为我在某个地方覆盖了内部函数,因为当我更改函数名称或直接插入代码而不是将其放入单独的函数中时,错误不会显示,但据我所知,所有 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

最佳答案

看来我重写了 unistd.h 中的标准 close() 函数,该函数可能由 SDL 的内部函数使用。更改函数的名称会有所帮助。

关于c - 未知的递归调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23857662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com