I've been reading a lot on the internet about this error and why is would be caused, but I can't find the fault in my code.
I have an Inventory
class that inherits a list to GameObject
pointers:
#ifndef INVENTORY_H
#define INVENTORY_H
#include "GameObject.h"
#include <list>
template <class GameObject>
class Inventory : public std::list<GameObject*>
{
private:
public:
Inventory() : std::list<GameObject*>::list() {}
};
#endif
GameObject
class looks like this:
class GameObject : public Updateable
{
private:
...
Inventory<GameObject*> m_inventory;
public:
...
void SetInventory(Inventory<GameObject*> inventory);
Inventory<GameObject*>& GetInventory();
};
Then I populate a new Inventory
object via this method:
Inventory<GameObject*>& GameInitializer::ConfigureItems(XMLElement* xmlGameObject) {
Inventory<GameObject*>* inv = new Inventory<GameObject*>();
...
while (currElement != NULL) {
GameObject* item = new GameObject();
// Configure all properties of the item
item->SetId(currElement->Attribute("id"));
item->SetPropertyHolder(ConfigureProperties(currElement));
item->SetName(item->GetPropertyHolder().GetProperty("NAME").As<string>());
// Add item to inventory
(*inv).push_back(&item);
currElement = currElement->NextSiblingElement();
}
return (*inv);
}
But whenever the reference to this Inventory
object is returned, the member variables in GameObject
class (id
, name
) are unable to read from memory: