A question on Pointers.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: A question on Pointers.

Hybrid View

  1. #1
    Join Date
    Mar 2009
    Posts
    20

    A question on Pointers.

    I am not pretty sure if I should post this here.

    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
    Code:
            /*! 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
    Code:
    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.
    Code:
    m_ppTEntList.push_back(&instance);
    and my screen class has this
    Code:
    //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.

    Code:
      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.

  2. #2
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: A question on Pointers.

    Quote Originally Posted by sundar0206 View Post
    The REGISTER_ENTITY_INSTANCE(a) pushes the address of the instance onto a vector.
    Code:
    m_ppTEntList.push_back(&instance);
    Just guessing here. Are you shure instance doesn't go out of scope after that.
    Kurt

  3. #3
    Join Date
    Mar 2009
    Posts
    20

    Re: A question on Pointers.

    I am not exactly sure as to what you mean by going out of scope!!
    But it seems to register it because when I test the size of the vector from the screen class I dont get a error and it says the entity has been properly registerd

  4. #4
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: A question on Pointers.

    i was referring to sth like this

    Code:
    void createInstance() {
         entity a;
         REGISTER_ENTITY_INSTANCE(a);
    }
    In such a case the entity a would go out of scope after return from createInstance() and you would have added a dangling pointer to the vector.
    Kurt

  5. #5
    Join Date
    Mar 2009
    Posts
    20

    Re: A question on Pointers.

    I seem to have gone wrong with the pointing but I think what you said is probably the cause of the problem.

    This is from a a level editor class that I use to register the entities on to the manager.

    Code:
    void LevelManager::InitLevel(){
        //A Instance of Entitymanager to which all the instances are registered
        vec3d pos;
        pos.Initialize(10,0,0);
        TEnt a(pos);
        EntityManager* Manager=EntityManager::getPInstance();
        Manager->REGISTER_ENTITY_INSTANCE(a);
    }
    and this is from the screen class
    Code:
        LevelEditor* Levels=new LevelEditor;
        Levels->Init_Level();
        EntityManager* EManager=EntityManager::getPInstance();
        EManager->drawEntities();
    The above code throws up errors and causes segmentation faults.

    But if i totally take off the level editor and put everything into the screen class like
    Code:
    EntityManager* EManager=EntityManager::getPInstance();
    vec3d pos;
    pos.Initialize(3,0,3);
    TEnt a(pos);
    EManager->REGISTER_ENTITY_INSTANCE(a);
    EManager->drawEntities();
    This doesn't......

    Is this due to the error which was told in the last post


    But I want to have a seperate Level Editor class which I can integrate with a GUI.

  6. #6
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: A question on Pointers.

    You will have to dynamically create the TEnt in InitLevel().
    If you would use a vector of smart pointers to register the entities you don't have to worry to delete the entities.
    Kurt

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center