Walnut icon indicating copy to clipboard operation
Walnut copied to clipboard

Is the create and delete of the app intentional in EntryPoint.h?

Open glenpierce opened this issue 1 year ago • 3 comments

In EntryPoint.h,

https://github.com/StudioCherno/Walnut/blob/3b8e414fdecfc6c8b58816106fe8d912bd172e31/Walnut/src/Walnut/EntryPoint.h#L10-L20

This code appears to have a logical error, as it creates and deletes a new Walnut::Application object in each iteration of the while loop. This means that the application is constantly being restarted and terminated, which is probably not the intended behavior. A more reasonable approach would be to create the application object once before the loop, and delete it once after the loop, like this:

namespace Walnut {

    int Main(int argc, char** argv)
    {
        Walnut::Application* app = Walnut::CreateApplication(argc, argv);

        while (g_ApplicationRunning)
        {
            app->Run();
        }

            delete app;

            return 0;
    }

}

Please correct me if I'm mistaken here, Or maybe there's a good reason for it, I'm very new to this project.

glenpierce avatar Mar 01 '24 05:03 glenpierce

Update: I got courageous and tried it. My solution is... not good. It causes the app to crash when I try to exit/close the window.

If anyone has the time to explain why this loop is doing this, I would be quite thankful. This is a bit confusing to me.

glenpierce avatar Mar 01 '24 06:03 glenpierce

g_ApplicationRunning is set to false when exiting the window. An educated guess would be that this loop is here to facilitate easy application restarts, when e.g. changing to fullscreen or whatnot.

learn-more avatar Mar 05 '24 08:03 learn-more

It's because the while loop is "paused" while the run() function is being called. When the run() function is completed, then the app is deleted.

prodskimaya avatar Mar 08 '24 21:03 prodskimaya