LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 03-30-2011, 12:25 AM   #1
Netooo
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?

Code:
#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;
 comienzo=clock();

 while(count){
   sprintf(buf,"%.2f",(clock()-comienzo)/(double)CLOCKS_PER_SEC );
   printf("%s\r",buf);
   //gtk_label_set_text(label,buf);
 }
}

void arranca(GtkWidget *widget, gpointer label){
  if(count==0)count=1;
  else count=0;
  GThread *thread;
  
  gtk_label_set_text(label,(count==1)?"Corriendo":"Alto");
  if(count==1)
     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);
  
  g_thread_init(NULL);

  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); 
  
  gtk_widget_show_all(window);

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

  gdk_threads_enter();
  gtk_main();
  gdk_threads_leave();

  return 0;
}
 
Old 03-30-2011, 01:42 AM   #2
paulsm4
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, 08:27 AM   #3
dugan
Senior Member
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 4,665

Rep: Reputation: 1440Reputation: 1440Reputation: 1440Reputation: 1440Reputation: 1440Reputation: 1440Reputation: 1440Reputation: 1440Reputation: 1440Reputation: 1440
Quote:
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
 
  


Reply

Tags
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 04:43 AM
Newbie Needs Help w/ Gimp/GTK compile alxdotnet Linux - Software 4 04-21-2004 11:47 PM
Newbie Question - What is GTK Exactly? Zeraxeal Slackware 4 03-24-2004 07:27 PM
GTK Newbie tslugmo Linux - Newbie 8 06-19-2003 02:12 PM


All times are GMT -5. The time now is 12:35 PM.

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