I am writing scene manager which, among other things, contains a map of all objects, or rather pointers to them. I remember to delete my variables created using "new" and i tried doing it in my Scene deconstructor, but I got 0xC0000005 exit code and afaik it means that i tried to use memory that doesnt really exist anymore. Here is my Scene class:
class Scene{
public:
Scene();
~Scene();
// other functions that I cut for question purposes
std::map<GLuint, Object*> view_layer;
Object AddObject(const std::string &title);
};
And I am creating new object in scene like this ("new Object" returns only pointer to newly created variable):
Object Scene::AddObject(const std::string &title) {
// other stuff again
view_layer.insert(std::make_pair(currentObjectID, new Object));
return *view_layer.find(currentObjectID)->second;
}
And i tried to delete it like this:
Scene::~Scene() {
for (auto const& [key, val] : view_layer)
{
if(val){
std::cout << "Deleting object...\n";
delete [] val;
}
}
};
But program crashes after this std::cout, right on delete [] val.
Val should be a reference to the pointer of my object, and delete [] needs pointer, so i guess that i have no syntax issue here.
My question is - Am I doing something wrong or I am just not supposed to delete this because somehow it deletes itself ? Thanks for any kind of help!