Re: [gtkmm] Convert Enum to GType



Bryan Forbes writes:
 >
 > class DataColumns : public Gtk::TreeModel::ColumnRecord
 > {
 > public:
 >   Gtk::TreeModelColumn<cstr_EntityType> m_col_entity_type; // cstr_EntityType is my enum
 > };
 > 
 > class cstr_TreeStoreDnd : public Gtk::TreeStore
 > {
 > public:
 >   static DataColumns m_modelColumns;
 > };

 > When I run the program with these two classes compiled in, I get this warning:
 > (process:760): GLib-GObject-CRITICAL **: gtype.c:1871: initialization assertion failed,
 > use g_type_init() prior to this function

 > and it segfaults when I try to add things to the treestore.  I figure it's because I'm
 > using that custom enum value.

At the first glance, this doesn't seem related to your enum at all.

This variation on the simplest gtkmm application will cause you
the same grief!

#include <gtkmm.h>

int
main(int argc, char *argv[])
{
    Gtk::Window win;
    Gtk::Main kit(argc, argv);
    kit.run(win);
    return 0;
}

When win is created it checks if its type is registered with glib, and
will register its type if it hasn't been.  Well, the glib type database
is not initialized until kit (the Gtk::Main object) is created ...

My guess is that *static* DataColumns is the culprit (with or without
the TreeModelColumn instantiated on your enum).  That is m_modelColumns
behaves as Gtk::Window does in the example above, before main(), where
you presumably create a Gtk::Main object, has a chance to initialize
the type registration database ...

I assume that making m_modelColumn non static will eliminate the
runtime errors.  You will be using redundant copies thereof ...

You could consider making it a pointer, and creating it the first
time you need it, after :) Gtk::Main has been instantiated, then
reusing that instance ...

An alternative would be to follow the error message's advice:
 > use g_type_init() prior to this function

but can't give advice on how to ensure it's called before your static
object is created, and won't investigate that either, the singleton
method has worked well enough for me.



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