Xcode launches the program twice - vquanghuy/learn-opengl GitHub Wiki

Problem

When developing OpenGL applications on macOS using GLFW and Xcode, I observed an unusual behavior: building and running the application directly from Xcode would not work as expected: the program would often spawn multiple windows instead of just one.

This behavior was puzzling, especially after ensuring that glfwDestroyWindow(window) and glfwTerminate() was correctly called at the end of the program's execution to clean up GLFW's resources. The issue seemed tied directly to the presence of the debugger.

image

Solution

My first attempt to address this was to disable attaching the debugger to the executable, which did resolve the multiple window issue but meant I lost the ability to debug the program effectively.

image

However, I later discovered that this was not a unique issue to my setup. It appeared to be a known problem with Xcode and GLFW, particularly when using the debugger.

The workaround I found to mitigate this issue while retaining debugging capabilities was surprisingly simple: adding a brief pause at the very beginning of the main function, before any calls to glfwInit() or other library initializations.

By inserting a simple sleep for one second using , I gave the operating system and the debugger a small window of time to finalize the termination and cleanup of the previous process.

#ifdef __APPLE__
    // Sleep for 1 second as waiting debugger to start
    std::this_thread::sleep_for(std::chrono::seconds(1));
#endif

References