SDL2 Programs stop responding

This is a problem I have been having with every program I write since I started using SDL 2. Whenever I compile my code and run my program, everything works perfectly fine until at some point (usually after 3-8 minutes of running), the program will stop responding completely and I will have to exit out of the console to close it. The code I believe is relevant is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
void MainLoop()
{
    InitLoop();
    while ( !QuitMain )
    {
        frametime = SDL_GetTicks() - start;
        if ( frametime < 1000.f/FPS ) SDL_Delay( 1000.f/FPS - ( SDL_GetTicks() - start ) );
        start = SDL_GetTicks();

        HandleInput();
        Display();
        Update();
        PrintSDLError();
        PrintGLError();

        FPSTracker.Average = (FPSTracker.Frame*FPSTracker.Average + (1000.f/frametime))/float(FPSTracker.Frame+1);
        FPSTracker.Frame++;
    }
}

void HandleInput()
{
    while ( SDL_PollEvent(&Event) )
    {
        int x,y;
        SDL_GetMouseState(&x,&y);

        switch( Event.type )
        {
        case SDL_QUIT:
            Close();
            break;

        case SDL_KEYDOWN:
            KeyPress(Event.key.keysym.sym, x, y);
            break;

        case SDL_KEYUP:
            KeyRelease(Event.key.keysym.sym, x, y);
            break;

        case SDL_MOUSEBUTTONDOWN:
            MousePress(Event.button.button, x, y);
            break;

        case SDL_MOUSEBUTTONUP:
            MouseRelease(Event.button.button, x, y);
            break;

        case SDL_MOUSEMOTION:
            if ( !( Event.motion.state & SDL_BUTTON_LMASK or Event.motion.state & SDL_BUTTON_RMASK ) )
                PassiveMouseMotion(x,y);
            else
                ActiveMouseMotion(x,y);
            break;

        case SDL_MOUSEWHEEL:
            MouseWheel(Sign( Event.wheel.y ), x, y);
            break;

        case SDL_WINDOWEVENT:
            if ( Event.window.type == SDL_WINDOWEVENT_RESIZED )
                Resize( Event.window.data1, Event.window.data2 );
            break;

        case SDL_SYSWMEVENT:
            {
                UINT Message = Event.syswm.msg->msg.win.msg;
                WPARAM wParam = Event.syswm.msg->msg.win.wParam;
                LPARAM lParam = Event.syswm.msg->msg.win.lParam;
                SysWM(Message, wParam, lParam);
            }
            break;
        }
    }
}


If I change SDL_PollEvent(&Event) to SDL_WaitEventTimeout(&Event,100), then the problem goes away (I did that and had the program running for about an hour without it stop responding before I decided that it solved the problem), so I believe that the problem has something to do with event handling. Also, it might be noteworthy to mention that when I use SDL_WaitEventTimeout with the second parameter being a small number (because 100 milliseconds is a long time to wait and makes the program run at like 8 FPS), the problem returns.
I think the problem is around line 7.

When you calculate SDL_GetTicks() - start on line 7 you might get a value that is larger than frametime because some additional milliseconds might have passed since you calculated frametime on the line above. If you are unlucky you get a value that is larger than 1000.f/FPS so you end up with a negative value. SDL_Delay takes an unsigned value as argument so when the negative value is converted into an unsigned value it becomes a huge value, so large that you probably have to wait over a month before SDL_Delay returns.
That seems to have been the problem. Thank you. I can't believe I missed something so obvious
Topic archived. No new replies allowed.