简体   繁体   中英

Destructor not run after exiting console

I have been fiddling about with GLEW and win32 lately, and I've run into a problem. Whenever I exit my application by means of closing the window, the destructor of my Window class gets called and my openGL rendering context deleted. I can tell because I tested the program using CodeXL and it was positive that my context was deleted. But if I exit my application by killing my console (created by using AllocConsole()), my context does not get deleted according to CodeXL and leaks memory. Here's the destructor which is supposed to destroy my context:

Window::~Window()
{
    wglMakeCurrent(0,0);
    wglDeleteContext(renderingContext);
}

Does anyone know why this destructor is not run when closing the console but is run whenever I kill the window?

Any input would be appreciated.

my context does not get deleted according to CodeXL and leaks memory.

Yes. So what? The process has been killed so all resources it did consumed are freed by the operating system. In fact, if a process is going to terminate anyway, you should not clean up. Just save those things that need to be saved into persistent storage, do the necessary communication to get things in order with other processes and then just terminate.

Iterating over all resources in a process and freeing/deleting them is just as if you were cleaning up and giving a house a paint job right before the demolition crew tears it down with a wrecking ball.

Memory leaks are never a problem at program termination! Memory leaks are a problem during program runtime: They make a process consume ever more resources, which eventually leads to system resource exhaustion. The reaction of the operating system is to kill processes that hog system resources, to gain breathing space.

Does anyone know why this destructor is not run when closing the console but is run whenever I kill the window?

Because these two actions are very different things. When closing a window, the system sends a WM_CLOSE message, which you can react to by properly leaving the message loop, returning from the main function which signals the runtime to call the constructors of all the objects that go out of scope.

When you close the console window, your process looses its controlling terminal (AllocConsole attaches the console as controlling terminal). This is a critical condition and the default behaviour is immediate process termination.

Update

There are of course several legitimate things to do at process exit. Writing things to a log, maybe generate an autosave of the very last state of the program before exit, stuff like that. When it comes to Windows Console Windows you have to install a handler, that offers the operating system a way to gracefully deal with console events. The function for this is called a HandlerRoutine : (documented at https://msdn.microsoft.com/en-us/library/windows/desktop/ms683242%28v=vs.85%29.aspx ) and set with SetConsoleCtrlHandler

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM