Multiple Windows and Utility Windows

Why destroy windows before exiting?

What happens if we don’t destroy a window before exiting the program?

Abstract art representing computer programming

Failing to destroy windows before exiting a program can lead to several problems, including resource leaks and undefined behavior. When you create a window with SDL_CreateWindow(), the SDL library allocates resources like memory, GPU buffers, and operating system handles.

These resources must be explicitly released by calling SDL_DestroyWindow() before the program terminates.

If a program exits without destroying its windows, the operating system may clean up the resources eventually, but this is not guaranteed to happen in a timely or consistent manner.

On some systems, lingering resources can cause performance issues, graphical glitches, or even crashes in other applications.

Here is an example of correctly destroying a window:

#include <SDL.h>

int main() {
  SDL_Init(SDL_INIT_VIDEO);
  SDL_Window* Window{
    SDL_CreateWindow("Example",
      100, 100, 640, 480, 0)
  };

  // ... Program logic ...

  SDL_DestroyWindow(Window); 
  SDL_Quit();
  return 0;
}

By explicitly calling SDL_DestroyWindow(), you ensure that all resources allocated for the window are properly released.

For larger applications managing multiple windows, tools like a WindowManager class can help automate this cleanup process within its destructor. Always clean up resources to maintain stability and avoid hard-to-diagnose issues.

This Question is from the Lesson:

Multiple Windows and Utility Windows

Learn how to manage multiple windows, and practical examples using utility windows.

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Multiple Windows and Utility Windows

Learn how to manage multiple windows, and practical examples using utility windows.

sdl2-promo.jpg
Part of the course:

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

Free, unlimited access

This course includes:

  • 67 Lessons
  • 100+ Code Samples
  • 91% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved