在 C++ 中接收分段错误代码

发布于 2024-12-12 06:08:51 字数 1610 浏览 0 评论 0 原文

我正在使用 C++、code::blocks(启用了警告并进行了检查以确保)和 SFML。

我已经尝试自己寻找错误,但我真的找不到问题所在。我知道段错误意味着什么,但这确实让我难住了。我是一个初学者,我可以补充一下,但我正在快速学习。

我有一个基本的主类 void Engine,它有一个方法 void RenderFrame,可以渲染应用程序的每一帧。在上述方法中,我有这段代码,应该将所有图块绘制到渲染窗口上:

Tile* tile;    
for (short y = 0; y < mapHeight; ++y) {
    for (short x = 0; x < mapWidth; ++x) {
        tile    = currentLevel -> GetTile(x, y);

        if (tile) {
            tile -> Draw((x * tileSize), (y * tileSize), Wnd);
        }
    }
}

GetTile 方法应该从 std::vector 中返回一个图块。 >

Draw 方法仅执行以下操作:

void Tile::Draw(int x, int y, sf::RenderWindow *pWnd) {
    sprite.SetPosition(x, y);
    pWnd -> Draw(sprite);
}

应用程序编译得很好,但在调用 sprite.SetPosition(x, y); 后立即崩溃

这是调试器的完整调用堆栈:

#0 68701829 sf::Drawable::SetPosition(float, float) () (D:\Coding\C++\sfml\bin\debug\sfml-graphics.dll:??)
#1 004021F9 Tile::Draw(this=0x76ab8cd5, x=0, y=0, pWnd=0x3e3310) (D:\Coding\C++\sfml\include\Tile.cpp:12)
#2 00401D7E Engine::RenderFrame(this=0x3e3298) (D:\Coding\C++\sfml\include\Engine.cpp:106)
#3 00401B29 Engine::MainLoop(this=0x3e3298) (D:\Coding\C++\sfml\include\Engine.cpp:63)
#4 00401E27 _fu0___ZTIPKc(this=0x3e3298) (D:\Coding\C++\sfml\include\Engine.cpp:119)
#5 004022D6 main() (D:\Coding\C++\sfml\Main.cpp:8)

我希望这些信息足以继续下去,并提前致谢。

编辑:哦,这是来自调试器的输出。 程序收到信号 SIGSEGV,分段错误。 在 sf::Drawable::SetPosition(float, float) () 中 没有提供有关该问题的更多信息。

I'm using C++, code::blocks (with warnings enabled and I checked to make sure) and SFML.

I've tried hunting the bug down myself but I really can't find what's wrong. I know what a segfault means but this one really has me stumped. I'm a beginner may I add but am quickly learning.

I've got a basic main class void Engine that has a method void RenderFrame that renders every frame of the application. Inside said method I have this code which is supposed to draw all the tiles onto the renderwindow:

Tile* tile;    
for (short y = 0; y < mapHeight; ++y) {
    for (short x = 0; x < mapWidth; ++x) {
        tile    = currentLevel -> GetTile(x, y);

        if (tile) {
            tile -> Draw((x * tileSize), (y * tileSize), Wnd);
        }
    }
}

The GetTile method is supposed to return a tile from within a std::vector<std::vector<Tile *> >

The Draw method only does this:

void Tile::Draw(int x, int y, sf::RenderWindow *pWnd) {
    sprite.SetPosition(x, y);
    pWnd -> Draw(sprite);
}

The application compiles just fine, but it crashes right after calling sprite.SetPosition(x, y);

This is the full call stack from the debugger:

#0 68701829 sf::Drawable::SetPosition(float, float) () (D:\Coding\C++\sfml\bin\debug\sfml-graphics.dll:??)
#1 004021F9 Tile::Draw(this=0x76ab8cd5, x=0, y=0, pWnd=0x3e3310) (D:\Coding\C++\sfml\include\Tile.cpp:12)
#2 00401D7E Engine::RenderFrame(this=0x3e3298) (D:\Coding\C++\sfml\include\Engine.cpp:106)
#3 00401B29 Engine::MainLoop(this=0x3e3298) (D:\Coding\C++\sfml\include\Engine.cpp:63)
#4 00401E27 _fu0___ZTIPKc(this=0x3e3298) (D:\Coding\C++\sfml\include\Engine.cpp:119)
#5 004022D6 main() (D:\Coding\C++\sfml\Main.cpp:8)

I hope this is enough information to go on, and thanks in advance.

Edit: Oh, and this is from the debugger output. Program received signal SIGSEGV, Segmentation fault.
In sf::Drawable::SetPosition(float, float) ()

Doesn't give much more information about the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一束光,穿透我孤独的魂 2024-12-19 06:08:51

回溯中的这一行看起来很可疑:

#1 004021F9 Tile::Draw(this=0x76ab8cd5, x=0, y=0, pWnd=0x3e3310) 

这似乎与您的 Tile::Draw 函数相对应,但 this 指针未对齐,这表明它不是有效的指针。所以也许你的 std::vector > 已被某种方式损坏。

This line in the backtrace looks suspicious:

#1 004021F9 Tile::Draw(this=0x76ab8cd5, x=0, y=0, pWnd=0x3e3310) 

This seems to correspond to your Tile::Draw function, except the this pointer is misaligned, which suggests that it's not a valid pointer. So perhaps your std::vector<std::vector<Tile *> > has been corrupted somehow.

一笑百媚生 2024-12-19 06:08:51

最可能的解释是 currentLevel->GetTile(x,y) 返回的指针无效。这可能是因为它没有正确初始化(NUL 或有效的分配对象)或者因为它引用的对象已被销毁。两者都会解释 sprite 不是一个有效的对象,并且在该对象上调用 SetPosition 将传递一个无效的 this 指针,该指针将触发 SIGSEGV。

The most probable explanation is that the pointer returned by currentLevel->GetTile(x,y) is not valid. This could be because it was not properly initialized (to either NUL or a valid allocated object) or because the object to which it refers has been destroyed. Both would explain that sprite is not a valid object and calling SetPosition on that object will pass an invalid this pointer that would trigger the SIGSEGV.

极致的悲 2024-12-19 06:08:51

分段错误的一般原因是取消引用空指针或无效指针。
1.检查GetTile(),是否是这个原因造成的?
2. 在取消引用之前检查currentLevel

A general cause of segmentation faults is dereferencing a null or invalid pointer.
1. Check GetTile(), is this causing the fault?
2. Check currentLevel before dereferencing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文