GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Aug 24, 2016 11:11 pm

All times are UTC

Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: g_signal_connect seems to be corrupting pointer to pbar???
PostPosted: Sun Feb 24, 2013 7:23 am 

Joined: Wed Feb 20, 2013 10:28 am
Posts: 1
I have spent a long time trying to find out what is wrong with this, and I have even tried doing it in different ways. I have majorly distilled my program down to the following code in an attempt to isolate the problem, but I simply cannot find the problem with the code or think of how I could further simplify this.

(edit: changed pointers think I copied wrong the first time, but the point is that they are different)

output on key press:
test: 0x9264048

edit: confirming that it works as expected when I do it with password_entry instead of progress_bar (output in both cases is: 0x9ee3028
test: 0x9ee3028)

this is the same general story. At first I tried to link the progress bar to the password entry widget but I kept getting error telling me it is not a progress bar. So then I checked and I see that indeed the pointer is changing when trying to pass it with g_signal_connect. If I pass it normally without g_signal_connect it works fine no matter what, but then I can not associate it with a key press event.

I cannot think of what else to try. With everything other than progress bar this seems to work so far. I also found a comment from someone else on a GTK forum who was apparently having this same issue, it spontaneously fixed for him but not for me. People claimed he probably had some memory corruption going on or some other bug somewhere. I have tried very hard to debug my application but I have no idea why the pointer is corrupting in this case.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gtk/gtk.h>

typedef struct{
  GtkGrid* grid;
  GtkWidget* sibling;
  int relational_position;
  int pad_top;
  int pad_bottom;
  int pad_left;
  int pad_right;
  int span_rows;
  int span_columns;
  int hexpand;
  int vexpand;
  int halign;
  int valign;

GtkWindow* initialize_top_window(char* title, int height, int width);
GtkWidget* add_entry(char* initial_text, int mask_text, char* path2icon, drawinfo* entry_drawinfo);
GtkWidget* add_progress_bar(int show_text, drawinfo* progress_bar_drawinfo);
void draw_widget(GtkWidget* widget, drawinfo* widget_drawinfo);
void add_stuff(GtkGrid* intro_grid);
void testf(GtkWidget* entropy_bar);

int main (int argc, char *argv[])

  gtk_init (&argc, &argv);
  GtkWindow* top_window = initialize_top_window("Program", 1000, 500);

  GtkGrid* top_grid = (GtkGrid*)gtk_grid_new();
  gtk_container_add(GTK_CONTAINER(top_window), (GtkWidget*)top_grid);
  gtk_widget_show( (GtkWidget*) top_grid);

  return 0;

void add_stuff(GtkGrid* intro_grid)

  drawinfo* password_entry_di = malloc(sizeof(drawinfo));
  password_entry_di->grid = intro_grid;
  password_entry_di->sibling = NULL;
  password_entry_di->relational_position = 0;
  password_entry_di->pad_top = 10;
  password_entry_di->pad_bottom = 0;
  password_entry_di->pad_left = 100;
  password_entry_di->pad_right = 100;
  password_entry_di->span_rows = 1;
  password_entry_di->span_columns = 1;
  password_entry_di->hexpand = TRUE;
  password_entry_di->vexpand = FALSE;
  password_entry_di->halign = GTK_ALIGN_FILL;
  password_entry_di->valign = GTK_ALIGN_FILL;
  GtkWidget* password_entry = add_entry("Password", TRUE, "graphics/key.png", password_entry_di);

  drawinfo* password_entropy_progress_bar_di = malloc(sizeof(drawinfo));
  password_entropy_progress_bar_di->grid = intro_grid;
  password_entropy_progress_bar_di->sibling = password_entry;
  password_entropy_progress_bar_di->relational_position = GTK_POS_BOTTOM;
  password_entropy_progress_bar_di->pad_top = 10;
  password_entropy_progress_bar_di->pad_bottom = 0;
  password_entropy_progress_bar_di->pad_left = 100;
  password_entropy_progress_bar_di->pad_right = 100;
  password_entropy_progress_bar_di->span_rows = 1;
  password_entropy_progress_bar_di->span_columns = 1;
  password_entropy_progress_bar_di->hexpand = TRUE;
  password_entropy_progress_bar_di->vexpand = FALSE;
  password_entropy_progress_bar_di->halign = GTK_ALIGN_FILL;
  password_entropy_progress_bar_di->valign = GTK_ALIGN_FILL;

  GtkWidget* progress_bar = gtk_progress_bar_new();

  gtk_progress_bar_set_show_text( (GtkProgressBar*) progress_bar, 1);
  draw_widget(progress_bar, password_entropy_progress_bar_di);

  g_signal_connect(password_entry, "key-release-event", G_CALLBACK(testf), progress_bar);

  printf("%p\n", progress_bar);


void testf(GtkWidget* entropy_bar)
  printf("test: %p\n", entropy_bar);

void draw_widget(GtkWidget* widget, drawinfo* widget_drawinfo)
  gtk_grid_attach_next_to(widget_drawinfo->grid, widget, widget_drawinfo->sibling,

  gtk_widget_set_hexpand(widget, widget_drawinfo->hexpand);
  gtk_widget_set_vexpand(widget, widget_drawinfo->vexpand);

  gtk_widget_set_halign(widget, widget_drawinfo->halign);
  gtk_widget_set_valign(widget, widget_drawinfo->valign);

  g_object_set(widget, "margin-left", widget_drawinfo->pad_left, NULL);
  g_object_set(widget, "margin-right", widget_drawinfo->pad_right, NULL);
  g_object_set(widget, "margin-bottom", widget_drawinfo->pad_bottom, NULL);
  g_object_set(widget, "margin-top", widget_drawinfo->pad_top, NULL);


GtkWidget* add_progress_bar(int show_text, drawinfo* progress_bar_drawinfo)
  GtkWidget* progress_bar = gtk_progress_bar_new();
  gtk_progress_bar_set_show_text( (GtkProgressBar*) progress_bar, show_text);
  draw_widget(progress_bar, progress_bar_drawinfo);
  return progress_bar;

GtkWidget* add_entry(char* initial_text, int mask_text, char* path2icon, drawinfo* entry_drawinfo)
  GtkWidget* entry = gtk_entry_new();
  gtk_entry_set_visibility( (GtkEntry*)entry, !mask_text);
  gtk_entry_set_placeholder_text( (GtkEntry*)entry, initial_text);
  GdkPixbuf* entry_icon = gdk_pixbuf_new_from_file( (const char*) path2icon, NULL);
  gtk_entry_set_icon_from_pixbuf( (GtkEntry*)entry, GTK_ENTRY_ICON_PRIMARY, entry_icon);
  draw_widget(entry, entry_drawinfo);
  return entry;

GtkWindow* initialize_top_window(char* title, int height, int width)
  GtkWidget* main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(  (GtkWindow*)main_window, title);
  gtk_window_set_default_size( (GtkWindow*)  main_window, height, width);
  g_signal_connect( (GtkWindow*)main_window, "delete-event", G_CALLBACK(exit), NULL);
  return (GtkWindow*)main_window;

 Post subject: Re: g_signal_connect seems to be corrupting pointer to pbar?
PostPosted: Mon Feb 25, 2013 5:55 am 
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 1046
Location: UK

The reason you are getting two different values for the pointer is that you are taking the pointer of different objects. This is because you are using the signal/call-back functions incorrectly. Each signal has a specific proto-type for their call-back function.

For the "key-release-event" signal the call-back function is
gboolean            user_function                      (GtkWidget *widget,
                                                        GdkEvent  *event,
                                                        gpointer   user_data)
Where :-
- widget : the object that received the signal
- event : the GdkEventKey which triggered this signal
- user_data : user data set when the signal handler was connected

Your call back function is
void testf(GtkWidget* entropy_bar)
  printf("test: %p\n", entropy_bar);
so you are printing the pointer to "password_entry" here are in the function add_stuff() you are printing the pointer to "progress_bar"

To correct this problem you need to change the call-back function to something like this
gboolean testf(GtkWidget* entropy_bar, GdkEvent *event,  gpointer data)
  printf("test: %p\n", data);
  return FALSE;


Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC

Who is online

Users browsing this forum: Google [Bot] and 2 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group