assembly - Call to gtk_main_quit causes "segmentation fault" in assembler code -


i'm learning assembler (fasm) , have weird problem, whenever want call gtk_main_quit() ends "segmentation fault".

why call gtk_main_quit causes segmentation fault?

test.asm

format elf  extrn gtk_init extrn gtk_main extrn gtk_main_quit extrn gtk_window_new extrn gtk_widget_show extrn g_signal_connect_data  public main  on_window_close:     call gtk_main_quit ; <- segmentation fault     ret  main:     push 0     push 0     call gtk_init     add esp, 8      push 0     call gtk_window_new     add esp, 4     mov [window_handle], eax      push 0     push 0     push 0     push on_window_close     push on_close_signal     push [window_handle]     call g_signal_connect_data     add esp, 24      push [window_handle]     call gtk_widget_show     add esp, 8      call gtk_main  window_handle dd 0 on_close_signal db 'destroy', 0 

makefile

all:     ~/apps/fasm/fasm ./test.asm     gcc -o test test.o `pkg-config --cflags --libs gtk+-3.0` 

when making function calls ensure restore stack after call. code this:

push [window_handle] call gtk_widget_show add esp, 8 

you push 1 dword onto stack parameter correct, after call gtk_widget_show add 8 esp. since pushed 4 bytes on stack, restores esp improperly. side effect return address of function main in wrong place yield segmentation fault when main function returns. code should have been:

push [window_handle] call gtk_widget_show add esp, 4 

that brings second issue. code:

    call gtk_main  window_handle dd 0 on_close_signal db 'destroy', 0 

after gtk_main returns start executing whatever instructions appear in memory after. in case happens variables , whatever else in memory. since c runtime called function main other function, should use ret return c runtime , let shutdown program cleanly.

the code like:

    call gtk_main     ret  window_handle dd 0 on_close_signal db 'destroy', 0 

Comments