函数SDL_FreeSurface的行为:为什么在这种情况下会崩溃?

时间:2019-03-22 03:12:15

标签: c++ c sdl

我正在通过在线教程学习SDL,但发现了我不理解的问题。 根据本教程,优良作法是始终正确释放应用程序使用的资源。就我而言,我想释放SDL表面使用的内存。

但是,如果我没有设置指针,那么SDL_FreeSurface会在退出前使程序崩溃,从而导致在调用函数之前将其释放给nullptr

在任何其他表面上都不会发生这种情况。即使通过更改语句的顺序解决问题,我也不喜欢“货物崇拜”。我想了解为什么,在这种特定情况下,我需要使指针nullptr 之前释放内存。

这是代码:

#define SDL_MAIN_HANDLED
#include<SDL2/SDL.h>

int main (void) {SDL_SetMainReady();

    SDL_Init(SDL_INIT_VIDEO);

    enum image_list{ /* set enum */ };

    SDL_Window  * ptr_Window = nullptr;
    SDL_Surface * ptr_Canvas = nullptr;
    SDL_Surface * ptr_Mask   = nullptr;     // this is the pointer with problem
    SDL_Surface * array_list_of_images[enum_Total]; /* each array initialized correctly */

    ptr_Mask = array_list_of_images[enum_Image_Default]; // here the problematic pointer is set to array[0]

    /* code */

    // Here is where the pointer is used:
    switch (union_events_holder.key.keysym.sym){
        case SDLK_UP    : ptr_Mask = array_list_of_images[enum_Image_Up]; break;
        case SDLK_DOWN  : ptr_Mask = array_list_of_images[enum_Image_Down]; break;
        case SDLK_LEFT  : ptr_Mask = array_list_of_images[enum_Image_Left]; break;
        case SDLK_RIGHT : ptr_Mask = array_list_of_images[enum_Image_Right]; break;
        default         : ptr_Mask = array_list_of_images[enum_Image_Default]; break;

    /* more code */

    SDL_BlitSurface(ptr_Mask , NULL , ptr_Canvas , NULL );
    SDL_UpdateWindowSurface(ptr_Window);

    /* more code */

    // HERE OCCURS THE PROBLEM.
    // WITHOUT POINTING ptr_Mask TO NULL BEFORE CALLING SDL_FreeSurface,
    // THE APP CRASHES.

    ptr_Mask    = nullptr;
    SDL_FreeSurface(ptr_Mask); // This cannot be called before previous statement. Why?

    for ( int ctd = 0 ; ctd < enum_Total ; ++ctd){
        SDL_FreeSurface(array_list_of_images[ctd]);
        array_list_of_images[ctd] = nullptr; // Here it is safe to set pointer to nullptr AFTER freeing memory.
    }   

    SDL_FreeSurface(ptr_Canvas);   
    SDL_DestroyWindow(ptr_Window);

    ptr_Canvas       = nullptr; // Here also it is safe to set pointer to nullptr AFTER freeing memory.
    ptr_Window       = nullptr;

    SDL_Quit();

return (0);}

注意:

  • 由于SDL而被标记为C。我正在用C ++进行编码。

  • 通过std::cerr + std::cin.get()

  • 找到了逐步进行操作的错误
  • SDL_GetError()无法输出信息,因为调用该函数时,Window和控制台都会崩溃。

  • 我怀疑存在取消引用nullptr的不确定行为,但是一旦该功能仅在我设置为nullptr时才能正常工作,这是没有意义的。

编辑-这是完整代码(巴西葡萄牙语中的变量)

#define SDL_MAIN_HANDLED
#include<SDL2/SDL.h>

int main (void) {SDL_SetMainReady();

    SDL_Init(SDL_INIT_VIDEO);

    SDL_Window  * ptr_Janela = nullptr;
    SDL_Surface * ptr_Tela   = nullptr;

    enum lista_de_imagens{
        LIMG_Imagem_Default, 
        LIMG_Imagem_Cima,
        LIMG_Imagem_Baixo,
        LIMG_Imagem_Esquerda,
        LIMG_Imagem_Direita,
        LIMG_Total_de_imagens
    };

    SDL_Surface * l_lista_de_imagens[LIMG_Total_de_imagens]; 

        l_lista_de_imagens[LIMG_Imagem_Default]   = SDL_LoadBMP("Lesson4/Default.bmp" );
        l_lista_de_imagens[LIMG_Imagem_Cima]      = SDL_LoadBMP("Lesson4/Cima.bmp"    );
        l_lista_de_imagens[LIMG_Imagem_Baixo]     = SDL_LoadBMP("Lesson4/Baixo.bmp"   );
        l_lista_de_imagens[LIMG_Imagem_Esquerda]  = SDL_LoadBMP("Lesson4/Esquerda.bmp");
        l_lista_de_imagens[LIMG_Imagem_Direita]   = SDL_LoadBMP("Lesson4/Direita.bmp" );
        l_lista_de_imagens[LIMG_Total_de_imagens ]= nullptr;

    SDL_Surface * ptr_mascara = nullptr;

    ptr_Janela = SDL_CreateWindow("Lesson 4 - Show image as keyboard inputs",
                                SDL_WINDOWPOS_UNDEFINED,
                                SDL_WINDOWPOS_UNDEFINED,
                                640, //SCREEN_WIDTH,
                                480, //SCREEN_HEIGHT,
                                SDL_WINDOW_SHOWN);
    ptr_Tela = SDL_GetWindowSurface(ptr_Janela);

    SDL_BlitSurface(l_lista_de_imagens[LIMG_Imagem_Default] , NULL , ptr_Tela , NULL );
    SDL_UpdateWindowSurface(ptr_Janela);

    SDL_Event u_Gerenciador_de_eventos;
    bool sair = false;

    ptr_mascara = l_lista_de_imagens[LIMG_Imagem_Default];

    while(!sair){

            while(SDL_PollEvent(&u_Gerenciador_de_eventos) !=0){

                if(u_Gerenciador_de_eventos.type == SDL_QUIT){sair = true;}
                if( u_Gerenciador_de_eventos.type == SDL_KEYDOWN){

                    switch (u_Gerenciador_de_eventos.key.keysym.sym){

                        case SDLK_UP    : ptr_mascara = l_lista_de_imagens[LIMG_Imagem_Cima]; break;

                        case SDLK_DOWN  : ptr_mascara = l_lista_de_imagens[LIMG_Imagem_Baixo]; break;

                        case SDLK_LEFT  : ptr_mascara = l_lista_de_imagens[LIMG_Imagem_Esquerda]; break;

                        case SDLK_RIGHT : ptr_mascara = l_lista_de_imagens[LIMG_Imagem_Direita]; break;

                        default         : ptr_mascara = l_lista_de_imagens[LIMG_Imagem_Default]; break;

                    /*end switch*/}

                /*end if*/}

        /*end laço de eventos*/}

    SDL_BlitSurface(ptr_mascara , NULL , ptr_Tela , NULL );
    SDL_UpdateWindowSurface(ptr_Janela);

    /*end laço principal*/}

    ptr_mascara    = nullptr;
    SDL_FreeSurface(ptr_mascara); 

    for ( int ctd = 0 ; ctd < LIMG_Total_de_imagens ; ++ctd){
        SDL_FreeSurface(l_lista_de_imagens[ctd]);
        l_lista_de_imagens[ctd] = nullptr;
    /*end for*/}    

    SDL_FreeSurface(ptr_Tela);   
    SDL_DestroyWindow(ptr_Janela);

    ptr_Tela       = nullptr;
    ptr_Janela     = nullptr;

    SDL_Quit();

return (0);
/*end main*/}

1 个答案:

答案 0 :(得分:2)

选定的表面被释放两次。

您有两个指向同一图像的指针。第一个在l_lista_de_imagens[LIMG_Imagem_Default]中,第二个在ptr_mascara中。

先执行SDL_FreeSurface(ptr_mascara)然后执行SDL_FreeSurface(l_lista_de_imagens[ctd]);,两次释放相同的曲面。

SDL_FreeSurface(nullptr)不会执行任何操作,因此,在ptr_mascara = nullptr;之前添加SDL_FreeSurface(ptr_mascara)时,只释放了一次曲面。