# Problems while passing function pointers to FFI

**URL:** <https://forum.modular.com/t/problems-while-passing-function-pointers-to-ffi/481>\
**Category:** Mojo\
**Tags:** debugging, mojo-compiler, 24\_6\
**Created:** [January 26, 2025, 5:42am UTC](https://forum.modular.com/t/problems-while-passing-function-pointers-to-ffi/481 "2025-01-26T05:42:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![HammadHAB](https://avatars.discourse-cdn.com/v4/letter/h/e495f1/32.png) [@HammadHAB](https://forum.modular.com/u/HammadHAB)\
**Post date:** [January 26, 2025, 5:42am UTC](https://forum.modular.com/t/problems-while-passing-function-pointers-to-ffi/481/1 "2025-01-26T05:42:43Z")

</div>

Hi. I am tyring to create library bindings of FLTK, in mojo. However, mojo’s compiler throws a segmentation fault when I try to pass an `UnsafePointer` of a function, to the ffi:

```mojo
from mjui._app import Application, FLTK_WIDGET_POINTER
from mjui.const import *
from mjui.const import __dll

fn log(w: FLTK_WIDGET_POINTER, *args:c_void) -> NoneType:
    print("Jello")
    return

fn main() raises:
    var app = Application()
    var window = fltk_create_window_new(1000, 500, "Window")
    var grid = fltk_layout_grid(0, 0, get_width(window), get_height(window))
    fltk_set_grid_layout_dimensions(grid, 10, 5, -1, 10)
    set_widget_color(grid, rgb_to_i32(77, 0, 42))

    var string = "Submit".as_bytes()
    var input = fltk_create_input_new(0, 0, 0, 0, ''.as_bytes())
    var button2 = fltk_create_button_new(0, 0, 0, 0, string)
    # set_widget_color(button, rgb_to_i32(0, 255, 0))
    var fn_pointer = UnsafePointer.address_of(log)

    set_widget_color(button2, rgb_to_i32(255, 166, 214))
    set_selection_color(button2, rgb_to_i32(250, 152, 204))
    set_widget_callback(button2, fn_pointer)

    fltk_grid_assign_pos(grid, input, 4, 2)
    fltk_grid_assign_pos(grid, button2, 5, 2)

    end_widget_child_append(grid)
    end_widget_child_append(window)
    show_widget(window)
    app.execute()

```

Here’s the implementation of set\_widget\_callback:

```c

void set_widget_callback(Fl_Widget *widget, Fl_Callback0 callback)
{
    widget->callback(callback);
}

```

and the code that fetches the symbol:

```mojo
var set_widget_callback = __dll.get_function[fn(w: FLTK_WIDGET_POINTER, handler:UnsafePointer[fn(w: FLTK_WIDGET_POINTER, *args:c_void)->NoneType, alignment=1]) -> NoneType]('set_widget_callback')

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex001/uploads/modular/original/1X/2751e0fbdc595a99718b216730957e9db4448cfd.jpeg) [@system](https://forum.modular.com/u/system)\
**Post date:** [July 25, 2025, 5:43am UTC](https://forum.modular.com/t/problems-while-passing-function-pointers-to-ffi/481/2 "2025-07-25T05:43:39Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
