libsdl-org / SDL

Simple Directmedia Layer
https://libsdl.org
zlib License
8.47k stars 1.59k forks source link

Assertion failure at SDL_SetKeyboardFocus #9747

Closed Dlinuigh closed 3 weeks ago

Dlinuigh commented 3 weeks ago

This WARN show every time when trying to quit SDL; And the SDL_keyboard.c file does not exist on my computer. Although this WARN can ignore, but I still feel uncomfortable.

SDL_QuitSubSystem(SDL_INIT_EVENTS);
SDL_QuitSubSystem(SDL_INIT_VIDEO);
for(auto f : font_lut){
  TTF_CloseFont(f.second);
}
TTF_Quit();
SDL_DestroyRenderer(render);
SDL_DestroyWindow(window);
SDL_Quit();

I think the problem may exist between this code. Because the break button tells me. I'm not sure if this is an SDL concern, but I think I might seek help here. Screenshot from 2024-05-10 15-37-39

Dlinuigh commented 3 weeks ago

Oh, I'm using SDL3 from main branch built on an Arch Linux Machine. The built date is about 19 April.

Sackzement commented 3 weeks ago

Does reordering the functions help?

for(auto f : font_lut){
  TTF_CloseFont(f.second);
}
TTF_Quit();
SDL_DestroyRenderer(render);
SDL_DestroyWindow(window);
SDL_QuitSubSystem(SDL_INIT_EVENTS); // <---
SDL_QuitSubSystem(SDL_INIT_VIDEO);  // <---
SDL_Quit();
slouken commented 3 weeks ago

Do you have a simple example we can run here to reproduce the problem?

Dlinuigh commented 3 weeks ago

I am trying to write a short code example, sorry for the late reply.

Dlinuigh commented 3 weeks ago

Does reordering the functions help?

for(auto f : font_lut){
  TTF_CloseFont(f.second);
}
TTF_Quit();
SDL_DestroyRenderer(render);
SDL_DestroyWindow(window);
SDL_QuitSubSystem(SDL_INIT_EVENTS); // <---
SDL_QuitSubSystem(SDL_INIT_VIDEO);  // <---
SDL_Quit();

It seems no difference, but I found the break point is still around SDL_DestroyWindow line.

Dlinuigh commented 3 weeks ago

ok, I complete the example

Dlinuigh commented 3 weeks ago

test_sdl.zip well, this example is complied with meson, and as a subproject.

Dlinuigh commented 3 weeks ago

Press the white button in the top left corner. I have tested this in a clean environment, though not a literal one. And

C++ compiler for the host machine: ccache c++ (gcc 14.1.1 "c++ (GCC) 14.1.1 20240507")
C++ linker for the host machine: c++ ld.bfd 2.42.0
Dlinuigh commented 3 weeks ago

And this is the meson.build file content for the latter test.

project('SDL_Test_SetKeyBoardFocus', 'cpp',
  version : '0.1',
  default_options : ['warning_level=3', 'cpp_std=c++14'])

deps = [
  dependency('sdl3'),
  dependency('sdl3-ttf')
]

executable('SDL_Test_SetKeyBoardFocus',
           'test_sdl.cpp',dependencies:deps,
           install : true)
Dlinuigh commented 3 weeks ago

So if you need any other information related, just leave a message.

Dlinuigh commented 3 weeks ago

I found that this is actually a Wayland only problem, Gnome with xorg will not show this WARN.

Dlinuigh commented 3 weeks ago

Lately, I've encountered numerous issues with Wayland compatibility, which has left me feeling quite tense.

Kontrabant commented 3 weeks ago

The sample code has several problems:

#include <SDL3/SDL.h>
#include <sys/types.h>

class Test
{
    SDL_Window *window;
    SDL_Renderer *render;
    SDL_Event event;

  public:
    Test()
    {
        SDL_Init(SDL_INIT_EVENTS);
        SDL_Init(SDL_INIT_VIDEO);
        window = SDL_CreateWindow("Test", 800, 600, SDL_WINDOW_BORDERLESS);
        render = SDL_CreateRenderer(window, NULL, 0);
    }
    bool in_btn()
    {
        float mouse_x, mouse_y;
        SDL_GetMouseState(&mouse_x, &mouse_y);
        return 0 < mouse_x && 0 + 64 > mouse_x && 0 < mouse_y &&
               32 > mouse_y;
    }
    bool handle()
    {
        while (SDL_PollEvent(&event)) {
            switch (event.type) {
            case SDL_EVENT_MOUSE_BUTTON_DOWN:
                if (event.button.button == SDL_BUTTON_LEFT && in_btn()) {
                    return false;
                }
                break;
            case SDL_EVENT_KEY_DOWN:
                if (event.key.keysym.sym == SDLK_q) {
                    return false;
                }
                break;
            }
        }

        return true;
    }
    void clean()
    {
        SDL_DestroyRenderer(render);
        SDL_DestroyWindow(window);
        SDL_QuitSubSystem(SDL_INIT_EVENTS); // <---
        SDL_QuitSubSystem(SDL_INIT_VIDEO);  // <---
        SDL_Quit();
    }
    void present()
    {
        SDL_SetRenderTarget(render, NULL);
        SDL_SetRenderDrawColor(render, 255, 255, 255, 255);
        SDL_FRect btn = { 0, 0, (float)64, (float)32 };
        SDL_RenderFillRect(render, &btn);
        SDL_RenderPresent(render);
    }
};

int main()
{
    Test test;
    while (test.handle()) {
        test.present();
    }
    test.clean();
    return 0;
}

With this on the current version of SDL3, it runs correctly.

Dlinuigh commented 3 weeks ago

I compiled and run the code above, it still has the problem on Wayland. So do you run the code in a xorg environment?

Dlinuigh commented 3 weeks ago

And I agree that this is the problem of mouse event for this won't happen when I quit with keyboard down event.

Dlinuigh commented 3 weeks ago

Anyway I will compile the latest version of SDL3 to test, thank you.

Dlinuigh commented 3 weeks ago

Ok, the problem has been solved. Thank you again.