A Query on pointers

Well another query for the day. A bit long post this time. Please bear with me and help me

I have a class called TEnt which basically defines a entity.
I have another class called Manager which sort of manages the entity. The manager is a singleton class which has the following members
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
        /*! This is called from other classes to register the instance with the manager*/
        void REGISTER_ENTITY_INSTANCE(TEnt& instance);

        /*! Call the set entity call in each of the registered entities*/
        void EntityCall(void);
        /*! Calls a draw entity call to each of the registerdd entity*/
        void drawEntities(void);
        /*! Prints out a test message*/
        void print();

    private:
        /*! A vector of TEnts*/
        std::vector < TEnt* >  m_ppTEntList;


    ///////////////////////////////////////////converting this into a singleton
    ////////////////////////////////////////////////////////////////////////////
     private:
            EntityManager(){;}
            EntityManager(const EntityManager&){;}
            EntityManager& operator= (const EntityManager&){;}
            static EntityManager* m_pInstance;
            static bool is_InstanceSet;

        public:
            static EntityManager* getPInstance();
            ~EntityManager()
            {
                 is_InstanceSet=false;
             }


I have a Screen class which displays all the entities on the screen and a LevelEditor class which initialises and registers the entities on to the entity manager.

My Level Editor Class has the following function
1
2
3
4
void LevelEditor::Init_Level(){
EntityManager* Manager=EntityManager::getPInstance();
Manager->REGISTER_ENTITY_INSTANCE(a);
}


The REGISTER_ENTITY_INSTANCE(a) pushes the address of the instance onto a vector.
 
m_ppTEntList.push_back(&instance);


and my screen class has this
1
2
3
4
5
//Init level
LevelEditor* Levels=new LevelEditor;
Levels->Init_Level();
//Init the Emanager
EntityManager* EManager=EntityManager::getPInstance();


This piece of code initialises the level and gets a instance of the Manager.

Now This is the error I am facing
When I call the print function in the screen class it works fine .
it says that a entity has been registered and that the size of the vector is now 1. But this is the piece of code that seems to go wrong and creates a SEGMENTATION fault.

1
2
3
4
5
  vector<TEnt*>::iterator iter;
    for (iter=m_ppTEntList.begin();iter!=m_ppTEntList.end();iter++)
    {
            (*iter)->my_entity_paint();
    }


All I wanted to do was access every element of the vector and then call a function of each of the element.

Can any one point me to where I am wrong or any better way at all to how this can be done.


You are using -> incorrectly.

1
2
3
garbage->stuff;
//is the same as
(*garbage).stuff;


You are dereferencing, then using ->, which is dereferencing again, causing the seg fault.
No, the syntax is correct. (*iter) dereferences the iterator to get to the element. The element is a pointer, so the -> dereferences the pointer.

This code looks a little suspect:
1
2
3
4
void LevelEditor::Init_Level(){
EntityManager* Manager=EntityManager::getPInstance();
Manager->REGISTER_ENTITY_INSTANCE(a);
}


Where does a live? Is a on the stack somewhere?
Hi guys,
Thanks for your feed back.
I sort of found out the error. I was creating a temperory instance of my entities and pushing it into a pointer vector which is in the Entity manager which gave raise to hanging pointers and hence seg fauts..
I got round it ..
Cheers and thanks again
Topic archived. No new replies allowed.