C++ Memory Leaks with my std::map of &Objects and std::vector of &Objects -


basically have this

std::map<std::string, location&> exits = std::map<std::string, location&>(); 

as private member in class. , unsure how delete free memory when object of class gets deleted

i have lot of vectors

std::vector<item> ritems; 

which unsure how delete, vector gets objects& added it

deleaker gives me around 1000 of following:

xmemory0, line 89 (c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0)

location object

class object; class location {     public:         location();         location(std::string roomname, std::string roomdesc);         ~location();         location(const location& e);         void addexit(std::string direction, location &room);         void additem(item &items);         void addobject(object &objects);         void removeobject(std::string objname);         void addnpc(npc &npcs);         void pickupitem(character &curchar, std::string itemname);         void displayall();         void displayexits();         void displayitems();         void displayobjects();         void displaynpcs();         std::string getname();         std::string getdesc();         location gocommand(std::string direction);         void talkcommand(std::string communication, character &maincharacter);         location operator=(const location &other);         object checkobject(std::string command, std::string objname);     private:         std::string name;         std::string description;          std::map<std::string, location&> exits = std::map<std::string, location&>();          std::vector<item> ritems;         std::vector<object> robjects;         std::vector<npc> rnpc; };  #include <iostream> #include "locations.h"   #include <regex> #include "object.h"  location::location() {     name = "";     description = ""; } location::location(std::string roomname, std::string roomdesc) {     name = roomname;     description = roomdesc; } location::~location() {  } location::location(const location& e) {     name = e.name;     description = e.description;     exits = e.exits;     ritems = e.ritems;     robjects = e.robjects;     rnpc = e.rnpc; } void location::addexit(std::string direction, location &room) {     exits.insert(std::pair<std::string, location*>(direction, &room)); } void location::additem(item &items) {     ritems.push_back(items); } void location::addobject(object &objects) {     robjects.push_back(objects); } void location::removeobject(std::string objname) {     object temp;     std::transform(objname.begin(), objname.end(), objname.begin(), ::tolower);     (int = 0; < robjects.size(); i++)     {         std::string temps = robjects[i].getname();         std::transform(temps.begin(), temps.end(), temps.begin(), ::tolower);         if (temps == objname)             robjects.erase(robjects.begin() + i);     } } void location::addnpc(npc &npcs) {     rnpc.push_back(npcs); } void location::pickupitem(character &curchar, std::string itemname) {     std::transform(itemname.begin(), itemname.end(), itemname.begin(), ::tolower);      (int = 0; < ritems.size(); i++)     {         std::string temp = ritems[i].getname();         std::transform(temp.begin(), temp.end(), temp.begin(), ::tolower);         if (temp == itemname)         {             curchar.additem(ritems[i]);             ritems.erase(ritems.begin() + i);         }     } } object location::checkobject(std::string command, std::string objname) {     object temp;     std::transform(command.begin(), command.end(), command.begin(), ::tolower);     std::transform(objname.begin(), objname.end(), objname.begin(), ::tolower);     (int = 0; < robjects.size(); i++)     {         std::string temps = robjects[i].getname();         std::transform(temps.begin(), temps.end(), temps.begin(), ::tolower);         if (temps == objname)             return robjects[i];     }     return temp; } void location::displayall() {     writeline(7, '-');     displayelement(7, description);     displayexits();     displayitems();     displayobjects();     displaynpcs();     writeline(7, '-'); } void location::displayexits() {     displayelement(7, "|- can travel; ");      (std::map<std::string, location*>::iterator ii = exits.begin(); ii != exits.end(); ++ii)     {         setcolour(7);         std::cout << "\t";         setcolour(112);         std::cout << "[" << (*ii).first << "]";         setcolour(8);         std::cout << " " << (*ii).second->getname() << std::endl;     } } void location::displayitems() {     int count = 0;     if (ritems.size() != 0)     {         displayelement(7, "items in room: ");         (int = 0; < ritems.size(); i++)         {             displayelementwc(count, 5, 13, ritems[i].getname());             displayelementwc(count, 6, 14, ritems[i].getdesc());             displayelementwc(count, 6, 14, ritems[i].getitemvalue());             count++;         }     } } void location::displayobjects() {     int count = 0;     if (robjects.size() != 0)     {         displayelement(7, "objects in room: ");         (int = 0; < robjects.size(); i++)         {             displayelementwc(count, 5, 13, robjects[i].getname());             displayelementwc(count, 6, 14, robjects[i].getdesc());         }     } } void location::displaynpcs() {     int count = 0;     if (rnpc.size() != 0)     {         displayelement(7, "npcs in room: ");         (int = 0; < rnpc.size(); i++)         {             displayelementwc(count, 5, 13, rnpc[i].getname());             displayelementwc(count, 6, 14, rnpc[i].getdesc());         }     } } std::string location::getname() {     return name; } std::string location::getdesc() {     return description; }  location location::gocommand(std::string direction) {     location returnloc = *this;     std::string test;     std::transform(direction.begin(), direction.end(), direction.begin(), ::tolower);     (std::map<std::string, location*>::iterator ii = exits.begin(); ii != exits.end(); ++ii)     {         test = (*ii).first;         std::transform(test.begin(), test.end(), test.begin(), ::tolower);         if (test == direction)             returnloc = *(*ii).second;     }     return returnloc; } void location::talkcommand(std::string communication, character &maincharacter) {     std::string test;     std::transform(communication.begin(), communication.end(), communication.begin(), ::tolower);     (int = 0; < rnpc.size(); i++)     {         test = rnpc[i].getname();         std::transform(test.begin(), test.end(), test.begin(), ::tolower);         if (test == communication)         {             rnpc[i].startconvo(maincharacter);         }     } } location location::operator=(const location &other) {     name = other.name;     description = other.description;     exits = other.exits;     ritems = other.ritems;     robjects = other.robjects;     rnpc = other.rnpc;     return *this; } 

okay hope mcve aha

#include <iostream> #include <map> #include <regex> #include <string> #include <windows.h> #include <cctype> //custom classes  class location; class updatelocation { public:     updatelocation();     ~updatelocation();     void addlocation(location &room);     void updatenow(location &room);     location getlocal(location &room);  private:     std::map<std::string, location*> locations = std::map<std::string, location*>(); };  class location { public:     location();     location(std::string roomname, std::string roomdesc);     ~location();     location(const location& e);     void addexit(std::string direction, location &room);     void displayexits();     std::string getname();     std::string getdesc();     location operator=(const location &other); private:     std::string name;     std::string description;      std::map<std::string, location*> exits = std::map<std::string, location*>(); };  updatelocation::updatelocation() {  } updatelocation::~updatelocation() {  } void updatelocation::addlocation(location &room) {     locations.insert(std::pair<std::string, location*>(room.getname(), &room)); } void updatelocation::updatenow(location &room) {     (std::map<std::string, location*>::iterator ii = locations.begin(); ii != locations.end(); ++ii)     {         if ((*ii).first == room.getname())         {             *(*ii).second = room;         }     } } location updatelocation::getlocal(location &room) {     (std::map<std::string, location*>::iterator ii = locations.begin(); ii != locations.end(); ++ii)     {         if ((*ii).first == room.getname())         {             return *(*ii).second;         }     } }  location::location() {     name = "";     description = ""; } location::location(std::string roomname, std::string roomdesc) {     name = roomname;     description = roomdesc; } location::~location() {  } location::location(const location& e) {     name = e.name;     description = e.description;     exits = e.exits;  } void location::addexit(std::string direction, location &room) {     exits.insert(std::pair<std::string, location*>(direction, &room)); } void location::displayexits() {     std::cout << "|- can travel; " << std::endl;      (std::map<std::string, location*>::iterator ii = exits.begin(); ii != exits.end(); ++ii)     {         std::cout << "\t";         std::cout << "[" << (*ii).first << "]";         std::cout << " " << (*ii).second->getname() << std::endl;     } } std::string location::getname() {     return name; } std::string location::getdesc() {     return description; } location location::operator=(const location &other) {     name = other.name;     description = other.description;     exits = other.exits;     return *this; }  void main() {     //create gameworld     updatelocation updateit;     location hallway("hallway", "long corridor wide array of footboats");      getchar();     getchar(); } 

first, want read (for c++03 , earlier):

why can't store references in stl map in c++?

for c++11 , later it's possible have references values in std::maps using std::map::emplace(), it's inconvenient , cannot see being useful raw pointers, should replaced std::unique_ptrs if container object owns objects placed in it.

you want

std::map<std::string, location *> exits; 

as private member. there's no need delete map nor vector. when destructor of class called, destructor of respective object called. self-destruct. explained exits object doesn't own location objects, exits shouldn't have deallocating memory allocated them.


Comments