在异常情况下,即使使用智能指针,C ++泄漏 [英] C++ leaks in case of exception even by using smart pointers

查看:185
本文介绍了在异常情况下,即使使用智能指针,C ++泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的智能指针世界。我做了我的阅读,所有的人都表示,智能指针将避免泄漏的记忆,即使程序将在遇到异常后退出。



我写了一个简单的程序尝试这一点,但 Valgrind 告诉我我的程序泄漏内存(三个alloc和只有一个免费)。



这是源代码:

  #include < iostream> 
#include< memory>

using namespace std;

int main()
{
auto_ptr< int> ptr_int(new int(5));

throw std :: bad_alloc();

cout<< * ptr_int;
}

此Valgrind报告:

  == 27862 == Memcheck,内存错误检测器
== 27862 ==版权所有(C)2002-2010和GNU GPL'd,由Julian Seward et al。
== 27862 ==使用Valgrind-3.6.0.SVN-Debian和LibVEX;重新运行-h版权信息
== 27862 ==命令:./smart_pointers
== 27862 ==父PID:5388
== 27862 ==
== 27862 ==
== 27862 == HEAP摘要:
== 27862 ==在退出时使用:2个块中的104个字节
== 27862 ==总堆使用量:3 allocs,1 frees,120字节分配
== 27862 ==
== 27862 == 1个块中的4个字节在丢失记录中仍然可达1 of 2
== 27862 == at 0x4026351:operator new(unsigned int)(vg_replace_malloc.c:255)
== 27862 == by 0x804878A:main(smart_pointers.cpp:8)
== 27862 ==
== 27862 == 1个块中的100个字节可能在丢失记录2中丢失2
== 27862 == at 0x4025BD3:malloc(vg_replace_malloc.c:236)
== 27862 == by 0x40E861A:__cxa_allocate_exception(in / usr / lib / libstdc ++。so.6.0.14)
== 27862 == by 0x80487AE:main(smart_pointers.cpp:10)
== 27862 ==
== 27862 ==漏洞摘要:
== 27862 ==明确丢失:0块在0块
== 27862 ==间接丢失:0块在0块
== 27862 ==可能丢失:100字节在1块
== 27862 ==仍然可达:4个字节在1块
== 27862 ==被抑制:0个字节在0块
== 27862 ==
== 27862 ==对于检测到的和抑制错误的计数,重新运行:-v
== 27862 == ERROR摘要:1个上下文中的1个错误(抑制:19从8开始)
=h2_lin>解决方案

std :: terminate() 被调用(对于未捕获的异常),正常清除不会运行(至少对于 main()),因此你在堆栈框架中分配的内存泄漏,即使它被智能指针管理。当您捕获 main()中的 std :: bad_alloc ,并正常返回时,智能指针做到这一点。


I am new to the smart pointers world. I've done my reading and all of them stated that smart pointers will avoid leaking memory even when the program will exit after encountering an exception.

I wrote down a simple program to try this out, but Valgrind is telling me my program is leaking memory (three allocs and only one free).

This is the source code:

#include <iostream>
#include <memory>

using namespace std;

int main()
{
    auto_ptr<int> ptr_int(new int(5));

    throw std::bad_alloc();

    cout << *ptr_int;
}

And this Valgrind report:

==27862== Memcheck, a memory error detector
==27862== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==27862== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
==27862== Command: ./smart_pointers
==27862== Parent PID: 5388
==27862==
==27862==
==27862== HEAP SUMMARY:
==27862==     in use at exit: 104 bytes in 2 blocks
==27862==   total heap usage: 3 allocs, 1 frees, 120 bytes allocated
==27862==
==27862== 4 bytes in 1 blocks are still reachable in loss record 1 of 2
==27862==    at 0x4026351: operator new(unsigned int) (vg_replace_malloc.c:255)
==27862==    by 0x804878A: main (smart_pointers.cpp:8)
==27862==
==27862== 100 bytes in 1 blocks are possibly lost in loss record 2 of 2
==27862==    at 0x4025BD3: malloc (vg_replace_malloc.c:236)
==27862==    by 0x40E861A: __cxa_allocate_exception (in /usr/lib/libstdc++.so.6.0.14)
==27862==    by 0x80487AE: main (smart_pointers.cpp:10)
==27862==
==27862== LEAK SUMMARY:
==27862==    definitely lost: 0 bytes in 0 blocks
==27862==    indirectly lost: 0 bytes in 0 blocks
==27862==      possibly lost: 100 bytes in 1 blocks
==27862==    still reachable: 4 bytes in 1 blocks
==27862==         suppressed: 0 bytes in 0 blocks
==27862==
==27862== For counts of detected and suppressed errors, rerun with: -v
==27862== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 19 from 8)

Does using smart pointers guarantee the allocated resources will be destroyed even if an exception shows up?

解决方案

When std::terminate() is called (as is the case for an uncaught exception), normal cleanup is not run (at least for the stack-frame of main()), and as such the memory you've allocated in that stack frame leaks, even though it's supposedly managed by a smart-pointer. When you're catching the std::bad_alloc in main(), and return normally, the smart-pointer will do it's duty.

这篇关于在异常情况下,即使使用智能指针,C ++泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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