Re: How many times can I call gtk_label_set_markup()?



Robert,

On Sat, Apr 9, 2011 at 12:05 AM, Robert Pearce <rob bdt-home demon co uk> wrote:
> Hi Igor,
>
> On Fri, 8 Apr 2011 13:33:29 -0700 you wrote:
>> >> gtk_label_set_markup( GTK_LABEL( data1 ), m_data1->str );
>> >
>> >> g_free( m_data1->str );
>> >
>> > ^^^^^^^^^^^^^^^^^^^^^^^^
>> > Really? What is m_data1? Your earlier extract suggests you reuse it after
>> > this.
>>
>> By doing g_free() I'm giving anownership of the markup string to the control.
>> Or I don't need this?
>
> No, by doing g_free you are releasing the memory allocated by
> m_data1->str. While this is probably OK because gtk_label_set_markup
> takes a copy, it is most definitely not "giving ownership". The example
> in the Gtk docs does it because the markup string is dynamically
> created and immediately disposed of, because it's being escaped. In
> your case it's pointed to by a structure member, which strongly hints
> at it not being such a temporary object. In any case, it's very bad
> practice leaving a hanging pointer to memory you've just free'd, so
> common advice is to set m_data1->str to NULL immediately after the
> g_free call. If that looks like the wrong thing in your program, then
> the g_free call is wrong.

Thank you for an explanation.

>
> Cheers,
> Rob
> _______________________________________________
> gtk-list mailing list
> gtk-list gnome org
> http://mail.gnome.org/mailman/listinfo/gtk-list
>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]