whoami
whoami

Reputation: 3

Receiving segmentation fault in C++ code

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.

Upvotes: 0

Views: 968

Answers (3)

Thomas Matthews
Thomas Matthews

Reputation: 57749

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.

Upvotes: 0

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.

Upvotes: 0

Chris Dodd
Chris Dodd

Reputation: 126526

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.

Upvotes: 2

Related Questions