1

I'm having a problem regarding memory. I will show you both my Valgrind output and source code. I really can't figure out what I'm doing wrong or how to fix the errors Valgrind is giving me.

Valgrind finds the following problems in my program:

==6040== Conditional jump or move depends on uninitialised value(s)
==6040==    at 0xC2F91EA: ??? (in /usr/lib/nvidia-304-updates/libnvidia-glcore.so.304.125)
==6040==    by 0xC2F96EF: ??? (in /usr/lib/nvidia-304-updates/libnvidia-glcore.so.304.125)
==6040==    by 0xE01BFFF: ??? (in /dev/nvidia0)
==6040==  Uninitialised value was created by a stack allocation
==6040==    at 0x4E8F3D0: ??? (in /usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.2.0)
==6040== 
==6040== Conditional jump or move depends on uninitialised value(s)
==6040==    at 0xC6AB97D: ??? (in /usr/lib/nvidia-304-updates/libnvidia-glcore.so.304.125)
==6040==  Uninitialised value was created by a stack allocation
==6040==    at 0x4E8F3D0: ??? (in /usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.2.0)
==6040== 
==6040== Conditional jump or move depends on uninitialised value(s)
==6040==    at 0xC6AB986: ??? (in /usr/lib/nvidia-304-updates/libnvidia-glcore.so.304.125)
==6040==  Uninitialised value was created by a stack allocation
==6040==    at 0x4E8F3D0: ??? (in /usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.2.0)
==6040== 
==6040== Conditional jump or move depends on uninitialised value(s)
==6040==    at 0xC6ABA28: ??? (in /usr/lib/nvidia-304-updates/libnvidia-glcore.so.304.125)
==6040==  Uninitialised value was created by a stack allocation
==6040==    at 0x4E8F3D0: ??? (in /usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.2.0)
==6040== 
==6040== 
==6040== HEAP SUMMARY:
==6040==     in use at exit: 35,600 bytes in 431 blocks
==6040==   total heap usage: 11,048 allocs, 10,617 frees, 14,074,851 bytes allocated
==6040== 
==6040== LEAK SUMMARY:
==6040==    definitely lost: 60 bytes in 7 blocks
==6040==    indirectly lost: 0 bytes in 0 blocks
==6040==      possibly lost: 0 bytes in 0 blocks
==6040==    still reachable: 35,540 bytes in 424 blocks
==6040==         suppressed: 0 bytes in 0 blocks
==6040== Rerun with --leak-check=full to see details of leaked memory
==6040== 
==6040== For counts of detected and suppressed errors, rerun with: -v
==6040== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 6 from 1)

This is my source code:

#include <stdio.h>
#include <SDL2/SDL.h>

typedef struct gameState
{
    SDL_Window* window;
    SDL_Renderer* renderer;
}
gameState;

void exitGame(gameState* game);

int main(void)
{
    gameState game = {NULL};

    // initialize SDL library
    if (SDL_Init(SDL_INIT_VIDEO) != 0)
    {
        fprintf(stderr, "Initialization error: %s", SDL_GetError());
        SDL_Quit();
        return 1;
    }

    // create game window
    game.window = SDL_CreateWindow(
        "Game",
        SDL_WINDOWPOS_UNDEFINED,
        SDL_WINDOWPOS_UNDEFINED,
        160,
        144,
        0
    );

    if (game.window == NULL)
    {
        fprintf(stderr, "Window error: %s\n", SDL_GetError());
        SDL_Quit;
        return 1;
    }

    game.renderer = SDL_CreateRenderer(game.window, -1, 0);

    if (game.renderer == NULL)
    {
        fprintf(stderr, "Renderer error: %s\n", SDL_GetError());
        SDL_DestroyWindow(game.window);
        SDL_Quit();
        return 1;
    }

    exitGame(&game);
    return 0;
}

void exitGame(gameState* game)
{
    SDL_DestroyRenderer(game->renderer);
    SDL_DestroyWindow(game->window);
    SDL_Quit();
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
Mampoempan
  • 347
  • 1
  • 3
  • 8
  • 1
    It might be the case Valgrind complains about issues in the libraries, not your code. See http://stackoverflow.com/questions/3174468/is-it-possible-to-make-valgrind-ignore-certain-libraries and http://stackoverflow.com/questions/2375726/how-do-you-tell-valgrind-to-completely-suppress-a-particular-so-file But wait until someone here confirms your code is OK. – Karol S Aug 06 '15 at 18:51
  • Normally, you should get a stack trace for each error that reaches back to the code you wrote. Did you compile with debugging enabled (`-g` for both creating object files and linking)? Is there a debugging version of the SDL2 and NVIDIA libraries? Did you configure something to limit the depth of the callback stack that is reported (if so, undo it)? – Jonathan Leffler Aug 06 '15 at 19:44
  • I compiled with -ggdb, which I assume is sufficient? I don't know about debugging versions of SDL2 or NVIDIA libraries. I didn't configure anything to limit the depth of the callback stack. Hope this is useful info. (Compiled: clang-3.6 -ggdb -std=c99 -Wall -Werror main.c -lSDL2 -o main) – Mampoempan Aug 06 '15 at 19:46

0 Answers0