C++ 内存泄漏与映射

C++ 内存泄漏与映射,c++,memory-leaks,map,C++,Memory Leaks,Map,我无法清除地图内存(我已通过检查) 我打电话的另一个密码 Singleton::object _test_object(Singleton::Instance()->insert_new("TEST")); 有什么问题吗?我得到了一个Valgrind错误,比如 ==19584== 17 bytes in 1 blocks are possibly lost in loss record 31,429 of 52,291 ==19584== at 0x

我无法清除地图内存(我已通过检查)

我打电话的另一个密码

    Singleton::object _test_object(Singleton::Instance()->insert_new("TEST"));
有什么问题吗?我得到了一个Valgrind错误,比如

      ==19584== 17 bytes in 1 blocks are possibly lost in loss record 31,429 of 52,291
      ==19584==    at 0x69A1642: operator new(unsigned int) (vg_replace_malloc.c:255)
      ==19584==    by 0x772CB0A: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.8)
      ==19584==    by 0x772D904: ??? (in /usr/lib/libstdc++.so.6.0.8)
       ==19584==    by 0x772DB16: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.8)
       ==19584==    by 0xBF1BC17: test::test() (test.C:34)
       ==19584==    by 0xBF1DB66: G__testDict_143_0_1(G__value*, char const*, G__param*, int) (testDict.C:190)
       ==19584==    by 0x70EA4E5: Cint::G__ExceptionWrapper(int (*)(G__value*, char const*, G__param*, int), G__value*, char*, G__param*, int) (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
       ==19584==    by 0x71EF2E4: G__call_cppfunc (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
       ==19584==    by 0x71C0095: G__interpret_func (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
       ==19584==    by 0x71AF883: G__getfunction (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
       ==19584==    by 0x71D8CC1: G__new_operator (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
       ==19584==    by 0x718D07F: G__getexpr (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
       ==19584==    by 0x717724E: G__define_var (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
       ==19584==    by 0x71FDEC6: G__defined_type (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
       ==19584==    by 0x7201A6D: G__exec_statement (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
       ==19584==    by 0x71BF6C8: G__interpret_func (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
       ==19584==    by 0x71AF62F: G__getfunction (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
       ==19584==    by 0x718437D: G__getitem (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
       ==19584==    by 0x7189F12: G__getexpr (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
       ==19584==    by 0x719713F: G__calc_internal (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
==19584==1个块中的17个字节可能在52291的丢失记录31429中丢失
==19584==at 0x69A1642:运算符新(无符号整数)(vg_替换_malloc.c:255)
==19584==by 0x772CB0A:std::string::_Rep::_S_create(unsigned int,unsigned int,std::allocator const&)(in/usr/lib/libstdc++.so.6.0.8)
==19584==0x772D904:???(in/usr/lib/libstdc++.so.6.0.8)
==19584==by 0x772DB16:std::basic_string::basic_string(char const*,std::allocator const&)(in/usr/lib/libstdc++.so.6.0.8)
==19584==by 0xBF1BC17:test::test()(test.C:34)
==19584==0xBF1DB66:G_uutestdict_143_0_1(G_u值*,字符常量*,G_u参数*,int)(testDict.C:190)
==19584==0x70EA4E5:Cint::G_u异常包装(int(*)(G_u值*,char const*,G_u参数*,int),G_u值*,char*,G_u参数*,int)(in/afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/libCint.so)
==19584==0x71EF2E4:G_ucall_cppfunc(在/afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so中)
==19584==0x71C0095:G_u解释函数(in/afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
==19584==0x71AF883:G_ugetfunction(in/afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
==19584==由0x71D8CC1:G_uunew_u运算符(in/afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
==19584==0x718D07F:G_ugetexpr(in/afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
==19584==0x717724E:G_udefine_var(in/afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
==19584==0x71FDEC6:G_u定义的类型(in/afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
==19584==0x7201A6D:G_uexec_语句(in/afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
==19584==0x71BF6C8:G_uuexploration_func(in/afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
==19584==0x71AF62F:G_ugetfunction(in/afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
==19584==0x718437D:G_ugetitem(in/afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
==19584==0x7189F12:G_ugetexpr(in/afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
==19584==0x719713F:G_计算内部(in/afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
试试:

{
std::map空映射;
空的映射交换(\u映射);
}

(至少,这是说服标准库容器实际释放内存的常用方法。)

简短回答:
您明确声明但未定义析构函数(忘记了
{}

长答案

  • 您的代码甚至不编译。类析构函数中缺少
    {}
    ,并且
    对前面缺少
    std::
  • 已更正并用
    main
    完成:

    #include <map>
    
    class testMap {
      public:
        testMap() {}
        ~testMap() {};
    
        void insert_map(int, int);
    
      private:
        std::map<int,int> _map;
    };
    
    void testMap::insert_map(int i, int j) {
        _map.insert(std::pair<int, int>(i,j));
    }
    
    int main() {
        testMap t;
        t.insert_map(12, 34);
        return 0;
    }
    
  • valgrind
    监督下运行:

    valgrind ./leak
    ==20773== Memcheck, a memory error detector
    ==20773== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
    ==20773== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
    ==20773== Command: ./leak
    ==20773== 
    ==20773== 
    ==20773== HEAP SUMMARY:
    ==20773==     in use at exit: 0 bytes in 0 blocks
    ==20773==   total heap usage: 1 allocs, 1 frees, 24 bytes allocated
    ==20773== 
    ==20773== All heap blocks were freed -- no leaks are possible
    ==20773== 
    ==20773== For counts of detected and suppressed errors, rerun with: -v
    ==20773== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 17 from 6)
    
  • 没有内存泄漏

  • 可能编译器自动定义了一个完全空的类析构函数(因为缺少
    {}
    ),在退出私有成员映射析构函数时不再自动调用


    希望有帮助:)

    您从valgrind获得了什么输出?您是否可以显示您正在进行的创建/销毁testMap对象的调用?以及,在地图名称中使用下划线?糟糕@乔治,我也这么想,直到有人告诉我实际的规则——在课堂上是可以的。我认为这是成员变量的一个非常常见的约定。@Sen:您使用了什么编译器?或者更简洁地说,
    std::map().swap(_-map)
    @ildjarn:Yours的优点是可以立即释放内存,而不是在
    empty\u map
    超出范围时释放内存。我的优点是,对于不熟悉这个成语的人来说,更容易阅读。“简洁”并不总是“更好”。我从来没有说过“更好”,这是有原因的。我很想知道长的答案,因为我不知道除了生成链接器错误之外,它还有什么坏处。@Mark:我想这里的重点是在出现语法错误的情况下,编译器的奇怪行为。刚才问OP他用的是什么编译器。@Vanni:如果编译器在用户声明析构函数时自动生成析构函数,我会感到震惊。。。应该是链接器错误。但是,+1说明map的一般用法是合理的,给出了一个合理的编译器。
    {
        std::map<int,int> empty_map;
        empty_map.swap(_map);
    }
    
    #include <map>
    
    class testMap {
      public:
        testMap() {}
        ~testMap() {};
    
        void insert_map(int, int);
    
      private:
        std::map<int,int> _map;
    };
    
    void testMap::insert_map(int i, int j) {
        _map.insert(std::pair<int, int>(i,j));
    }
    
    int main() {
        testMap t;
        t.insert_map(12, 34);
        return 0;
    }
    
    g++ leak.cpp -o leak
    
    valgrind ./leak
    ==20773== Memcheck, a memory error detector
    ==20773== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
    ==20773== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
    ==20773== Command: ./leak
    ==20773== 
    ==20773== 
    ==20773== HEAP SUMMARY:
    ==20773==     in use at exit: 0 bytes in 0 blocks
    ==20773==   total heap usage: 1 allocs, 1 frees, 24 bytes allocated
    ==20773== 
    ==20773== All heap blocks were freed -- no leaks are possible
    ==20773== 
    ==20773== For counts of detected and suppressed errors, rerun with: -v
    ==20773== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 17 from 6)