SDL_ShowOpenFileDialog segfault

Hello, I have been struggling to get this async callback function SDL_ShowOpenFileDialog working for the past couple of days. Can anyone provide an example of how to properly embed this function into the event loop? This is my code in C++:

SDL_DialogFileCallback loadFileDialog()
{

    SDL_DialogFileCallback callback;
    SDL_ShowOpenFileDialog(callback, NULL, NULL, NULL, NULL, NULL, 0);

return callback;

}

while( SDL_PollEvent( &e ) )
{
if( e.type == SDL_EVENT_MOUSE_BUTTON_DOWN)
{
auto vv = std::async(loadFileDialog);
}
if( e.type == SDL_EVENT_QUIT )
{
quit = true;
}
}

I may be doing something obviously wrong, if so please tell me, thank you

first of all, format your code.

yeah, your code is very wrong. let’s forget your current approach and work from zero.

a callback is a function. you define a function obeying this signature: SDL3/SDL_DialogFileCallback - SDL Wiki

e.g.:

void my_dialog_callback(void *userdata, char const *const *filelist, int filter)
{
    (void)userdata;
    (void)filter;

    if (NULL == filelist)
        SDL_Log("Dialog error: %s\n", SDL_GetError());
    else if (NULL == *filelist)
        SDL_Log("No file selected\n");
    else for (; *filelist != NULL; filelist++)
        SDL_Log("I got file %s\n", *filelist);
}

then somewhere in your program initialization (your main function), just call:

SDL_ShowOpenFileDialog(my_dialog_callback, NULL, NULL, NULL, 0, NULL, true);

I don’t what is this std::async() you used, but it is probably wrong. just because the documentation says “This is an asynchronous function” you can’t go around calling code just because the keyword “async” is associated with it. be careful and be 100% sure of everything you are doing. never ever guess. “async” is among the misunderstood and misused words, to make matters worse, so be extra extra careful.

notice in documentation for SDL_ShowOpenFileDialog: “This function should be called only from the main thread”, so yeah, just call it from main thread.

finally, you callback code needs to be thread-safe. what do you want to do with the files selected? depending on your use case, atomics can solve it trivially, otherwise you will have to look into synchronization primitives such as a mutex.

p.s.: I’m a C programmer, so I just use a function as callback. maybe C++ geniuses can write a lambda thing or a closure or whatever as a callback, I don’t know.

1 Like

do you know what is “asynchronous”? do you know what is a callback? do you know what is a function pointer? do you know when a callback is called? do you know what is thread-safety? do you know that programs can execute in many threads? do you know what is the main thread?

we can carefully explain each concept, just ask :slight_smile:

1 Like

Your comment helped me so much, I am new to asynchronous programming, never had to use it so I’ve been trying to get it working. You gave me it working and seriously I thank you! I struggle with those concepts, I’m trying to understand them still. Again thank you so much :blush:

1 Like

I’m glad bro. use the edited code, I made a small mistake. good luck!!

1 Like

SDL 3 has a “Thread Safety” section. Sometimes things will work but might not on other OSes.

This function should be called only from the main thread. The callback may be invoked from the same thread or from a different one, depending on the OS’s constraints.

1 Like