I found a good workaround for this problem. First step, make your GdkPixbuf a built-in icon with
gtk_icon_theme_add_builtin_icon(). This is done instead of loading it into a GtkImage. Second step, set the icon of the GtkToolButton with
gtk_tool_button_set_icon_name(), using the name you specified in the first step. This is done instead of using gtk_tool_button_set_icon_widget().
Incidentally, I created the GdkPixbuf using cairo drawing functions. First I create an image surface with surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height). Then I create a cairo context with cr = cairo_create(surface). Then I draw on this context with cairo's drawing functions. And finally I convert the image surface to a GdkPixbuf with
gdk_pixbuf_get_from_surface(). It's a nice way to create icons programmatically.