Reputation:
I keep getting this error and weird gibberish when I try to run the following snippet of code:
ERROR is:
(foo:11333): Pango-WARNING **: Invalid UTF-8 string passed to pango_layout_set_text()
Structure definition:
typedef struct chat_info ChatInfo;
struct chat_info {
GtkWidget *text_line1;
GtkWidget *text_line2;
GtkWidget *text_line3;
GtkWidget *entry;
};
And this code makes the weird gibberish come out on the third label (second label is empty)
static void send_message(GtkWidget *window, gpointer data){
GuiInfo *g = (GuiInfo *) data;
ChatInfo *c = g->chat;
const gchar *message = gtk_entry_get_text(GTK_ENTRY(c->entry));
const gchar *oldtext1 = gtk_label_get_text( (GtkLabel *) c->text_line1);
const gchar *oldtext2 = gtk_label_get_text( (GtkLabel *) c->text_line2);
gtk_label_set_text( (GtkLabel *) c->text_line1, message);
gtk_label_set_text( (GtkLabel *) c->text_line2, oldtext1);
gtk_label_set_text( (GtkLabel *) c->text_line3, oldtext2);
}
I looked up the error, and I'm guessing it's probably due to something like giving a pointer address when I should be giving a gchar string? Everything looks right though.
Upvotes: 0
Views: 603
Reputation: 1460
I believe the problem is that the memory that's coming back from your _get_text()
calls is being freed by GtkLabel
before you use it again by you calling _set_text()
.
For example when you call gtk_label_set_text(c->text_line1, message)
that renders the pointer you retrieved into oldtext1
invalid (the call to gtk_label_get_text()
returns a pointer to the internal copy of the string).
Now you can fix this by reordering your code to remove that dependency:
gtk_label_set_text( (GtkLabel *) c->text_line3, oldtext2);
gtk_label_set_text( (GtkLabel *) c->text_line2, oldtext1);
gtk_label_set_text( (GtkLabel *) c->text_line1, message);
Or perhaps for readability you should strdup()
the strings you get from _get_text()
and then pass those into _set_text()
and free those later.
Upvotes: 4