c++ - Memory leak with map -


i can't clear map memory (i checked 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)); } 

i tried _map.clear(), erase(), deleted _map->second manually not still no luck.


thanks replies. map alone not problem map singleton causing leak. what's wrong code below?

#include <iostream> #include <string> #include <map> #include <algorithm> #include "object.h"  #include<boost/smart_ptr.hpp>  using namespace std;  class singleton {     public:          // 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 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 singleton object" << std::endl; }  singleton::~singleton() {     _object_maps.clear(); } 

from code calling by

    singleton::object _test_object(singleton::instance()->insert_new("test")); 

is there problem? getting valgrind error, like

      ==19584== 17 bytes in 1 blocks possibly lost in loss record 31,429 of 52,291       ==19584==    @ 0x69a1642: operator new(unsigned int) (vg_replace_malloc.c:255)       ==19584==    0x772cb0a: std::string::_rep::_s_create(unsigned int, unsigned int, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.8)       ==19584==    0x772d904: ??? (in /usr/lib/libstdc++.so.6.0.8)        ==19584==    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==    0xbf1bc17: test::test() (test.c:34)        ==19584==    0xbf1db66: g__testdict_143_0_1(g__value*, char const*, g__param*, int) (testdict.c:190)        ==19584==    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==    0x71ef2e4: g__call_cppfunc (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libcint.so)        ==19584==    0x71c0095: g__interpret_func (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libcint.so)        ==19584==    0x71af883: g__getfunction (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libcint.so)        ==19584==    0x71d8cc1: g__new_operator (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libcint.so)        ==19584==    0x718d07f: g__getexpr (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libcint.so)        ==19584==    0x717724e: g__define_var (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libcint.so)        ==19584==    0x71fdec6: g__defined_type (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libcint.so)        ==19584==    0x7201a6d: g__exec_statement (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libcint.so)        ==19584==    0x71bf6c8: g__interpret_func (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libcint.so)        ==19584==    0x71af62f: g__getfunction (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libcint.so)        ==19584==    0x718437d: g__getitem (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libcint.so)        ==19584==    0x7189f12: g__getexpr (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libcint.so)        ==19584==    0x719713f: g__calc_internal (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libcint.so) 

short answer:
explicitly declared not defined destructor (forgot {}).

long answer:

  1. your code not compile. missing {} in class destructor , std:: in front of pair.
  2. corrected , completed 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; } 
  3. compiled on 32-bit ubuntu 11.04:

    g++ leak.cpp -o leak 
  4. run under valgrind supervision:

    valgrind ./leak ==20773== memcheck, memory error detector ==20773== copyright (c) 2002-2010, , gnu gpl'd, julian seward et al. ==20773== using valgrind-3.6.1 , libvex; rerun -h copyright info ==20773== command: ./leak ==20773==  ==20773==  ==20773== heap summary: ==20773==     in use @ exit: 0 bytes in 0 blocks ==20773==   total heap usage: 1 allocs, 1 frees, 24 bytes allocated ==20773==  ==20773== heap blocks freed -- no leaks possible ==20773==  ==20773== counts of detected , suppressed errors, rerun with: -v ==20773== error summary: 0 errors 0 contexts (suppressed: 17 6) 
  5. no memory leakage.

probably compiler auto-defines empty class destructor (because of missing {}), not auto-calling anymore on exit private member map destructor.

hope helps :)


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -