jac*_*ill -2 c++ segmentation-fault
我的程序有时只会得到一个Segmentation fault: 11,我无法弄清楚我的生活.我不太了解C++和指针领域,所以我应该寻找什么样的东西?
我知道它可能与我正在使用的一些函数指针有关.
我的问题是什么样的东西会产生分段错误?我拼命地迷失了,我已经查看了所有可能导致这种情况的代码.
我正在使用的调试器是lldb,它显示了此代码段中的错误:
void Player::update() {
// if there is a smooth animation waiting, do this one
if (queue_animation != NULL) {
// once current animation is done,
// switch it with the queue animation and make the queue NULL again
if (current_animation->Finished()) {
current_animation = queue_animation;
queue_animation = NULL;
}
}
current_animation->update(); // <-- debug says program halts on this line
game_object::update();
}
Run Code Online (Sandbox Code Playgroud)
current_animation并且queue_animation都是阶级的指针Animation.
另外需要注意的是,within Animation::update()是一个在构造函数中传递给Animation的函数指针.
如果你需要查看所有代码,那就在这里.
编辑:
我更改了代码以使用bool:
void Player::update() {
// if there is a smooth animation waiting, do this one
if (is_queue_animation) {
// once current animation is done,
// switch it with the queue animation and make the queue NULL again
if (current_animation->Finished()) {
current_animation = queue_animation;
is_queue_animation = false;
}
}
current_animation->update();
game_object::update();
}
Run Code Online (Sandbox Code Playgroud)
它没有任何帮助,因为我有时候仍会遇到Segmentation故障.
编辑2:
修改后的代码:
void Player::update() {
// if there is a smooth animation waiting, do this one
if (is_queue_animation) {
std::cout << "queue" << std::endl;
// once current animation is done,
// switch it with the queue animation and make the queue NULL again
if (current_animation->Finished()) {
if (queue_animation != NULL) // make sure this is never NULL
current_animation = queue_animation;
is_queue_animation = false;
}
}
current_animation->update();
game_object::update();
}
Run Code Online (Sandbox Code Playgroud)
只是为了看看这个函数何时输出而没有任何用户输入.每当我遇到分段故障时,这将在故障之前输出两次.这是我的调试输出:
* thread #1: tid = 0x1421bd4, 0x0000000000000000, queue = 'com.apple.main-thread, stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
frame #0: 0x0000000000000000
error: memory read failed for 0x0
| 归档时间: |
|
| 查看次数: |
1724 次 |
| 最近记录: |