c++ - Vector reserve causes memory leak -


i have c++ app , know in advance expecting around 1000 elements (sometimes few more) store , process hundreds of instances. issue memory used process increases memory of server overwhelmed in few hours.
using valgrind detecting eventual memory leaks.

edit first of suggestions. managed solve many errors. added virtual destructors classes , changed pointers boost::shared_ptr using reset delete them. still have increasing memory usage on time.

the main parts of app follows :

#ifdef global #define global #else #define global extern #endif typedef std::map<uint32_t, resultstruct> classdresultmap ; typedef std::map<uint32_t, classdresultmap > classcresultmap ; typedef std::map<uint32_t, classcresultmap > classbresultmap ; typedef std::map<uint32_t, classbresultmap > classaresultmap ; global classaresultmap classaresultmap; 

classzero creates many classa

typedef std::map<uint32_t,classa*> classamap; classa_[aid] = new classa(io_service_, filename.c_str(), id, pri_queue_); 

inside classa, 3200 classb instance created:

typedef std::map<uint32_t, boost::shared_ptr<classb >> classbmap ; classbmap[aid].insert(std::pair<uint32_t, boost::shared_ptr<classb>>(bid, boost::make_shared<classb >(io_service_, pri_queue_, ...))); 

inside classb, 4 classc instances created inside configmap struct having member boost::shared_ptr<classc > classc_; :

typedef std::map<uint32_t, classcconfig> configmap; (configmap::iterator)->second.classc_ = boost::make_shared<classc >(io_service_, pri_queue_, ...); 

inside classc, 1 classd instantiated classc constructor

classc::classc(...): classd(io_service_, pri_queue_,...),... { } 

then inside classd, elements processed , results calculated every 10-30 seconds stored in classdresultmap :

(classdresultmap::iterator)->second[results.stime] = results; 

finally, inside class, classstorage, results in global classaresultmap saved files , cleared :

std::ofstream res_stream_; res_stream_ << "{ \"start\" : " << results_it->second.stime << ...; (classdresultmap::iterator)->second.clear(); std::map<uint32_t, classdresultmap>().swap((classdresultmap::iterator)->second); 

this valgrind 1 class : 8 classb : 32 class c/d

==9544== heap summary: ==9544==     in use @ exit: 25,817 bytes in 171 blocks ==9544==   total heap usage: 148,771 allocs, 148,600 frees, 16,803,272 bytes allocated ==9544==  ==9544== 1,216 bytes in 4 blocks possibly lost in loss record 39 of 45 ==9544==    @ 0x4c2677b: calloc (vg_replace_malloc.c:593) ==9544==    0x40118a2: _dl_allocate_tls (in /lib64/ld-2.12.so) ==9544==    0x5f971e8: pthread_create@@glibc_2.2.5 (in /lib64/libpthread-2.12.so) ==9544==    0x4e3acc0: boost::thread::start_thread_noexcept() (in /usr/local/lib/libboost_thread.so.1.54.0) ==9544==    0x45ee92: boost::thread::start_thread() (thread.hpp:180) ==9544==    0x461429: boost::thread::thread<boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::reference_wrapper<boost::asio::io_service> > >&>(boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::reference_wrapper<boost::asio::io_service> > >&) (thread.hpp:267) ==9544==    0x4605f7: boost::thread* boost::thread_group::create_thread<boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::reference_wrapper<boost::asio::io_service> > > >(boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::reference_wrapper<boost::asio::io_service> > >) (thread_group.hpp:78) ==9544==    0x45cac9: main (main.cpp:151) ==9544==  ==9544== leak summary: ==9544==    lost: 0 bytes in 0 blocks ==9544==    indirectly lost: 0 bytes in 0 blocks ==9544==      possibly lost: 1,216 bytes in 4 blocks ==9544==    still reachable: 24,601 bytes in 167 blocks ==9544==         suppressed: 0 bytes in 0 blocks ==9544== reachable blocks (those pointer found) not shown. ==9544== see them, rerun with: --leak-check=full --show-reachable=yes ==9544==  ==9544== counts of detected , suppressed errors, rerun with: -v ==9544== error summary: 1 errors 1 contexts (suppressed: 6 6) 

and part of main.cpp valgrind complains :

boost::asio::io_service work_io_service; classzero manager(work_io_service); boost::thread_group work_threads; uint32_t workers = boost::thread::hardware_concurrency(); (std::size_t = 0; != workers; ++i) {     work_threads.create_thread(     boost::bind(&boost::asio::io_service::run, boost::ref(work_io_service))); } 

my issue in 20 minutes, server memory (4gb ) increased 3% 17% , keeps going on. process memory usage increasing stays below 4% !


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -