Re: Why cani i initialize prefix with a default string?

2011-12-10 Thread Gary Kline
On Sat, Dec 10, 2011 at 03:23:10AM +0100, David Ne??as wrote:
 Date: Sat, 10 Dec 2011 03:23:10 +0100
 From: David Ne??as y...@physics.muni.cz
 Subject: Re: Why cani i initialize prefix with a default string?
 To: Gary Kline kl...@thought.org
 Cc: GTK Devel List gtk-app-devel-list@gnome.org
 
 On Fri, Dec 09, 2011 at 05:40:09PM -0800, Gary Kline wrote:
  guys, i am appending a piece of gtk test code.  it saves the
  internal filename buffer for espeak -f %s ifbuf to run
  on--to be the user's voice.  if i set 
  
  static char *prefix = talk; 
  
  then back up and enter another string in the box at the
  bottom, strange things happen.  Anybody know where i am
  messing up?
 
 Strange things most likely occur after attempting to free the const
 string in entry_changed_cb()
 
 g_free(prefix);
 
 which was fine if prefix was NULL initially.  You can use -Wwrite-strings
 to catch that.  

your advice was right on target.  i changed the gcc line,
then checked 

if (prefix == NULL)
printf(NOT NULL\n)

early in main().  since i set prefix , the string printed to
stdout.  long-story-short, my test program works the way i 
want.

 Unforutnately, passing constant strings as various
 user-data-kind arguments or to g_hash_table_insets() then produces
 warnings too.

okay; i'll watch out!

 
 Also learn to use valgrind; it would show you this problem immediately.
 

a couple hours ago i installed valgrind and a front end.

The next thing on my to-try list is to get the
increase/decrease buttons together;  this means reading up
on the hbox stuff.  i found one example somewhere last week
and it looks tricky.  --anyhow, thans for your keen
insights.  

gary


 Yeti
 

-- 
 Gary Kline  kl...@thought.org  http://www.thought.org  Public Service Unix
   Journey Toward the Dawn, E-Book: http://www.thought.org
  The 8.57a release of Jottings: http://jottings.thought.org
 Twenty-five years of service to the Unix community.

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Why cani i initialize prefix with a default string?

2011-12-09 Thread Gary Kline

guys, i am appending a piece of gtk test code.  it saves the
internal filename buffer for espeak -f %s ifbuf to run
on--to be the user's voice.  if i set 

static char *prefix = talk; 

then back up and enter another string in the box at the
bottom, strange things happen.  Anybody know where i am
messing up?

tia,

gary

PS.  i am thru with the play button and callback, but
would like to understand my errors above first!


-- 
 Gary Kline  kl...@thought.org  http://www.thought.org  Public Service Unix
   Journey Toward the Dawn, E-Book: http://www.thought.org
  The 8.57a release of Jottings: http://jottings.thought.org
 Twenty-five years of service to the Unix community.


/*
 * compile with:
 * gcc -Wall -Wextra -Werror -g x.c -o x1 `pkg-config --cflags gtk+-2.0` 
`pkg-config --libs gtk+-2.0`
 */

#include gtk/gtk.h
#include stdio.h
#include string.h

static int counter = 1;
static GtkWidget *label;
static char *suffix = .txt;
static char *prefix;
char ifbuf[1024];  /*  internal filename buffer--CURRENT--for espeak -f   */


#define DOT '.'


static void
update_label (void)
{
char buffer[1024];
memset (buffer, 0, sizeof buffer);

/*
   If counter is  1, use markup to highlight text
 */
if (counter = 1)
  {
  if (prefix)
  {
  g_snprintf (buffer, 1023,
  span foreground=\red\ background=\yellow\ 
size=\x-large\%s%c%d%s/span, prefix, DOT, counter, suffix);
  }
  else
{
g_snprintf (buffer, 1023,
span foreground=\red\ background=\yellow\ 
size=\x-large\%d%s/span, counter, suffix);
}
  gtk_label_set_markup (GTK_LABEL (label), buffer);
  }
else
  {
  if (prefix)
  {
  g_snprintf (buffer, 1023, %s%c%d%s, prefix, DOT, counter, 
suffix);
  }
  else
  {
g_snprintf (buffer, 1023, %d%s, counter, suffix);
  }
  gtk_label_set_label (GTK_LABEL (label), buffer);
  }
}

static void
inc_button_click_cb (GtkButton * button, gpointer data)
{
(void) button;
GtkWidget *dec_button = data;
counter++;
/*
   Change senstivity of the decrement button based on counter
 */
if (counter  0  !gtk_widget_is_sensitive (dec_button))
gtk_widget_set_sensitive (dec_button, TRUE);
/*
   Update label to show updated counter 
 */

if (prefix)
{
sprintf (ifbuf, %s%c%d%s, prefix, DOT, counter, suffix);
fprintf(stdout, DEBUG: internsl filename = [%s]\n, ifbuf);
}
else
{
sprintf (ifbuf, %d%s,  counter, suffix);
fprintf(stdout, DEBUG: internsl filename = [%s]\n, ifbuf);
}

update_label ();
return;
}

static void
dec_button_click_cb (GtkButton * button, gpointer data)
{
(void) data;
counter--;
if (counter = 0)
  {
  counter = 1;
  }

/*
   Change senstivity of the decrement button based on counter
 */
if (counter  1  gtk_widget_is_sensitive (GTK_WIDGET (button)))
gtk_widget_set_sensitive (GTK_WIDGET (button), FALSE);
/*
   Update label to show updated counter 
 */

 if (prefix)
 {
sprintf (ifbuf, %s%c%d%s, prefix, DOT, counter, suffix);
fprintf(stdout, DEBUG: internsl filename = [%s]\n, ifbuf);
}
else
{
sprintf (ifbuf, %d%s,  counter, suffix);
fprintf(stdout, DEBUG: internsl filename = [%s]\n, ifbuf);
}

update_label ();
return;
}


static void
entry_changed_cb (GtkEditable * editable, gpointer data)
{
(void) data;
/*
   Caller has to free the text, so call g_free 
 */
g_free (prefix);
/*
   Get the complete text 
 */
prefix = gtk_editable_get_chars (editable, 0, -1);
/*
   Update label to show updated prefix 
 */
update_label ();
return;
}

int
main (void)
{
GtkWidget *button_inc;
GtkWidget *button_dec;
GtkWidget *entry_label;
GtkWidget *entry;
GtkWidget *window;
GtkWidget *vbox;

gtk_init (NULL, NULL);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
gtk_window_set_title (GTK_WINDOW (window), VoiceByComputer);
g_signal_connect (window, destroy, G_CALLBACK (gtk_main_quit), NULL);
vbox = gtk_vbox_new (FALSE, 5);
gtk_container_add (GTK_CONTAINER (window), vbox);
gtk_container_set_border_width (GTK_CONTAINER (window), 10);

label = gtk_label_new ();
update_label ();

button_dec = gtk_button_new_with_label (Decrease counter);
g_signal_connect (button_dec, clicked, G_CALLBACK (dec_button_click_cb),
  NULL);
gtk_widget_set_sensitive (button_dec, FALSE);

button_inc = gtk_button_new_with_label (Increase counter);

Re: Why cani i initialize prefix with a default string?

2011-12-09 Thread David Nečas
On Fri, Dec 09, 2011 at 05:40:09PM -0800, Gary Kline wrote:
   guys, i am appending a piece of gtk test code.  it saves the
   internal filename buffer for espeak -f %s ifbuf to run
   on--to be the user's voice.  if i set 
 
   static char *prefix = talk; 
 
   then back up and enter another string in the box at the
   bottom, strange things happen.  Anybody know where i am
   messing up?

Strange things most likely occur after attempting to free the const
string in entry_changed_cb()

g_free(prefix);

which was fine if prefix was NULL initially.  You can use -Wwrite-strings
to catch that.  Unforutnately, passing constant strings as various
user-data-kind arguments or to g_hash_table_insets() then produces
warnings too.

Also learn to use valgrind; it would show you this problem immediately.

Yeti

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list