c++中内存泄漏的示例(通过使用异常)
Example of memory leak in c++ (by use of exceptions)
在C++如何编程中有一段话说:
一种常见的编程实践是分配动态内存,分配该内存指向一个指针,使用该指针操作内存并解除分配当不再需要内存时,使用delete来删除内存。如果在成功分配内存,但在delete语句执行之前,内存泄漏可能发生。C++标准在头中为提供类模板unique_ptr处理这种情况。
任何人都可以向我介绍一个真实的例子,异常发生,内存会像这篇文章一样泄漏吗?
一个更微妙的例子。
以一个包含两个动态分配数组的类的天真实现为例:
struct Foo {
private:
int* first;
int* second;
public:
Foo()
: first(new int[10000])
, second(new int[10000])
{ }
void Bar() { throw 42; }
~Foo()
{
delete [] first;
delete [] second;
}
};
int main()
{
Foo f;
/* more code */
}
现在,如果我们因为在某个地方调用方法Bar
而得到异常,那么一切都很好——堆栈展开保证了f
的析构函数被调用。
但是,如果我们在初始化second
时得到一个bad_alloc
,我们就会泄漏first
指向的内存。
class MyClass
{
public:
char* buffer;
MyClass(bool throwException)
{
buffer = new char[1024];
if(throwException)
throw std::runtime_error("MyClass::MyClass() failed");
}
~MyClass()
{
delete[] buffer;
}
};
int main()
{
// Memory leak, if an exception is thrown before a delete
MyClass* ptr = new MyClass(false);
throw std::runtime_error("<any error>");
delete ptr;
}
int main()
{
// Memory leak due to a missing call to MyClass()::~MyClass()
// in case MyClass()::MyClass() throws an exception.
MyClass instance = MyClass(true);
}
另请参阅:C++:如果构造函数可能抛出异常,则处理资源(参考FAQ17.4]
void func()
{
char *p = new char[10];
some_function_which_may_throw(p);
delete [] p;
}
如果对some_function_which_may_throw(p)
的调用引发异常,我们会泄漏p
指向的内存。
的简单示例
try {
int* pValue = new int();
if (someCondition) {
throw 42;
}
delete pValue;
} catch (int&) {
}
为了有一个不那么做作的例子,我最近在用给定分配器对象分配节点时,在代码中发现了这种潜在的泄漏。
std::unique_ptr<node,alloc_aware> allocate_new_node(allocator& al, const value_type^ v) {
char* buffer = al.allocate(sizeof(node)); //allocate memory
return std::unique_ptr<node>(al.construct(buffer, v),{al})); //construct
}
由于缓冲区的原因,如何解决这个问题还不太明显,但在帮助下,我得到了它:
struct only_deallocate {
allocator* a;
size_type s;
only_deallocate(allocator& alloc, size_type size):a(&alloc), s(size) {}
template<class T> void operator()(T* ptr) {a->deallocate(ptr, s);}
operator alloc_aware() const {return alloc_aware(*a, s);}
};
std::unique_ptr<node,alloc_aware> allocate_new_node(allocator& al, const value_type& v) {
std::unique_ptr<node, only_deallocate> buf(alloc.allocate(sizeof(node)),{alloc, sizeof(node)});//allocate memory
alloc.construct(buf.get(), value);
return std::unique_ptr<node,alloc_aware>(std::move(buf));
}
在此处编译代码
相关文章:
- 从构造函数抛出异常时如何克服内存泄漏
- 是否值得降低我的代码的可读性,以便在出现内存不足错误时提供异常安全性?
- 我们应该在抛出异常之前取消分配内存吗
- 未找到匹配的运算符删除;如果初始化引发异常,内存将不会被释放
- 内存位置出现Microsoft C++异常:std::out_of_range
- C++中的内存分配(引发异常:读取访问冲突)
- 提升进程间共享内存open_or_create每次都会引发异常
- 错误 在测试.exe 0x76C9FD62时出现未经处理的异常:Microsoft C++异常:内存位置0x006FF8
- 从构造函数内存泄漏引发异常
- 从内存导入密钥时加密++ BERDecode异常?
- 当有大量内存分配时,如何处理C++编程中的异常
- 存在内存访问异常,但我不确定我的代码中出了什么问题
- 内存碎片是否导致内存异常
- cv:内存位置的异常 - 运行 openCV 代码 Visual Studio 2013 & Visual Studio 2017 - Windows 7
- 返回内存异常错误的矢量
- 如果在构造函数中发生异常,如何释放动态内存
- Microsoft C++异常runtime_error在内存位置(代码 0x75A818A2)
- 位置处未处理的异常:内存位置处的Microsoft C++异常:std::length_error
- 在 1.exe 中0x77e4bef7时未处理的异常:Microsoft C++异常:内存位置 0x0012fb8c 处
- 0x771515ee Microsoft C++异常时未处理的异常:内存位置 0x0049f904 处的 std::ba