0

I have a struct that i populate to update my textview widget (note i am mixing C and C++ code, and my compiler is g++)

struct wdata
{ 
// some other variables;
// assume all the widget are initiated properly
   struct *GUI { all the widgets...};
   const char* psText , *csText;   
}

In a thread (different from gtk_main thread), after certain messages are received ...the status is reported in a texview, and because it is running in a different thread, i use gdk_threads_add_idle()

wd->csText = convChar(" Valid message : Ok... \n");
gdk_threads_add_idle(disptext_CS,(void*)wd);

where the methods convChar() and disptext_CS are follow:

const gchar* convChar(string sc) 
{
return sc.c_str();
}

and

gboolean disptext_RFCS(void* wdata)
{
   WinData* wd = (WinData*)wdata;

   wd->GUI->mark = gtk_text_buffer_get_insert(wd->GUI->buffview);
   gtk_text_buffer_get_end_iter(wd->GUI->buffview, &wd->GUI->iter);
   gtk_text_buffer_move_mark(wd->GUI->buffview, wd->GUI->mark,
         &wd->GUI->iter);
   gtk_text_buffer_insert_at_cursor(wd->GUI->buffview, wd->csText, -1);
   gtk_text_view_scroll_to_mark(wd->GUI->txtview, wd->GUI->mark, 0.0, TRUE,
         1.0, 0.0);

return G_SOURCE_REMOVE;
}

Output in a GtkTextview:

The textview displays messy strings with @ and i suppose it has something to do with not having a null ended string of characters or wrong size , my conversion from string to char, is the suspect.

 Valid messa@ Valid messa@ Valid message : O@ Valid message : O@

The ouutput in shell:

Gtk-CRITICAL **: gtk_text_buffer_emit_insert: assertion 'g_utf8_validate (text, len, NULL)' failed

But i am expecting something like this :

Valid message : Ok...
Valid message : Ok...
Valid message : Ok...
Valid message : Ok...
Sam Gomari
  • 733
  • 4
  • 13
  • 37

1 Answers1

0
const char* psText , csText;

does not create two pointers. It is the same as

const char* psText;
const char  csText;   

The compiler should have warned about this error, if not prevented it entirely, by telling you that assignments to csText converted a pointer to an integer without a cast.

To fix this, you need a * before both names:

const char *psText, *csText;

In addition, your convChar() method is not safe: once the sc parameter goes out of scope, the c_str() pointer is made invalid. You will need to store a copy of it. You can make one easily with g_strdup(); be sure to free it with g_free() when done.

If the string you're passing to convChar() is always a "string literal", however, you don't need the function at all; the type of a string literal is already const char *.

andlabs
  • 11,290
  • 1
  • 31
  • 52
  • Thank you, i acutally was using pointer for `csTest`, i just wrote it here without the `*`. Help me understand me something here, how is that `s_str()` is made invalid once the `sc` goes out of scope, i thought the return would return a copy in the form of a `char` pointer and therefore the function would serve its intended purpose. ? Thanks a lot – Sam Gomari Jun 07 '15 at 07:32
  • @SamGomari if you read the documentation fir std::string it becomes clear that c_str() does not return a copy. E.g. _"The pointer returned may be invalidated by further calls to other member functions that modify the object."_ – Jussi Kukkonen Jun 07 '15 at 16:13
  • To be honest, i still don't see why it would be invalidated, as you return the pointer to the char block. It it were to be invalidated you will get segmented fault as it executes...but i reckon that there is something wrong as the output of the string is truncated.... – Sam Gomari Jun 07 '15 at 21:42
  • The memory is freed in the destructor. You're using freed memory, whose value is undefined. It just so happens that the beginning of the text hasn't been overwritten by new memory yet. – andlabs Jun 07 '15 at 22:24