Jump to content
  • Advertisement
Sign in to follow this  
ElGeeko7

SDL2: Some functions crash the program

This topic is 471 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys,

So I've been messing with SDL this weekend, and found some great tutorials on YouTube, but I've been running into a bizarre problem that Google has been no help in solving.  Some functions, for no good reason (there is a reason, but it has been obfuscated to oblivion), will crash my program.  Here's my code so far:

#include <iostream>
#include "SDL.h"

#define WINDOW_WIDTH 800;
#define WINDOW_HEIGHT 600
#define FPS 60

void DrawChessBoard(SDL_Renderer *renderer)
{
    int row = 0,column = 0,x = 0;
    SDL_Rect rect, screen_size;

    /* Get the Size of drawing surface */
    SDL_RenderGetViewport(renderer, &screen_size);

    for ( ; row < 8; row++) {
        column = row % 2;
        x = column;
        for ( ; column < 4 + (row % 2); column++) {
            SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0xFF);

            rect.w = screen_size.w/8;
            rect.h = screen_size.h/8;
            rect.x = x * rect.w;
            rect.y = row * rect.h;
            x = x + 2;
            SDL_RenderFillRect(renderer, &rect);
        }
    }
}



int WinMain()   // SDL needs WinMain, for some reason, not main
{
    // Fire up SDL
    SDL_Init(SDL_INIT_EVERYTHING);

    // Create the window
    SDL_Window *window = SDL_CreateWindow("You Geek!", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_RESIZABLE);
    if (window == NULL)
    {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Window creation fail : %s\n",SDL_GetError());
    }

    // Declare variables
    SDL_Event *event;
    bool running = true;
    SDL_Renderer *renderer;
    SDL_Surface *surface = SDL_GetWindowSurface(window);
    renderer = SDL_CreateSoftwareRenderer(surface);
    if (!renderer)
    {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Render creation for surface fail : %s\n",SDL_GetError());
        return 1;
    }

    // Main loop
    while (running)
    {
        // Draw stuff
        DrawChessBoard(renderer);    // This worked, ironically
        SDL_UpdateWindowSurface(window);

        /* Clear the rendering surface with the specified color */
        SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
        SDL_RenderClear(renderer);

        // Handle events
        while (SDL_PollEvent(event))
        {
            if (event->type == SDL_QUIT)
            {
                running = false;
                break;
            }
        }
    }

    // Clean up
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}

And some of the functions that cause the epic meltdown are:

SDL_LoadBMP

SDL_GetTicks

SDL_GetWindowSurface (though that one has stopped after a few hours of tinkering, reason still unknown)

And here's what my debugger has to say:

#0 0x768ba9f2    RaiseException() (C:\Windows\SysWOW64\KernelBase.dll:??)
#1 0x6c81b14c    SDL_LogCritical() (C:\Media\Code\Projects\EXPERI~1\SDLTES~1\bin\Debug\SDL2.dll:??)
#2 0x406d1388    ?? () (??:??)
#3 ??    ?? () (??:??)
So apparently, whatever is happening has even stumped the debugger (lol).  I've never seen a bunch of question marks like that, but I'm guessing they're bad lol.  Seriously...?

Anyway, here are some other details:

My IDE is CodeBlocks with MinGW.

My OS is Windows 10 Anniversary Edition, 64-bit

I'm using SDL 2.0, and I've had to use the 32-bit version of the DLL because that seems to be how MinGW wants it (or so said Google when I was battling a different epic crash problem - using the 32-bit DLL allowed my program to run correctly - could that be what's foobarring every other function I try?).

I've been all over the documentation, their Bugzilla page, and lots and lots of Googlefishing, but no dice.  Is SDL 2.0 just unstable or something?  lol idk but I'm seriously considering trying Unreal again (though getting that to startup has been a way bigger struggle lol - seems the big C++ frameworks like to pile on the challenge-factor for us Windows guys lol, but anywayz).  Obviously, I'm stuck outta luck here, so I'd sure appreciate any info on what this hair-jerker of a bug could be.  tyvm. :)

Share this post


Link to post
Share on other sites
Advertisement

Googled with "sdl2 mingw raiseexception" and it says that you should add this line before any calls to SDL: 

SDL_SetHint(SDL_HINT_WINDOWS_DISABLE_THREAD_NAMING, "1");

Share this post


Link to post
Share on other sites

You're polling for events and writing them into uninitialised memory. You're lucky it ever gets past the first frame. Turn your warning level up to maximum because the compiler should probably warn you about that. Then start passing the address of a real SDL_Event object into SDL_PollEvent. (Docs with example here.)

Share this post


Link to post
Share on other sites

Wow, thanks for the quick replies!

In the end, it was Kylotan's answer that solved the problem.  What you said makes perfect sense, and so does the example in the docs.  idk why I didn't see that, but I should've guessed it when the program returned 0xC00005 (may be off a zero - it's the exit code that roughly translates to "I can't dereference a null pointer, you geek!"). :D

But all joking aside, thanks for catching that!  All those question marks in the stack trace, and no useful info from Google, had made it seem infinitely more complicated than it really was.  Now I can get back to building stuff. :)

Share this post


Link to post
Share on other sites

returned 0xC00005 (may be off a zero - it's the exit code that roughly translates to "I can't dereference a null pointer, you geek!").


Nullptr is considering yourself lucky. Uninitialized variables don't default to zero, though in debug it can do so for you. Also, warnings as errors is something I'd strongly recommend as well

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!