C++ C++;我的std::map of&;内存泄漏;对象和std::的向量&;物体
基本上我有这个C++ C++;我的std::map of&;内存泄漏;对象和std::的向量&;物体,c++,object,dictionary,memory,reference,C++,Object,Dictionary,Memory,Reference,基本上我有这个 std::map<std::string, Location&> exits = std::map<std::string, Location&>(); std::map exits=std::map(); 作为班级中的私人成员。当类的对象被删除时,我不确定如何删除它以释放内存 我也有很多这样的向量 std::vector<Item> Ritems; std::vector-Ritems; 我也不确定如何删除,向量得到对
std::map<std::string, Location&> exits = std::map<std::string, Location&>();
std::map exits=std::map();
作为班级中的私人成员。当类的对象被删除时,我不确定如何删除它以释放内存
我也有很多这样的向量
std::vector<Item> Ritems;
std::vector-Ritems;
我也不确定如何删除,向量得到对象并添加到其中
Deleaker给了我大约1000条以下信息:
xmemory0,第89行(c:\ProgramFiles(x86)\microsoft visual studio 14.0\vc\include\xmemory0)
位置对象
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);
for (int i = 0; i < 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);
for (int i = 0; i < 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);
for (int i = 0; i < 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, "|- You can travel; ");
for (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 << " to " << (*ii).second->GetName() << std::endl;
}
}
void Location::DisplayItems()
{
int Count = 0;
if (Ritems.size() != 0)
{
DisplayElement(7, "Items in room: ");
for (int i = 0; i < 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: ");
for (int i = 0; i < 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: ");
for (int i = 0; i < 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);
for (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);
for (int i = 0; i < 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;
}
类对象;
类位置
{
公众:
位置();
位置(std::string RoomName,std::string RoomDesc);
~Location();
位置(施工位置和e);
void AddExit(标准:字符串方向、位置和房间);
无效附加项(项和项);
void AddObject(对象和对象);
void RemoveObject(std::string ObjName);
无效AddNPC(NPC和NPC);
无效拾取项(字符和CurChar,std::string ItemName);
void DisplayAll();
void DisplayExits();
作废显示项();
void DisplayObjects();
无效显示npcs();
std::string GetName();
std::string GetDesc();
位置命令(标准::字符串方向);
void TalkCommand(标准::字符串通信、字符和主字符);
位置运算符=(常量位置和其他);
对象检查对象(std::string命令,std::string对象名);
私人:
std::字符串名;
std::字符串描述;
std::map exits=std::map();
std::矢量标准;
std::向量对象;
std::载体RNPC;
};
#包括
#包括“Locations.h”
#包括
#包括“Object.h”
位置::位置()
{
Name=“”;
Description=“”;
}
位置::位置(std::string RoomName,std::string RoomDesc)
{
名称=房间名称;
Description=RoomDesc;
}
位置::~Location()
{
}
位置::位置(常数位置和e)
{
名称=e.名称;
描述=e.描述;
出口=出口;
里特姆斯=e.里特姆斯;
机器人=e.机器人;
RNPC=e.RNPC;
}
void位置::AddExit(标准::字符串方向、位置和房间)
{
插入(标准::对(方向和房间));
}
无效位置::附加项(项和项)
{
仪式。推回(项目);
}
无效位置::AddObject(对象和对象)
{
机器人。推回(物体);
}
无效位置::RemoveObject(标准::字符串对象名)
{
对象温度;
std::transform(ObjName.begin(),ObjName.end(),ObjName.begin(),::tolower);
对于(int i=0;i std::cout首先,您可能想阅读以下内容(对于c++03及更早版本):
对于c++11和更高版本,实际上可以使用std::map::emplace()
在std::map
s中使用引用作为值,但这很不方便,而且我看不出它像原始指针那样有用,如果容器对象拥有放置在其中的对象,也应该用std::unique_ptr
s替换它
你可能想要
std::map<std::string, Location *> exits;
std::map出口;
作为您的私有成员。无需删除地图或向量。当调用类的析构函数时,将调用相应对象的析构函数。它们基本上是自毁的。您解释说退出对象不拥有位置对象,因此退出不应该有任何内容取消分配分配给他们的内存。你没有删除这些的内存。我不明白所有1000次泄漏可能来自哪里?你在项中分配了内存吗?
?也许你没有正确处理3的规则。项中只有一些字符串和布尔值。你只删除你想要的东西u使用new
创建。除非使用new
创建容器,否则不会删除容器;除非使用new
创建容器,否则不会删除容器的元素。也就是说,您的设计暗示了对对象所有权的一些混淆,应该进行更改,以使所有权清晰明确。此外,您的MCVE没有显示任何泄漏,因为它不起任何作用。你从未向容器中添加任何东西。你忘记了MCVE t的部分
std::map<std::string, Location *> exits;