You can help CodeWalrus stay online by donating here. | New CodeWalrus | Old (dark mode) | Old (light) | Discord server

SDL2 on Linux Ubuntu - Seg Fault Error

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

0
b/General Help publicado por u/123outerme September 18, 2017, 07:52:41 PM
When I run the code below inside of one of my functions called drawTile() (inside of another one called drawTilemap() ), I get a segfault. On Windows, nothing happens, and the code works fine, yet on my Linux VM (64-bit/Ubuntu, if that helps), I get a segmentation fault.

SDL_RenderCopyEx(mainRenderer, tilesetTexture, &rect1, &rect2, 0, &center, flip);

where mainRenderer = the global SDL_Renderer variable,
tilesetTexture = the global SDL_Texture that renders the full tileset (or individual tiles, in this case),
rect1 = a local SDL_Rect variable that specifies the tilesetTexture clip rectangle,
rect2 = a local SDL_Rect variable that specifies where on the screen the tile is to be rendered to,
center = a local SDL_Point variable that specifies the centerpoint of the rotating operations,
and flip = a local SDL_RenderFlip parameter that specifies which way(s) to flip the drawn image.

Like I said, on Windows, when I build and run, it runs perfectly. On Linux, when I redownload the library files (just to be safe), build, and run, it always segfaults on this line. I backtraced the issue in GDB, and here's what it told me:

#0  0x00007ffff7b1b21c in ?? () from /usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0
#1  0x00007ffff7b1b5fb in ?? () from /usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0
#2  0x000000000000b05f in drawTile (id=0, xCoord=0, yCoord=0, width=48, flip=SDL_FLIP_NONE)
#3  0x000000000040af6c in drawTilemap (startX=0, startY=0, endX=20, endY=15, updateScreen=0)
<my notes: There are 6 in total it displays, but all of them expand out to my main(), so nothing I didn't know past this.>
<As well, drawTile() is the function that calls SDL_RenderCopyEx(). The line it breaks on is the line that calls SDL_RenderCopyEx.>

I went through and searched all my variables for improper pointer initialization, passing bad arguments, even that the clipping bounds in rect1 weren't faulty, everything, but I found nothing. What's strange is that, I mentioned how I ran this line inside of a function inside another function; when I run drawTile() (which a call for is nested in drawTilemap(), naturally) by itself, it doesn't segfault. What's even stranger, is that when I navigated to either of the frames that say "?? ()" and type "list", it gives me random lines from either of my C sources.

Here's a larger look at my source:

From myInclude.c:

void drawTilemap(int startX, int startY, int endX, int endY, bool updateScreen)
{
    for(int dy = startY; dy < endY; dy++)
        for(int dx = startX; dx < endX; dx++)
            drawTile(tilemap[dy][dx], dx * TILE_SIZE, dy * TILE_SIZE, TILE_SIZE, SDL_FLIP_NONE);
    if (updateScreen)
        SDL_RenderPresent(mainRenderer);
}

void drawTile(int id, int xCoord, int yCoord, int width, SDL_RendererFlip flip)
{
    SDL_Rect rect1 = {.x = (id / 8) * width, .y = (id % 8) * width, .w = width, .h = width};
    SDL_Rect rect2 = {.x = xCoord, .y = yCoord, .w = width, .h = width};
    SDL_Point center = {.x = width / 2, .y = width / 2};
    SDL_RenderCopyEx(mainRenderer, tilesetTexture, &rect1, &rect2, 0, &center, flip);
}

and from main.c:

int mainLoop(player* playerSprite)
{
    //... setting up other, unrelated variables
    //on Linux, seg fault after calling drawTilemap
    drawTilemap(0, 0, WIDTH_IN_TILES, HEIGHT_IN_TILES, false);
    //WIDTH_IN_TILES is defined as 20, and HEIGHT_IN_TILES is defined as 15
    //... rest of code that isn't run because of the segfault
}

(The body of this message came from my StackOverflow question here, if you want to take a look at that as well)
Inicia sesión o crea una cuenta para dejar un comentario
u/gameblabla October 10, 2017, 08:14:17 PM
Btw guys, i looked at the error some days ago with him and the crashes is very mysterious.
However, the game code was fundamentally flawed because he is not clearing & updating graphics properly.
This needs more investigation and more printf magic, because not even GDB can't catch the error. (so probably a framebuffer issue)
u/123outerme October 10, 2017, 08:22:56 PM
Quote from: gameblabla on October 10, 2017, 08:14:17 PM
Btw guys, i looked at the error some days ago with him and the crashes is very mysterious.
However, the game code was fundamentally flawed because he is not clearing & updating graphics properly.
This needs more investigation and more printf magic, because not even GDB can't catch the error. (so probably a framebuffer issue)
Yep, however now I clear and update properly, so that definitely can't be the cause. I think it's because somehow my tilemap isn't loading correctly. There could be an issue like it's in a .bin file, so the mapdata (which is just in plaintext) is corrupted/read in the wrong format, causing a Seg Fault when I try to draw it. It can't be the drawTile function itself, since it works when displaying the cursor in the menus.
u/gameblabla October 10, 2017, 08:38:57 PM
So yeah, while i couldn't find the reason for the flickering at the menu screen,
i found the reason why it was crashing in the menu.
This is because the code for parsing the map is... well flawed i mean take a look :
for(int dy = 0; dy < y; dy++)
{
for(int dx = 0; dx < x; dx++)
*(array + dx + dy * x) = sameArray[dy][dx];
}

Playing around with Pointers, huh ? (< _<)
What is sameArray anyway ?

Here's the function btw :
void loadMapFile(char* filePath, int* array[], const int lineNum, const int y, const int x)

Surprisingly, this works on Windows lmao.
u/123outerme October 10, 2017, 09:12:01 PM
Quote from: gameblabla on October 10, 2017, 08:38:57 PM
So yeah, while i couldn't find the reason for the flickering at the menu screen,
i found the reason why it was crashing in the menu.
This is because the code for parsing the map is... well flawed i mean take a look :
for(int dy = 0; dy < y; dy++)
{
for(int dx = 0; dx < x; dx++)
*(array + dx + dy * x) = sameArray[dy][dx];
}

Playing around with Pointers, huh ? (< _<)
What is sameArray anyway ?

Here's the function btw :
void loadMapFile(char* filePath, int* array[], const int lineNum, const int y, const int x)

Surprisingly, this works on Windows lmao.
Yeah I kinda botched it together :P sameArray is the array that will be transposed into the supplied int* array[]. I couldn't find a good way to pass a 2D array as a function though, so how would you recommend I do it? Or should I instead return it?
u/gameblabla October 10, 2017, 10:18:49 PM
QuoteYeah I kinda botched it together :P sameArray is the array that will be transposed into the supplied int* array[]. I couldn't find a good way to pass a 2D array as a function though, so how would you recommend I do it? Or should I instead return it?
Yeah, using return instead should be safer.
Start a Discussion

b/General Help

Need programming or hardware help? Need software installing or running support, or just help with technology in general? Then this is the place to ask in.

77
Topics
Explore Board
Website statistics


MyCalcs | Ticalc.org | Cemetech | Omnimaga | TI-Basic Developer | MaxCoderz | TI-Story | Casiocalc.org | Casiopeia | The Museum of HP Calculators | HPCalc.org | CnCalc.org | Music 2000 Community | TI Education | Casio Education | HP Calcs | NumWorks | SwissMicros | Sharp Calculators
Powered by EzPortal