2014-05-25 6 views
0

약 2 초 동안 이미지를 표시 한 다음 종료하는 프로그램을 만들려고하지만 끝에 문제가 발생하면 프로그램이 잘 실행됩니다. 다음 관찰 결과는 close() 함수 호출로 스택 오버플로가 발생 함을 나타냅니다.알 수없는 재귀 호출

1) Valgrind에 스택 오버플로가 있다고합니다. 내가 변경할 때 오류가 표시되지 않기 때문에 close() 기능에

2) 메시지도

#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; 
} 

내가 어디 선가 내부 기능을 무시하고 생각 Loading Media... 님에게 메일하기 전에, 시간을 많이 표시 함수 이름을 삽입하거나 코드를 별도의 함수에 넣는 대신 코드를 직접 삽입하지만 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 
+5

'close()'는 파일 기술자에서 작동하는 표준 c 함수입니다. 그것은'unistd.h '에 정의되어있다. 함수를 다른 것으로 변경하고자 할 수 있습니다. – alvits

+0

@OliCharlesworth 죄송합니다. 그것을 수정했습니다 – Apoorv

+0

@alvits하지만 unistd.h를 포함하지 않았습니다. 여전히 표준 함수에 영향을 미칩니 까? – Apoorv

답변

1

내가 아마 SDL의 내부 기능에 의해 사용되는 unistd.h의 표준 close() 기능을 오버라이드 (override) 된 것으로 보인다. 함수의 이름을 변경하면 도움이됩니다.