[英]Receiving segmentation fault in C++ code
I'm using C++, code::blocks (with warnings enabled and I checked to make sure) and SFML. 我正在使用C ++,code :: blocks(启用了警告,我检查确认)和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. 我有一个基本的主类void Engine
,它有一个方法void RenderFrame
,它渲染应用程序的每一帧。 Inside said method I have this code which is supposed to draw all the tiles onto the renderwindow: 在所述方法中我有这个代码,它应该将所有的tile绘制到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 *> >
GetTile方法应该从std::vector<std::vector<Tile *> >
返回一个tile
The Draw method only does this: Draw方法只能这样做:
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);
应用程序编译得很好,但在调用sprite.SetPosition(x, y);
后它立即崩溃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) ()
Program received signal SIGSEGV, Segmentation fault. In sf::Drawable::SetPosition(float, float) ()
Doesn't give much more information about the problem. Program received signal SIGSEGV, Segmentation fault. In sf::Drawable::SetPosition(float, float) ()
中没有提供有关该问题的更多信息。
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. 这似乎对应于你的Tile :: Draw函数,除了this
指针未对齐,这表明它不是一个有效的指针。 So perhaps your std::vector<std::vector<Tile *> >
has been corrupted somehow. 所以也许你的std::vector<std::vector<Tile *> >
已经以某种方式被破坏了。
The most probable explanation is that the pointer returned by currentLevel->GetTile(x,y)
is not valid. 最可能的解释是currentLevel->GetTile(x,y)
返回的指针无效。 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. 这可能是因为它未正确初始化(对NUL或有效的已分配对象)或因为它所引用的对象已被销毁。 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. 两者都会解释sprite
不是有效对象,并且在该对象上调用SetPosition
将传递一个无效的指针, this
指针将触发SIGSEGV。
A general cause of segmentation faults is dereferencing a null or invalid pointer. 分段错误的一般原因是取消引用空指针或无效指针。
1. Check GetTile(), is this causing the fault? 1.检查GetTile(),这是否导致故障?
2. Check currentLevel
before dereferencing. 2.在解除引用之前检查currentLevel
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.