SDL Init slows the program while code is correct a

2019-08-22 10:32发布

问题:

I have a SDL2 program that works perfectly. But from time to time, when I compile, execute and close several times my program, it slows drastically when calling SDL_Init(). I can still see my console, but it is just very slow and needs 5 minutes to resume.

It happens when I use "SDL_INIT_EVERYTHING" as a flag. But after 5 minutes, it runs the program smoothly. But when I use "SDL_INIT_VIDEO | SDL_INIT_EVENTS" as a flag, the program freezes when calling SDL_PollEvent() instead of SDL_Init().

I use a development lib of SDL_2.0.9 dynamically included. I repeat that this program has already been running perfectly and breaks itself while no mod is applied to the code.

Actually, the code is working. I don't think it is the problem here.

int main(int argc, char** argv) {

    srand(_getpid());


    if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
        printf("ERR SDL_INIT\n");
        SDL_Quit();

        system("PAUSE");
        return EXIT_FAILURE;
    }

    //* Basically the rest of my program.

}

When running the program step by step, the program clearly slows when calling SDL_Init().

Here are some pics of the problem :

It should actually initialize SDL fast, as it does most of the time, and then continue executing the program without blocking calling SDL_PollEvent().

I can more or less solve the problem by rebooting my pc but it occurs quite sometimes.

Tell me if other information I didn't think of is needed.

EDIT :

As suggested, I tried writing a minimal verifiable code to confirm the problem doesn't come from the code.

#include <stdlib.h>
#include <stdio.h>
#include "SDL.h"

int main(int argc, char** argv) {

if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    printf("ERR SDL_INIT\n");
    SDL_Quit();

    system("PAUSE");
    return EXIT_FAILURE;
}

SDL_Window* window = NULL;
window = SDL_CreateWindow("WindowName", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);

if (!window) {  //test si la fenetre existe bien
    printf("ERR SDL_WINDOW\n");
    SDL_Quit();

    system("PAUSE");
    return EXIT_FAILURE;
}

SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);


SDL_bool quit = SDL_FALSE;
SDL_Event e;

while (!quit) {

    SDL_SetRenderDrawColor(renderer, 255, 0, 255, SDL_ALPHA_OPAQUE);
    SDL_RenderClear(renderer);

    while (SDL_PollEvent(&e)) {

        if (e.window.event == SDL_WINDOWEVENT_CLOSE)
            quit = SDL_TRUE;

        switch (e.type) {

        case SDL_KEYDOWN:
            switch (e.key.keysym.sym)
            {
            case SDLK_ESCAPE:
                quit = SDL_TRUE;
                break;
            default:
                break;
            }
        }

    }

    SDL_RenderPresent(renderer);


}

SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();

return EXIT_SUCCESS;
}

And now, as SDL_Init() is called using SDL_INIT_VIDEO, the program freezes at the call of SDL_PollEvent(). The VS console returns me those lines at the exact moment of the freeze :

'MinimalVerifiableExample.exe' (Win32) : Chargé 'C:\Windows\SysWOW64\hid.dll'. Impossible de trouver ou d'ouvrir le fichier PDB.
'MinimalVerifiableExample.exe' (Win32) : Chargé 'C:\Windows\SysWOW64\devobj.dll'. Impossible de trouver ou d'ouvrir le fichier PDB.

I don't know if it is related tho.