我尝试NULL指针时出现分段错误

时间:2013-10-21 19:02:35

标签: c++ pointers segmentation-fault

当代码重新为NULL指针时,我在调试器中出现了分段错误。

此功能执行错误:

void Player::nullActive()
{
    activeCharacter = NULL;
}

activeCharacter只是Character类的指针。

此外,这是播放器类:

class Player
{
    public:
        Player(unsigned int accID, std::string aID, bool isBanned); //constructor, allow only create object id-s one time
        ~Player();

        std::string getAtheriusID(); //return account id

        unsigned int getAccountID(); //return atherius id

        boost::ptr_vector<Character> characters;

        bool isBanned();

        bool hasActiveCharacter();

        void nullActive();

        void setActiveCharacter(Character * character);

        void setConnection(CSConnection * con);
        CSConnection * getConnection();

        Character * getActiveCharacter();

        unsigned int atheriusCoins;
    protected:
        unsigned int accountID; //account unique id
        std::string atheriusID; //account name / atherius id
        bool banned;

    private:
        CSConnection * connection;
        Character * activeCharacter = NULL;
};

调试器的输出:

  

编程接收信号SIGSEGV,分段故障。在   F:\ EternalHeroes \ server \ src \ game \ src \ Player.cpp:36继续......   程序接收信号SIGSEGV,分段故障。在   F:\ EternalHeroes \ server \ src \ game \ src \ Player.cpp:36继续......   [Inferior 1(进程11552)退出,代码为030000000005] Debugger   完成状态0

1 个答案:

答案 0 :(得分:3)

将它从评论转移到答案,因为它不太可能会有任何其他的;)

尽管我可以通过这么多信息猜测,但activeCharacter是一个成员变量,当执行此函数时,该指针会被损坏/无效。