Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-30-2011, 01:25 AM   #1
LQ Newbie
Registered: Jun 2010
Location: Guadalajara, Jalisco, Mexico
Distribution: Slackware
Posts: 20

Rep: Reputation: 0
Unhappy GTK newbie

Hi, I hope somebody can help me, my program crashes if I uncomment the line "gtk_label_set_text(label,buf);" from the "timer" function. Application works fine to me, I just want a very simple cronometer, and it works if I keep that line commented, the problem is that I can only show the cronometer to the console, and I want it in the GUI.

I don't know why the gtk_label_set_text function works in my function "arranca" but crashes in function "timer", any ideas?

#include <gtk/gtk.h>
#include <time.h>
#include <stdio.h>

gint count = 0;
char buf[8];

void timer(GtkWidget *widget, gpointer label){
 clock_t comienzo;

   sprintf(buf,"%.2f",(clock()-comienzo)/(double)CLOCKS_PER_SEC );

void arranca(GtkWidget *widget, gpointer label){
  else count=0;
  GThread *thread;
     thread=g_thread_create((GThreadFunc)timer, label, FALSE, NULL);

int main(int argc, char** argv) {
  clock_t comienzo;
  int test;

  GtkWidget *label;
  GtkWidget *window;
  GtkWidget *frame;
  GtkWidget *button;
  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_window_set_default_size(GTK_WINDOW(window), 250, 150);
  gtk_window_set_title(GTK_WINDOW(window), "CRONOMETRO");

  frame = gtk_fixed_new();
  gtk_container_add(GTK_CONTAINER(window), frame);

  button = gtk_button_new_with_label("START/STOP");
  gtk_widget_set_size_request(button, 90, 35);
  gtk_fixed_put(GTK_FIXED(frame), button, 50, 50);

  label = gtk_label_new("Alto");
  gtk_fixed_put(GTK_FIXED(frame), label, 180, 60); 

  g_signal_connect(window, "destroy",
      G_CALLBACK (gtk_main_quit), NULL);
  g_signal_connect(button, "clicked", 
      G_CALLBACK(arranca), label);


  return 0;
Old 03-30-2011, 02:42 AM   #2
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Two suggestions:

1. Make "buf[8]" larger (maybe you're overwriting your buffer and corrupting adjacent memory).

2. Compile with "-g" and step through the code in under the debugger.
Verify that the hex address of "label" is the same in each of "timer()", "arranca()" and "main()".

'Hope that helps .. PSM
Old 03-30-2011, 09:27 AM   #3
LQ Guru
Registered: Nov 2003
Location: Canada
Distribution: Slackware
Posts: 6,079

Rep: Reputation: 1925Reputation: 1925Reputation: 1925Reputation: 1925Reputation: 1925Reputation: 1925Reputation: 1925Reputation: 1925Reputation: 1925Reputation: 1925Reputation: 1925
Originally Posted by paulsm4 View Post
1. Make "buf[8]" larger (maybe you're overwriting your buffer and corrupting adjacent memory).
And use something other than sprintf, which makes it very easy to make this kind of mistake. Use snprintf, or better yet, change the char arrar to a gchar array and use g-snprintf


gtk, multithreading

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
linux newbie needs help with gtk+ and tk leposima Linux - Software 1 01-04-2006 05:43 AM
Newbie Needs Help w/ Gimp/GTK compile alxdotnet Linux - Software 4 04-22-2004 12:47 AM
Newbie Question - What is GTK Exactly? Zeraxeal Slackware 4 03-24-2004 08:27 PM
GTK Newbie tslugmo Linux - Newbie 8 06-19-2003 03:12 PM

All times are GMT -5. The time now is 04:32 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration