Search code examples
c++sdl

Why does SDL doesn't emit SDL_QUIT when last windows closed


I'm writing a Game Engine just for practice but I still stuck with the first chellange. The Window Manager.

https://github.com/thebenius/SDL

I've created a GitHub Repo to show you the code but don't worry. Its not much. But I absolutely don't know where is my mistake.

In the Code I create three Windows and I Manage the input for SDL_QUIT to stop the game loop and for SDL_WINDOWEVENT_CLOSE to close the windows. Everything works fine until the last window is closed. As far as I know, now the SDL_QUIT Event must be emitted by SDL. But the Gameloop goes on.

I think I maybe have a kind of memory leak and there is still a windows saved. But I checked the window stack (Window::windows hashmap) it is empty. And also the variables in main are cleared.

I also tried to additionally clear the window and renderer variable in the hash map

Window::~Window() {

// Delete Window and Renderer
SDL_DestroyRenderer(Window::windows[this->windowID]->renderer);
SDL_DestroyWindow(Window::windows[this->windowID]->window);

Window::windows[this->windowID]->renderer = nullptr;
Window::windows[this->windowID]->window = nullptr;

// Delete Window from map
Window::windows.erase(this->windowID);

// Delete Window and Renderer
SDL_DestroyRenderer(this->renderer);
SDL_DestroyWindow(this->window);

// Reset Pointer
this->renderer = nullptr;
this->window = nullptr;

Nothing worked.

I'm new in C++ and SDL. I hope you can help me out.


Solution

  • Thank you o11c,

    Your answer was the riddles solution. I just put SDL_Quit() out of the Destructor. This obviously blocked the Event Handler to catch SDL_QUIT. So I put it to the constructor in atexit()

    After that (don't know why before not) I got an Segfault when deleting the window pointer in main. I deleted that and just set them all to nullptr.

    Now the WindowManager works properly. Thank you for your help