当用户强迫我的程序退出时进行清理? [英] Cleanup when user forces my program to exit?

查看:51
本文介绍了当用户强迫我的程序退出时进行清理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的C程序中,我有许多动态分配内存的数据结构.如果用户执行以下任一操作:

In my C program, I have many data structures that are dynamically allocating memory. If the user does any of the following:

  • Alt-F4

  • Alt-F4

点击X

通过任务管理器等强制退出

Force quits via Task Manager, etc

然后我的程序将发生内存泄漏.如何在关闭请求时设置设置功能,以便可以在程序异常关闭之前清理全局变量?

then my program will have a memory leak. How do I do a set on close request feature so I can clean up my global variables before my program is shut down unnaturally?

推荐答案

(您的术语强烈建议使用特定于Windows的应用程序,因此我相应地编写了此答案.在其他现代操作系统上,术语不同,但是行为是除了某些嵌入式环境外,其他都是相同的,但是如果您为那些环境进行编码,则不会问这个问题.)

(Your terminology strongly suggests a Windows-specific application so I have written this answer accordingly. On other modern operating systems, the terminology is different, but the behavior is the same, except for some embedded environments, but if you were coding for those you would not have asked this question.)

首先,操作系统将在终止时自动取消分配与您的进程关联的所有内存.您不必担心会剩余RAM. 1

First, the OS will automatically deallocate all memory associated with your process when it is terminated. You never have to worry about RAM remaining allocated.1

第二,您可以在Alt-F4/窗口关闭时得到通知.我不知道细节,从来没有为Windows编写过GUI应用程序,但是应该有某种方式可以安排它,使其与从应用程序菜单中选择退出"相同.默认情况下,您的工具包"甚至可能会为您执行此操作.此通知的正确用法是将文件保存到磁盘并干净地关闭网络对话.(操作系统还将自动关闭所有文件句柄并断开连接所有网络套接字,但它不会为您刷新未保存的更改,也不会发送应用程序层的再见消息)

Second, you can get notified upon Alt-F4 / window close. I don't know the details, never having written a GUI application for Windows, but there should be some way you can arrange to get it treated the same as selecting Quit from the application's menus; your "toolkit" might even do this for you by default. The proper use of this notification is to save files to disk and cleanly shut down network dialogues. (The OS will also automatically close all file handles and disconnect all network sockets, but it won't flush unsaved changes for you, nor will it send application-layer goodbye messages.)

第三,您不能在强制退出"时收到通知,这是设计使然,因为它等同于工业机器人上红色大紧急停止按钮的软件-适用于情况现在,使过程消失的地方,无论什么,它比任何潜在的数据丢失都更为重要.由于您对此无能为力,因此不必担心.

Third, you cannot receive a notification upon "force quit", and this is by design, because it's the software equivalent of the big red emergency stop button on an industrial robot -- it's for situations where making the process go away right now, no matter what is more important than any potential data loss. As you can't do anything about this, you should not worry about it.

1 如注释中所讨论:是的,这意味着在退出之前不必手动释放内存.实际上,如此处.需要手动重新分配的常识"可以追溯到微型计算机的早期.CP/M和MS-DOS(的某些迭代)确实确实需要每个应用程序手动取消分配内存.如今,它是货物崇拜编程的实例.

1 As discussed in the comments: yes, this means it is not necessary to deallocate memory manually before exiting. In fact, it is more efficient if you don't, as discussed here and here. The "common wisdom" that manual deallocation is required dates to the early days of microcomputers; (some iterations of) CP/M and MS-DOS did indeed need each application to deallocate memory manually; nowadays it is an instance of cargo-cult programming.

也就是说,有 个充分的理由可以在退出之前手动释放内存.最明显的是某些泄漏检测工具需要您执行此操作,因为它们不知道如果应用程序烦扰了,它们可能会释放的内存"与该应用程序的内存可能会释放"之间的区别.丢失了所有指向的有效指针,并且无法释放了".一个不太明显的情况是,如果您正在编写一个库,或者将来您的应用程序有可能变成一个库.库必须能够在应用程序的请求下 手动释放所有分配,因为在一个进程的生命周期中,应用程序可能需要多次设置,使用和拆除库.(但是,如果 application 在其生存期内设置一次该库,然后重复使用它,然后在退出前不打扰它,则效率会更高.)

That said, there are some good reasons to deallocate memory manually before exiting. The most obvious is that some leak-detection tools need you to do it, because they do not know the difference between "memory that the application could have freed if it had bothered" and "memory that the application has lost all valid pointers to, and cannot free anymore". A less obvious case is if you are writing a library, or there's a possibility that your application might get turned into a library in the future. Libraries need to be able to manually free all of their allocations upon request by the application, because the application might need to set up, use, and tear down the library many times in the lifetime of one process. (However, it will be more efficient if the application sets up the library once in its lifetime, uses it repeatedly, and then doesn't bother tearing it down before exiting.)

这篇关于当用户强迫我的程序退出时进行清理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆