Jump to content
  • Advertisement
Sign in to follow this  
kandie

Seg Fault: clueless over the cause!

This topic is 3494 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

As the title says, I'm receiving a segmentation fault when i'm executing this one certain method; however, this certain piece of code has been working fine for over 5 weeks, and I haven't changed the slightest bit. After some, eventually useless, tracking, I have found what's going on, but I couldn't figure out why it's happening. The problem: Within the method, if I try to access ANY class member, i get the seg fault; whether it is a pointer, an int, a static bool, it doesn't seem to matter. However, I tried to access my logger using its singleton; it worked fine. So, I'm only receiving the error trying to access the class members. I'll post the code for the class header, and the code for the faulty class method (createUnit()).

#ifndef H_CombatManager_H
#define H_CombatManager_H

#include "CombatScene.h"
#include "HeroFactory.h"
#include "UnitFactory.h"
#include "Spell.h"

using std::vector;
using std::queue;
using std::pair;
namespace Pixy
{
    class CombatManager
    {
        public:
            virtual ~CombatManager();
            static CombatManager* getSingletonPtr();

            /*! \brief
             *  Sets up Hero objects, their spells in Hand, and calls the CombatScene class to setup our scene.
             */
            void setupCombat();

            /*! \brief
             *  Game loop goes here; monitors the states of all units and Heroes in game, and signals events accordingly.
             */
            void updateCombat();

            /*! \brief
             * Creates a Pixy::Unit GameObject, and informs CombatScene to create an
             * Ogre::Entity and attach it to an Ogre::SceneNode.
             */
            void createUnit(int inIdOwner, int inIdModel) throw (std::bad_alloc);

            /*! \brief
             *  Destroys the Pixy::Unit with inIdEntity id, calls CombatScene to detach the Entity from the Scene.
             */
            void destroyEntity(Entity* inEntity) throw (std::out_of_range);

            /*! \brief
             *  Moves a Unit to a certain position based on its state.
             */
            void moveUnit(Pixy::Unit *inEntity, pUNITPOS inDestination);

            /*! \brief
             *  Ends the current turn of X player, where X is determined by the flag fHostTurn.
             */
            void endTurn();

            Pixy::Hero&	getHero (int inIdOwner);
            Pixy::Unit&	getUnit (int inIdOwner, int inIdUnit) throw (std::out_of_range);
            const std::vector<Unit*>&	getUnits (int inIdOwner);

            // ui event handlers
            bool 	onClick_castSpell (const CEGUI::EventArgs &args);
            bool 	onClick_endTurn (const CEGUI::EventArgs &args);
            bool    onClick_attack (const CEGUI::EventArgs &args);
            bool 	onClick_Block (const CEGUI::EventArgs &args);
            bool 	onClick_Pass (const CEGUI::EventArgs &args);
            bool 	onClick_endGame (const CEGUI::EventArgs &args);
            bool 	onClick_mainMenu (const CEGUI::EventArgs &args);

            void endGame(Hero* inLoser);

            bool emptyMoveQueue();


        protected:
            void init();
            int parseIdNodeFromName (Ogre::String inNodeName);

            Pixy::CombatScene   *mCombatScene;
            Pixy::PixyLogger    *mLog;
            HeroFactory         *mHeroFactory;
            UnitFactory         *mUnitFactory;

            // entity containers
            vector<Hero*> 	    mHeroes;
            vector<Unit*> 	    hUnits;
            vector<Unit*> 	    cUnits;

            queue< pair<Unit*, pUNITPOS> > mMoveQueue;
            int 	            nrHostUnits;
            int 	            nrClientUnits;

            static CombatManager* 	mCombatMgr;
            static bool 	        fInitialized;
            int ctrChargingUnits;
            int ctrBlockingUnits;

            int parseIdNodeFromName (Ogre::String inNodeName);
        private:
            CombatManager();
            CombatManager(const CombatManager &);
            CombatManager& operator= (const CombatManager &);
    };
}

#endif
I've commented out most of the method body, because it'll drop dead on the first line that tries to access a member as I said before, here it is :
    void CombatManager::createUnit(int inIdOwner, int inIdModel)
    throw (std::bad_alloc)
    {
        // create a Pixy::Unit
        LOG_H(__FUNCTION__);


        std::vector<Unit*>* mUnits;
        int* tmpNrUnits;


        // HERE -->
        pRACE tmpRace = mHeroes[inIdOwner]->getRace();


        /*std::cout<<"fetched hero race.\n";
        if (inIdOwner == ID_HOST)
        {
            mUnits = &hUnits;
            tmpNrUnits = &nrHostUnits;
        } else {
            mUnits = &cUnits;
            tmpNrUnits = &nrClientUnits;
        };

        /*std::cout<<"retrieving model from factory\n";
        mUnits->push_back ( mUnitFactory->createEntity((*tmpNrUnits)++, inIdModel, inIdOwner, EARTH) );

        // debug
        /*
        LOG("nrHostUnits = " + Ogre::StringConverter::toString(nrHostUnits));
        LOG("nrClientUnits = " + Ogre::StringConverter::toString(nrClientUnits));
        LOG("vector<hUnits> size = " + Ogre::StringConverter::toString(hUnits.size()));
        LOG("vector<cUnits> size = " + Ogre::StringConverter::toString(cUnits.size()));
        */

        /*
        mHeroes[inIdOwner]->attachUnit(mUnits->back());

        int idEntity = mUnits->back()->getIdEntity();

        Ogre::String meshPath =  (*mUnits).back()->getMeshPath();

        mCombatScene->attachToScene(mUnits->back());

        //LOG_F(__FUNCTION__);

        mUnits = NULL;
        tmpNrUnits = NULL;*/
    };
any clues? :D

Share this post


Link to post
Share on other sites
Advertisement
If accessing any member variable (or virtual function) causes a segfault, then your this pointer is invalid - usually from calling a method on a null or deleted pointer.

Share this post


Link to post
Share on other sites
wow I feel so dumb, the pointer calling the method was indeed invalid, I was calling that method via a Spell object which has a pointer to the CombatManager class singleton; but on a late night I apparently commented out that singleton call for some reason, thus invalidating the pointer!!

thanks alot for your comment, it helped
cheers
/k

Share this post


Link to post
Share on other sites
Quote:
You seem to be making use of exception specifications. This might be an interesting read for you.


Insightful article, thank you for recommending it; ironically though, I've read in numerous books that ES are far away from being frowned upon, much less considered as what the article deemed!

I'll keep it in mind, thanks again for sharing!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!