问题描述
在我的代码中,我使用了windows.h
中的HANDLE
.它们的用法就像
in my code I use HANDLE
s from windows.h
. They are used like
HANDLE h;
if (!openHandleToSomething(arg1, arg2, &h)) {
throw std::exception("openHandleToSomething error");
}
/* Use the handle in other functions which can throw as well */
if (!CloseHandle(h)) {
throw std::exception("closeHandle error");
}
如您所见,您必须将此CloseHandle
插入到在获取和发布过程中可能发生的每个异常.因此,很可能您忘记了一个(或者有一个您不知道的奇特的SEH异常),然后发现内存泄漏.
As you see, you have to insert this CloseHandle
to every exception which can happen in the middle of acquiration and release. Therefore, it's likely you forget one (or there is a fancy SEH exception which you didn't know about) and voilà, you have your memory leak.
最近,我读到有关RAII的信息,它应该可以消除此类情况下的头痛,并应自动将其称为CloseHandle
.我还看到C ++中有类似std::auto_ptr<someType>
的东西可以解决使用new
分配的资源的问题.
Recently, I've read about RAII which should remove the headaches from such cases and should call this CloseHandle
automatically. I've also seen that there is something like std::auto_ptr<someType>
in C++ which solves the problem for resources which were allocated with new
.
但是,由于我不使用new
,并且由于HANDLE
只是被typedef
修改为void *
,所以我想知道如何使用std::auto_ptr<someType>
.以某种方式,应该有可能为其提供自定义删除器功能(if (!CloseHandle(h)) { throw std::exception("closeHandle error"); }
).创建类将是另一种方法,因为只要析构函数的实例超出范围,就会调用该析构函数.但是,为每一个简单的事情都上一堂课是太过分了.
However, since I don't use new
and since HANDLE
is just typedef
ed to be a void *
, I wonder how I should use the std::auto_ptr<someType>
. Somehow, it should be possible to give it a custom deleter function (if (!CloseHandle(h)) { throw std::exception("closeHandle error"); }
). Creating a class would be another method since the destructor gets called any time an instance of it gets out of scope. However, it's just overkill to have a class for every simple thing.
如何解决这些意外的内存泄漏?
How can I fix these accidential memory leaks?
请注意,我更喜欢纯C ++,无库且无大依赖项的解决方案,除非它们确实很小,并且无论如何都在大多数环境中使用.
Note that I would prefer solutions which are in pure C++ with no libraries and big dependencies except if they are really small and used in most of the environments anyways.
推荐答案
想到的一个想法是使用 boost :: shared_ptr 和自定义删除器.
One idea that comes to mind is to use boost::shared_ptr with a custom deleter.
这篇关于使非对象资源符合RAII的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!