Permalink
Browse files

Set the default engine in init() and not constructor()

We set the default engine in init() because if a plugin is loaded
and it needs the default engine then a different engine would be returned
as the default engine would not have been set yet.
  • Loading branch information...
1 parent fe328c4 commit 672a2b04dcfd2badd81e3751b89ac5cb7b28efd1 @gregier committed Feb 12, 2012
Showing with 33 additions and 13 deletions.
  1. +14 −13 libpeas/peas-engine.c
  2. +19 −0 tests/libpeas/engine.c
View
@@ -325,6 +325,17 @@ loader_destroy (LoaderInfo *info)
static void
peas_engine_init (PeasEngine *engine)
{
+ /* Set the default engine here and not in constructor() to make sure
+ * that if a plugin is loaded and calls peas_engine_get_default()
+ * that this engine is returned and not another.
+ */
+ if (default_engine == NULL)
+ {
+ default_engine = engine;
+ g_object_add_weak_pointer (G_OBJECT (engine),
+ (gpointer *) &default_engine);
+ }
+
engine->priv = G_TYPE_INSTANCE_GET_PRIVATE (engine,
PEAS_TYPE_ENGINE,
PeasEnginePrivate);
@@ -364,8 +375,6 @@ peas_engine_constructor (GType type,
guint n_construct_params,
GObjectConstructParam *construct_params)
{
- GObject *object;
-
/* We don't support calling PeasEngine API without module support */
if (!g_module_supported ())
{
@@ -379,17 +388,9 @@ peas_engine_constructor (GType type,
"as it has been shutdown.");
}
- object = G_OBJECT_CLASS (peas_engine_parent_class)->constructor (type,
- n_construct_params,
- construct_params);
-
- if (default_engine == NULL)
- {
- default_engine = PEAS_ENGINE (object);
- g_object_add_weak_pointer (object, (gpointer *) &default_engine);
- }
-
- return object;
+ return G_OBJECT_CLASS (peas_engine_parent_class)->constructor (type,
+ n_construct_params,
+ construct_params);
}
static void
View
@@ -93,9 +93,28 @@ test_engine_dispose (PeasEngine *engine)
static void
test_engine_get_default (void)
{
+ GType the_type;
+ PeasEngine *test_engine;
+
g_assert (peas_engine_get_default () == peas_engine_get_default ());
g_object_unref (peas_engine_get_default ());
+
+
+ /* Check that the default engine is the newly created engine
+ * even when peas_engine_get_default() is called during init().
+ */
+ the_type = g_type_register_static_simple (PEAS_TYPE_ENGINE,
+ "TestEngineGetDefault",
+ sizeof (PeasEngineClass), NULL,
+ sizeof (PeasEngine),
+ (GInstanceInitFunc) peas_engine_get_default,
+ 0);
+ test_engine = PEAS_ENGINE (g_object_new (the_type, NULL));
+
+ g_assert (peas_engine_get_default () == test_engine);
+
+ g_object_unref (test_engine);
}
static void

0 comments on commit 672a2b0

Please sign in to comment.