Valgrind在std :: make_unique中显示内存泄漏 [英] Valgrind shows memory leak in std::make_unique

查看:418
本文介绍了Valgrind在std :: make_unique中显示内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Valgrind检查内存泄漏. 不幸的是,我收到了Leak_DefinitelyLost警告.

I'm using Valgrind to check for memory leaks. Unfortunately I get a Leak_DefinitelyLost warning.

随附的是我的代码的简化版本,该错误再现了该错误:

Attached is a simplified version of my code that reproduces the error:

#include <iostream>
#include <vector>
#include <memory>
#include <unordered_map>

using namespace std;

class Base{
public:
    explicit Base(double a){
        a_ = a;
    }
    virtual void fun() = 0;

    protected:
        double a_;
};


class Derived_A : public Base{

    public:
        Derived_A(double a, vector<double> b, vector<double> c): Base(a), b_{b}, c_{c}{
        }
        void fun() override{
            cout << "Derived_A " << a_ << endl;
        }

    private:
        vector<double> b_;
        vector<double> c_;
};


class Derived_B : public Base{

    public:
        Derived_B(double a, double b, double c): Base(a), b_{b}, c_{c}{
        }

        void fun() override{
            cout << "Derived_B " << a_ << endl;
        }

    private:
        double b_;
        double c_;
};

int main() {

    unordered_map<string, unique_ptr<Base> > m;

    for(int i=0; i<10; ++i){
        unique_ptr<Base> o;
        if(i%2 == 0){
            vector<double> b{1., 2., 3.};
            vector<double> c{4., 5., 6.};
            o = make_unique<Derived_A>(i, move(b), move(c));
            m[to_string(i)] = move(o);
        }else{
            double b = 1.;
            double c = 2.;
            o = make_unique<Derived_B>(i, b, c);
            m[to_string(i)] = move(o);
        }
    }

    for(const auto &any:m){
        any.second->fun();
    }

    return 0;
}

迷失发生在make_unique通话期间:

Leak_DefinitelyLost
vg_replace_malloc.c
240 bytes in 10 blocks are definitely lost in loss record 1 of 1
    operator new(unsigned long)
    __gnu_cxx::new_allocator<double>::allocate(unsigned long, void const*)
    std::allocator_traits<std::allocator>::allocate(std::allocator<double>&, unsigned long)
    std::_Vector_base<double, std::allocator>::_M_allocate(unsigned long)
    std::_Vector_base<double, std::allocator>::_M_create_storage(unsigned long)
    std::_Vector_base<double, std::allocator>::_Vector_base(unsigned long, std::allocator<double> const&)
    std::vector<double, std::allocator>::vector(std::vector<double, std::allocator> const&)
    Derived_A::Derived_A(double, std::vector<double, std::allocator>, std::vector<double, std::allocator>)
    std::_MakeUniq<Derived_A>::__single_object std::make_unique<Derived_A, int&, std::vector, std::vector>(int&, std::vector<double, std::allocator>&&, std::vector<double, std::allocator>&&)
    main

我不确定自己在做什么错.有人可以澄清错误发生的地方吗?

I'm not sure what I'm doing wrong. Can someone please clarify where the error occurs?

(我从CLion 2018.1.5,Valgrind 3.13.0中呼叫Valgrind.)

(I'm calling Valgrind from CLion 2018.1.5, Valgrind 3.13.0.)

推荐答案

Base缺少虚拟析构函数,因此您调用 UB .

Base is missing a virtual destructor, so you invoke UB.

这篇关于Valgrind在std :: make_unique中显示内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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