`focus-in-event` receives a different memory address as user_data than what was originally added during signal connecting

Using GTK 3.24, I’m receiving a different pointer to my user_data in the focus-in-event than what I put in when connecting the signal:

// focusData is a member struct
g_signal_connect(widget, "focus-in-event", G_CALLBACK(focus_in_event), &focusData);
std::cout << "Connect: focusData is " << &focusData << std::endl;

My focus-in function looks like this:

static gboolean focus_in_event(GtkWidget* self, GdkEventFocus event, gpointer user_data)
{
    DialogFocusData* focusData = (DialogFocusData*)user_data;
    std::cout << "FocusIn: focusData is " << user_data << std::endl;
    return false;
}

This setup has worked fine with the focus signal, where I can cast the user_data pointer to a pointer to my DialogFocusData struct and it’s always pointing at the address I put in when connecting the signal.

But when doing this with the focus-in-event, the pointer address of user_data never is the address I put in during connecting the signal and is surprisingly changing after some events:

// Connecting the signal to some GtkWidgets, note how all pointer addresses are the same
Connect: focusData is 0x55555591c568
Connect: focusData is 0x55555591c568
Connect: focusData is 0x55555591c568
Connect: focusData is 0x55555591c568
Connect: focusData is 0x55555591c568
Connect: focusData is 0x55555591c568
// focus_in_event is called, note how the addresses never match and sometimes change
FocusIn: focusData is 0x555555a83b90
FocusIn: focusData is 0x555555a83b90
FocusIn: focusData is 0x555555c8cfe0
FocusIn: focusData is 0x555555c8cfe0
FocusIn: focusData is 0x555555c8cfe0
FocusIn: focusData is 0x555555c8cfe0
FocusIn: focusData is 0x555555c8cfe0
FocusIn: focusData is 0x555555b8ea90
FocusIn: focusData is 0x555555b8ea90

Is there anything special with the focus-in-eventthat reserves the user_data pointer?

The signal signature is:

gboolean
user_function (GtkWidget *widget,
               GdkEvent  *event,
               gpointer   user_data)

I see in your code that you omitted the * for the GdkEvent.

So: GdkEventFocus event should be replaced by GdkEventFocus *event (or GdkEvent *event).

The problem probably comes from that.

1 Like

Thanks for spotting this, I’ll try this later.

I just copy & pasted the signature from the docs: Gtk.Widget::focus-in-event

gboolean
focus_in_event (
  GtkWidget* self,
  GdkEventFocus event,
  gpointer user_data
)

Where did you find that the second parameter is a pointer?

However, it would also explain why the GdkEventFocus window parameter never matched with my dialog’s window address.

I read the API reference as generated by gtk-doc (not gi-docgen), in the Devhelp app.

Linux distros usually have a package for installing the gtk-doc of gtk3 and other libraries.

1 Like

So it’s maybe a bug in gi-docgen, in which case it should be reported on GitLab.

2 Likes

I reported it to gi-docgen’s issue tracker.

With the correct function signature, everything is working as expected.
Thanks a lot for your help!

1 Like

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