-1

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:

enter image description here

Edward
  • 6,278
  • 1
  • 25
  • 50
Ruben Vervaeke
  • 51
  • 2
  • 11
  • Off-topic, but might be useful: [About inheriting from STL containers](http://stackoverflow.com/questions/2034916/is-it-okay-to-inherit-implementation-from-stl-containers-rather-than-delegate) – emlai Feb 08 '15 at 15:25
  • This code would benefit greatly from the use of [smart pointers](http://stackoverflow.com/questions/106508/what-is-a-smart-pointer-and-when-should-i-use-one) – Edward Feb 08 '15 at 15:32
  • 1
    Just to make sure, are you aware that `m_inventory` ends up inheriting from `std::list`? I think one `*` would be enough, but I'm not sure whether your code needs two. – emlai Feb 08 '15 at 15:37
  • If so, I would recommend renaming the template argument of the `Inventory` class to distinguish it from your class with the same name. Might clear up some confusion. – emlai Feb 08 '15 at 15:42

3 Answers3

3

In your second code block you push_back() a pointer to a local variable (i.e. GameObject* item). It gets destroyed on returning and makes the IDE point this error out.

cadaniluk
  • 14,449
  • 2
  • 34
  • 61
  • 1
    Better recheck that. It looks to me that `item` is allocated on the heap using `new` and is therefore *not* destroyed when the function returns. – Edward Feb 08 '15 at 15:35
  • The item pointed to by `item` is not destroyed but he uses a pointer to the pointer itsself, which is a local variable. – cadaniluk Feb 08 '15 at 15:36
  • Ok, I see it now, but I cant write this: (*inv).push_back(item); – Ruben Vervaeke Feb 08 '15 at 15:41
  • Either allocate the pointer on the heap (what seems weird, at least to me), or rerwrite push_back to copy the pointer by value. – cadaniluk Feb 08 '15 at 15:42
2

I'd recommend changing this:

Inventory<GameObject*> m_inventory;

to this:

Inventory<GameObject> m_inventory;

so it will be an std::list<GameObject*> instead of std::list<GameObject**>.

Storing pointer-to-pointer-to-GameObject elements seems redundant, and storing simply pointers to GameObject should be enough, and make your other code simpler (such as this line: (*inv).push_back(&item)).

emlai
  • 37,861
  • 9
  • 87
  • 140
  • Thank you su much, that's the cause for all my problems I think – Ruben Vervaeke Feb 08 '15 at 15:58
  • I think the cause for all your problems is having the same name for both the `GameObject` class and the `GameObject` template argument ;) Template arguments should be named `T` or something similarly simple to clear up confusion like this. – emlai Feb 08 '15 at 15:59
0

I recently had this problem, which was due to me declaring the variable at the top of the function then declaring it again.