gpt4 book ai didi

c++ - 功能 SDL_FreeSurface : why it crashes in this context? 的行为

转载 作者:太空宇宙 更新时间:2023-11-04 07:48:40 25 4
gpt4 key购买 nike

我正在通过在线教程学习 SDL,发现一个我不明白的问题。根据本教程,始终正确释放应用程序使用的资源是一种很好的做法。就我而言,我想释放 SDL 表面使用的内存。

但是,如果我在调用该函数之前没有将它将要释放的指针设置为 nullptr,那么一个 SDL_FreeSurface 会使我的程序在退出时崩溃。

这不会发生在任何其他表面上。即使通过改变语句顺序来解决问题,我也不喜欢“ cargo 崇拜”。我想了解为什么在这种特定情况下我需要在释放内存之前使指针为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*/}

最佳答案

选定的表面被释放两次。

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

当您执行 SDL_FreeSurface(ptr_mascara)SDL_FreeSurface(l_lista_de_imagens[ctd]); 时,您将释放同一表面两次。

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

关于c++ - 功能 SDL_FreeSurface : why it crashes in this context? 的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55292378/

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