简体   繁体   English

退出控制台后,析构函数无法运行

[英]Destructor not run after exiting console

I have been fiddling about with GLEW and win32 lately, and I've run into a problem. 最近我一直在摆弄GLEW和win32,但遇到了问题。 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. 每当我通过关闭窗口退出应用程序时,都会调用Window类的析构函数,并删除我的openGL渲染上下文。 I can tell because I tested the program using CodeXL and it was positive that my context was deleted. 我可以说是因为我使用CodeXL测试了程序,并且可以肯定地说我的上下文被删除了。 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. 但是,如果我通过杀死控制台(使用AllocConsole()创建)退出应用程序,则根据CodeXL不会删除我的上下文,并且会泄漏内存。 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. 我的上下文不会根据CodeXL删除并泄漏内存。

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. 关闭窗口时,系统会发送WM_CLOSE消息,您可以通过适当地离开消息循环来作出反应,该消息循环从主函数返回,并向运行时发出信号,以调用超出范围的所有对象的构造函数。

When you close the console window, your process looses its controlling terminal (AllocConsole attaches the console as controlling terminal). 关闭控制台窗口时,您的进程将失去其控制终端(AllocConsole将控制台附加为控制终端)。 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. 对于Windows控制台Windows,您必须安装处理程序,该处理程序为操作系统提供了一种优雅地处理控制台事件的方法。 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 此功能称为HandlerRoutine :(在https://msdn.microsoft.com/zh-cn/library/windows/desktop/ms683242%28v=vs.85%29.aspx中记录 ),并使用SetConsoleCtrlHandler进行设置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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