C++ std::map memory management -


i have following c++ code

map<long, teleminfov01> lasttelemetry;  void updatetelemetry( const teleminfov01 &info ) {     lasttelemetry[info.mid] = info; } 

where teleminfov01 struct

the updatetelemetry method called outside of code, passing value, cache , use later. how map manage memory? copying struct in same way, have delete manually after removing global lasttelemetry map?

i not control scope of 'info' variable coming method. want cache it's value use in different call.

the main reason asking have memory leaks , track down.

thanks, stevo

the updatetelemetry method called outside of code, passing value, cache , use later. how map manage memory?

map keep own copy of class instances, if teleminfov01 implemented dont have worry memory leaks. if allocate memory inside must follow rule of three prevent memory leaks, still better put pointers inside smart pointers (the called rule of zero).

is copying struct in same way, have delete manually after removing global lasttelemetry map?

you dont have worry, after struct removed map, destructor called on , destroyed. so, memory leak here have allocate bare pointer in struct constructor, , forget deleting in destructor.

if cache global variable, destroyed once return main. if check memory leaks befor main end, cache might memory leak.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -