Flix01 / imgui

Dear ImGui Addons Branch = plain unmodified dear imgui plus some extra addon.
https://github.com/Flix01/imgui/wiki/ImGui-Addons-Branch-Home
MIT License
396 stars 34 forks source link

addon_example/main2 crashes with segmentation fault #21

Closed traw closed 7 years ago

traw commented 7 years ago

Except main2 all other examples given in _addonexamples works. I am using following command to compile all the examples. I'm using Ubuntu 16.04LTS.

 g++ -g -o main main.cpp -I"../../" ../../imgui.cpp ../../imgui_draw.cpp ../../imgui_demo.cpp -D"IMGUI_INCLUDE_IMGUI_USER_H" -D"IMGUI_INCLUDE_IMGUI_USER_INL" `sdl2-config --cflags` -D"IMGUI_USE_SDL2_BINDING" -L"/usr/lib/x86_64-linux-gnu" `sdl2-config --libs` -lX11 -lm -lGL -lstdc++

Crash log:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) bt
#0  0x0000000000000000 in ?? ()
#1  0x000000000040ec40 in ImGui::MemAlloc (sz=80) at ../../imgui.cpp:2043
#2  0x0000000000444204 in ImGui::TabWindow::TabWindow (this=0x7b2920 <tabWindows>) at ../.././addons/./imguitabwindow/imguitabwindow.cpp:2165
#3  0x00000000004075a8 in __static_initialization_and_destruction_0 (__initialize_p=1, __priority=65535) at main2.cpp:57
#4  0x0000000000407628 in _GLOBAL__sub_I_myImageTextureId () at main2.cpp:982
#5  0x00000000005067fd in __libc_csu_init ()
#6  0x00007ffff6c057bf in __libc_start_main (main=0x4073e6 <main(int, char**)>, argc=1, argv=0x7fffffffdfc8, init=0x5067b0 <__libc_csu_init>, fini=<optimized out>, 
    rtld_fini=<optimized out>, stack_end=0x7fffffffdfb8) at ../csu/libc-start.c:247
#7  0x00000000004032d9 in _start ()
Flix01 commented 7 years ago

Your command-line refers to main.cpp. However when I use this command-line:

/.../addons_examples$ g++ -g -o main2_sdl  ../../imgui.cpp ../../imgui_draw.cpp main2.cpp -I"../../" -D"IMGUI_INCLUDE_IMGUI_USER_H" -D"IMGUI_INCLUDE_IMGUI_USER_INL" `sdl2-config --cflags` -D"IMGUI_USE_SDL2_BINDING" -L"/usr/lib/x86_64-linux-gnu" `sdl2-config --libs` -lX11 -lm -lGL
/.../addons_examples$ ./main2_sdl

Then it works. The reason is probably that, technically, when compiling more than one source file together in a single command line, it's better to keep them in order (that is to compile main2.cpp last): otherwise it might happen that a static variable needs another static variable that's not defined yet (I guess the linker is not smart enough...). In any case my command-line should work. It's not an error in the source code.

P.S. I must admit that this is the first time I see this happen (and of course when using any of the provided project files for Linux it does not happen because each source file is compiled separately and linked at the end). It seems that the emscripten compiler is not affected by the cpp file order too.

It's even possible that this is a bug of g++... We should try using clang++ and see...

Flix01 commented 7 years ago

Actually it seems that what you've discovered affects clang and emscripten as well! I've made some modifications to README_FIRST.txt to explain that the source file order is important.