Friday, November 27, 2020

Mystery crash in an SDL2 application (solved)

I am developing an application in SDL2. It works OK most of the time. But once in a while, it will crash while just going to refresh the screen with new content with SDL_RenderPresent(). The backtrace from gdb shows:

#0  0x00007ffff73a1c7b in raise () from /lib64/libc.so.6
#1  0x00007ffff7382548 in abort () from /lib64/libc.so.6       
#2  0x00007ffff35a682b in ?? () from /usr/lib64/xorg/modules/dri/iris_dri.so
#3  0x00007ffff40a5886 in ?? () from /usr/lib64/xorg/modules/dri/iris_dri.so
#4  0x00007ffff35c29e9 in ?? () from /usr/lib64/xorg/modules/dri/iris_dri.so
#5  0x00007ffff35b6d2b in ?? () from /usr/lib64/xorg/modules/dri/iris_dri.so
#6  0x00007ffff4ef99e0 in glPrimitiveBoundingBox () from /usr/lib64/libGLX_mesa.so.0
#7  0x00007ffff4eecad1 in ?? () from /usr/lib64/libGLX_mesa.so.0
#8  0x00007ffff7ef6283 in ?? () from /usr/lib64/libSDL2-2.0.so.0
#9  0x0000000000415f42 in refresh_display (ctx=0x7fffffffd370) at gui/main.c:1077
#10 0x0000000000413c79 in main_loop_handler (ctx=0x7fffffffd370) at gui/main.c:362
#11 0x0000000000413b22 in main (argc=1, argv=0x7fffffffdd58) at gui/main.c:323

I searched around the web with SDL2 crashing during SDL_RenderPresent(). Most of them points to some form of memory corruption causing that, but I have double check my code with various tools and manual inspection, there is no memory corruption. Then I came across this article "Rock solid frame rates with SDL2", it mentioned a function SDL_SetSwapInterval() which will delay SDL_RenderPresent() from swapping the render buffers till the swap interval (wait for vsync). This seems match what I suspected that the buffer is updated on the wrong time and causing the crash.

There is just a little problem, there is no more SDL_SetSwapInterval() any more, in stead it become SDL_GL_SetSwapInterval(). I added in the oneline call SDL_GL_SetSwapInterval(1), and vala, so far all tests do not show the crash anymore.

Thursday, July 9, 2020

WebAssmebly passing string from C to JavaScript

Recently, I am working on a project that involves WebAssembly. In the project I would need to call out a JavaScript function with a C string char *string as parameter.

I could not get the string in JavaScript function, searching on the internet does not result in any useful information. None of the solutions I found are elegant. Then I realized I had used the WebSocket APIs from Emscripten, which is passing the URL as a C string to the underline JavaScript support function. How does it do that? I digged out the source of it and found in function emscripten_websocket_new, it is using UTF8ToString to convert the C string to a JavaScript String. UTF8ToString is Emscripten's runtime function, I could not call it directly from my JavaScript function.

According to the preamble.js document, I would need to use -s 'EXTRA_EXPORTED_RUNTIME_METHODS=["UTF8ToString"]' on the linker command to export the function and use it as Module.UTF8ToString. Problem solved.

Below are sample code that demonstrate the usage:
JavaScirpt function:

function processing_string_from_c(ptr, len) {
    var text = Module.UTF8ToString(ptr, len);
    ....
}
In the C code:
 char *string;
 ...
 EM_ASM({process_string_from_c($0, $1);}, string, strlen(string));
 ...
And in the linker line, remember to use
-s 'EXTRA_EXPORTED_RUNTIME_METHODS=["UTF8ToString"]'
P.S. In the latest release of emscripten, the export functions become global scope. So calls would be UTF8ToString() directly.