On 03.04.2013 08:59, Edzard Egberts wrote:
>> 3. When the add menu item is clicked, a new button appears, but has
>> garbage text.
>
> Garbage text sounds like a well known problem:
>
>>      void set( string str ) { label(str.c_str()); }
>
> label() doesn't copy the string (but there is a copy_label()), but just
> saves the given const char*. But your string is local, so the content
> vanishs fast.

True.

> There are several solutions, most easy seems to be:
>
> void set( string str)
> {
>      delete[] label();

Please *DON'T* do this (above).  The widget "knows better" if it
had copied a label before (with copy_label()) or not (either the
NULL pointer or a pointer assigned with label()). And, you don't
know how the copied string was allocated (in fact it's done with
strdup(), so delete[] is wrong), and the following copy_label()
call would again call free() since you can't reset the internal
pointer and flag.

>      copy_label(str.c_str());

This is just enough! Let the widget do the rest!

So there is no need to use an own method, unless you want to use
the string type and not use copy_label(str.c_str()) in the program
code directly.


-- 

Albrecht
_______________________________________________
fltk mailing list
fltk@easysw.com
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to