GNOME Bugzilla – Bug 58777
gtk_entry_set_text () doesn't allow NULL as the 2nd parameter
Last modified: 2014-04-11 08:59:02 UTC
GtkEntry doesn't allow setting text to NULL. However GtkLabel allows that. This looks inconsistent. I assume that they both should whether allow or disallow NULL. Test case: #include <glib.h> #include <gtk/gtk.h> int main (int argc, char** argv) { GtkWidget* entry; GtkWidget* label; gtk_init (&argc, &argv); label = gtk_label_new(""); gtk_label_set_text (GTK_LABEL(label), NULL); entry = gtk_entry_new (); gtk_entry_set_text (GTK_ENTRY(entry), NULL); gtk_widget_destroy (entry); gtk_widget_destroy (label); } Outputest (pid:3019): Gtk-CRITICAL **: file gtkentry.c: line 3112 (gtk_entry_set_text): assertion `text != NULL' failed t:
GtkLabel needs to allow it for backwards compat. It doesn't make any particular sense, however, and I don't think we should change GtkEntry.
Would you consider this for a second time perhaps? I find myself special-casing NULL in various occasions, where my code simply doesn't differentiate between NULL and "". All the extra effort in code can be tedious. I don't really see how this behaviour might be useful.
Created attachment 200694 [details] [review] Trivial patch +1 for silently converting a NULL argument to an empty string. I frequently bind GtkEntry:text to other string properties and always have to be mindful of the NULL issue to avoid runtime warnings.
Please add a line to the documentation along "Since 3.4 @text can be %NULL".
I still don't think it makes any sense to mix up NULL and ""
Created attachment 209713 [details] [review] Revised trivial patch Mentions the change in the API docs and adds an (allow-none) annotation to @text.
(In reply to comment #5) > I still don't think it makes any sense to mix up NULL and "" We've had a similar discussion before: https://bugzilla.gnome.org/show_bug.cgi?id=399880 While there is clearly a difference, in real terms, there isn't. The string is either empty or it isn't. In this particular case, it's really just a courtesy to help the app developer. I would advocate consistency too where possible. Just my 2p ;)
the inconsistency here is that gtklabel silently allows NULL, not that gtkentry doesn't.
Could some GTK+ developer comment whether this inconsistency is wanted, and if it makes sense?
comment 1 contains that information
*** Bug 684425 has been marked as a duplicate of this bug. ***