Я не могу очистить память карты (я проверял 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));
}
Я пытался _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();
}
Из другого кода, который я звонил по
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)