内存泄漏与地图

Abh*_*Sen 5 c++ memory-leaks map

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

#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( pair<int, int>(i,j));
}
Run Code Online (Sandbox Code Playgroud)

我试过_map.clear(),手动erase()删除_map->second但不是没有运气.


感谢所有回复.实际上map一个人不是问题,但是map单身人士会导致泄密.下面的代码有什么问题?

#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#include "Object.h"

#include<boost/smart_ptr.hpp>

using namespace std;

class Singleton {
    public:

        // A wrapper around Object class
        class object
        {
            public:
                object() : _object(new Object())
                    {}
                Object get(void)
                    { return _object.get(); }
            private:
                boost::shared_ptr<Object> _object;
        };

        object insert_new(const std::string key)
        {
            _object_maps.insert( pair<string,object>( key, object() ));
            return _object_maps.find( key )->second;
            //_test_object = object();
            //return _test_object;  // Leak goes away if I don't use map.
        }

        static Singleton* Instance();
        void Print();

    protected:
        Singleton(){}
        ~Singleton();

    private:
        static Singleton* _instance;

        std::map<std::string, object > _object_maps;
        object _test_object;
};

Singleton* Singleton::_instance = 0;

Singleton* Singleton::Instance() {
    if( _instance ==0 )
    {
        _instance = new Singleton();
    }
    return _instance;
}

void Singleton::Print() {
    std::cout << " Hi I am a singleton object" << std::endl;
}

Singleton::~Singleton()
{
    _object_maps.clear();
}
Run Code Online (Sandbox Code Playgroud)

从另一个我打电话的代码

    Singleton::object _test_object(Singleton::Instance()->insert_new("TEST"));
Run Code Online (Sandbox Code Playgroud)

有问题吗?我得到一个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)
Run Code Online (Sandbox Code Playgroud)

Van*_*aro 5

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

答案很长:

  1. 您的代码甚至无法编译.{}在类析构函数和std::前面缺少pair.
  2. 更正并完成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;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 在32位Ubuntu 11.04上编译:

    g++ leak.cpp -o leak
    
    Run Code Online (Sandbox Code Playgroud)
  4. 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)
    
    Run Code Online (Sandbox Code Playgroud)
  5. 没有内存泄漏.

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

希望能帮助到你 :)


Nem*_*emo 3

尝试:

{
    std::map<int,int> empty_map;
    empty_map.swap(_map);
}
Run Code Online (Sandbox Code Playgroud)

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

  • 或者更简洁地说,`std::map&lt;int, int&gt;().swap(_map);`。 (4认同)
  • 我从来没有说过“更好”,这是有原因的。 (2认同)