Emanuele Aina wrote:
Kees Cook spiegò:
With some help from mental, this is now fixed. General warning about using the .c_str() function on Glib::ustring's returned from objects: don't use 'em: they can get trashed on the heap. Instead, use a local Glib::ustring, and call it's .c_str() when you need it.
e.g.
No: char * str = Node->contents().c_str(); do_something( str );
Yes: Glib::ustring str = Node->contents(); do_something( str.c_str() );
Just curious: any idea why?
My guess (I haven't checked) would be that Node->contents() returns a temporary object, like this for example: ----------- // Code return Glib::ustring("foobar"); } ----------- This object would only live for the duration of the statement in which Node->contents() is called and would therefore be gone when do_something is called. If that is the case something like this may have also solved this particular bug (although it depends a bit on what do_something does): ----------- do_something( Node->contents().c_str() ); -----------