在进程终止解除分配资源 [英] Deallocate resources on process termination

查看:178
本文介绍了在进程终止解除分配资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能释放资源时的过程中被杀害的,例如,任务管理器?有没有一种方法来调用一个函数之前的过程中被关闭?

How I can deallocate resources when the process gets killed by, for example, the Task Manager? Is there a way to call a function before the process gets closed?

推荐答案

真的是你可以做,如果你的进程被杀死什么。根据定义,查杀过程就是这样 - 杀死它。此过程不会得到一个机会来运行任何code。这是非常大的设计。

There really is nothing you can do if your process is killed. By definition, killing a process is just that - killing it. The process does not get an opportunity to run any code. This is very much "by design".

想象一下,你可以(或其他进程)注册被调用时,你的进程已被用户程序。会是什么呢?在你的进程中的其他线程将处于不确定状态,你会如何同步呢?请记住,这个想法是,这个过程需要被杀死。

Imagine that you could register a routine that was called when your process was killed by the user (or by another process). What would it do? All the other threads in your process would be in an indeterminate state, How would you synchronize with them? Remember, the idea is that the process needs to be killed.

另一种情况是更加强硬:你的code是良性的,并试图做正确的事 - 如清理是一个很好的系统公民。有些code不是。想象一下,一个福音一个恶意软件作者将是,如果要运行该是被杀死一个过程中,允许操作系统code。这将是糟糕透了对于使用标准用户权限运行,完全无可非议的,具有管理权限的所有正在运行的恶意进程。

The other scenario is even tougher: your code is benign and trying to do the right thing - e.g. clean up and be a good system citizen. Some code isn't. Imagine what a boon to a malware author it would be if the OS allowed code to be run for a process that was being killed. It would be bad enough for malicious processes that were running with standard user privileges, and completely awful for any running with administrative privileges.

关键定型和结构化异常处理将不解决这个根本问题。

Critical finalizes and structured exception handling will not solve this fundamental issue.

在上攻,操作系统将释放所有它知道,当你的进程被终止,即内存和内核对象的资源。那些不会泄漏。但是,浏览器不知道你的程序,因此它不能清除它。

ON the upside, the OS will free all the resources it knows about when your process is killed, namely memory and kernel objects. Those will not leak. But explorer doesn't know about your process so it cannot clean up for it.

要解决这个问题是有一个过程的监控,保持你的其他进程的状态跟踪和清理它的一种方式。可以用一个简单的过程做到这一点,或者与服务。你也可以考虑某种形式的外壳扩展有它自己的线程做同样的事情。

One way to solve this would be to have a monitoring process that keeps track of your other processes state and cleans up for it. You could do this with a simple process, or with a service. You might also consider some kind of shell extension that had its own thread that did the same thing.

这篇关于在进程终止解除分配资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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