Re: Why is cast from NULL illegal?



Hi,

Lars Clausen <lrclause cs uiuc edu> writes:

> After changing Dia's code to have more static type checking, I'm getting a
> number of errors about invalid casts from NULL.  But surely NULL can be any
> type, so any cast of it should be valid.

surely you can cast NULL to any type but you are using type-checking
casts here. These casts are supposed to guarantee that the variable points
to a valid GObject of a specific GType. This can't possibly work with a
NULL pointer.

> The function in question here is gtk_item_factory_get_item(), which despite
> always returning NULL or a GtkMenuItem* has a return type of GtkWidget*.
> After some nasty bugs caused by the lack of static type check for this, I
> decided to make better use of the compilers typechecking.  But if I say
> GTK_MENU_ITEM(gtk_item_factory_get_item(...)), I get warnings whenever it
> returns NULL.  Having to check for NULL before casting would be
> cumbersome.

gtk_item_factory_get_item() returns a GtkWidget pointer so the return
value should be assigned to a GtkWidget pointer as well. If you later
need to use the widget as a GtkMenu, you should do the type-checking
cast there (after checking that it is non-NULL).


Salut, Sven



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