【发布时间】:2013-10-21 19:02:34
【问题描述】:
当代码 rey 指向 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 (process 11552) exited with code 030000000005] 调试器 以状态 0 结束
【问题讨论】:
-
没有足够的信息,
Character类是什么?你是说char *activeCharacter ;吗? -
如果没有其他未定义的行为发生,就不可能给你一个段错误(除非
operator=为该类型做了一些愚蠢的事情)。 -
据我所知,
activeCharacter是一个成员变量,在执行此函数时this指针已损坏/无效。 -
这可能是在你将它设为空指针之后发生的事情。
-
添加了 Player 类和调试器输出。
标签: c++ pointers segmentation-fault