valgrind shows memory leak in std::make_unique

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
14
down vote

favorite
1












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> b1., 2., 3.;
vector<double> c4., 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;



The lost happens during the make_unique call:



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?



(I`m calling valgrind from CLion 2018.1.5, Valgrind 3.13.0)







share|improve this question
























    up vote
    14
    down vote

    favorite
    1












    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> b1., 2., 3.;
    vector<double> c4., 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;



    The lost happens during the make_unique call:



    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?



    (I`m calling valgrind from CLion 2018.1.5, Valgrind 3.13.0)







    share|improve this question






















      up vote
      14
      down vote

      favorite
      1









      up vote
      14
      down vote

      favorite
      1






      1





      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> b1., 2., 3.;
      vector<double> c4., 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;



      The lost happens during the make_unique call:



      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?



      (I`m calling valgrind from CLion 2018.1.5, Valgrind 3.13.0)







      share|improve this question












      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> b1., 2., 3.;
      vector<double> c4., 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;



      The lost happens during the make_unique call:



      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?



      (I`m calling valgrind from CLion 2018.1.5, Valgrind 3.13.0)









      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 5 at 8:24









      user7431005

      310111




      310111






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          36
          down vote



          accepted










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






          share|improve this answer




















            Your Answer





            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52180485%2fvalgrind-shows-memory-leak-in-stdmake-unique%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            36
            down vote



            accepted










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






            share|improve this answer
























              up vote
              36
              down vote



              accepted










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






              share|improve this answer






















                up vote
                36
                down vote



                accepted







                up vote
                36
                down vote



                accepted






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






                share|improve this answer












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







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Sep 5 at 8:25









                Jarod42

                106k1295166




                106k1295166



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52180485%2fvalgrind-shows-memory-leak-in-stdmake-unique%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    What does second last employer means? [closed]

                    List of Gilmore Girls characters

                    One-line joke