Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8424557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:01:35+00:00 2026-06-10T04:01:35+00:00

I am learning GTK+ and this simple application crashes every time I run it.

  • 0

I am learning GTK+ and this simple application crashes every time I run it.

It creates a label in the main window, and every time a button is clicked (the key_press_event) the label and the title should swap.

If I comment out the gtk_label_set_text in the change_title function the title alternates correctly and the app doesn’t crash. Why does gtk_label_set_text crash my app?

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

const gchar first[]="FIRST";
const gchar last[]="LAST";

static void destroy(GtkWidget *window,gpointer data)
{
  gtk_main_quit();
}

static gboolean change_title(GtkWidget *widget,GtkLabel *data)
{
  if(strcmp(last,gtk_window_get_title(GTK_WINDOW(widget)))){
    gtk_window_set_title(GTK_WINDOW(widget),last);
    gtk_label_set_text(data,first);
  } else {
    gtk_window_set_title(GTK_WINDOW(widget),first);
    gtk_label_set_text(data,last);
  }
  return FALSE;
}

int main(int argc,char **argv)
{
  GtkWidget *window, *label;
  gtk_init(&argc,&argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(window),last);
  gtk_widget_set_size_request(window,300,100);
  g_signal_connect(window,"destroy_event",G_CALLBACK(destroy),NULL);

  label = gtk_label_new("caasdasdjadnjadjahadjad");
  gtk_container_add(GTK_CONTAINER(window),label);

  g_signal_connect(window,"key_press_event",G_CALLBACK(change_title),GTK_LABEL(label));

  gtk_widget_show_all(window);

  gtk_main();
  return 0;
}        

EDIT: I found the problem using GDB, the label pointer isn’t passed correctly to the change_title function. I don’t know why. (Ex: in main() label = 0xb6406608 , in change_title() label = 0x807bda8)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:01:37+00:00Added an answer on June 10, 2026 at 4:01 am

    After doing a simple Google search on key_press_event I saw that the callback to that event have another argument between the widget and the user-data pointer. The prototype is this:

    gboolean key_event_handler(GtkWidget *widget,GdkEventKey *event, gpointer data);
    

    So simple change your function to this:

    static gboolean change_title(GtkWidget *widget, GdkEventKey *event, GtkLabel *data)
    

    and it should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am learning Python by building a simple PyGTK application that fetches data from
I am learning C and compiling the codes with GTK+. I have a simple
Learning some VBA. So far, I've constructed this piece of code which should allow
Just learning the world of jquery, and all my googling gives examples like this:
I have set about learning the gtk. And when I was browsing the reference
Learning Ruby. Needed to create a hash of arrays. This works... but I don't
I've started learning Lisp recently and wanted to write a program which uses gtk
(learning c#) I have a C# WPF application which when a certain form is
Learning how to use Phonegap to create an Android application I am still experimenting
I'm learning GTK+2 and glade 3. I'm trying to add a text to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.