1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
#include <glib-object.h>
/* --------------------------------- */
/* test_object_constructor_singleton */
typedef GObject MySingletonObject;
typedef GObjectClass MySingletonObjectClass;
GType my_singleton_object_get_type (void);
G_DEFINE_TYPE (MySingletonObject, my_singleton_object, G_TYPE_OBJECT)
static MySingletonObject *singleton;
static void
my_singleton_object_init (MySingletonObject *obj)
{
}
static GObject *
my_singleton_object_constructor (GType type,
guint n_construct_properties,
GObjectConstructParam *construct_params)
{
GObject *object;
if (singleton)
return g_object_ref (singleton);
object = G_OBJECT_CLASS (my_singleton_object_parent_class)->
constructor (type, n_construct_properties, construct_params);
singleton = (MySingletonObject *)object;
return object;
}
static void
my_singleton_object_finalize (MySingletonObject *obj)
{
singleton = NULL;
G_OBJECT_CLASS (my_singleton_object_parent_class)->finalize (obj);
}
static void
my_singleton_object_class_init (MySingletonObjectClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->constructor = my_singleton_object_constructor;
object_class->finalize = my_singleton_object_finalize;
}
static void
test_object_constructor_singleton (void)
{
MySingletonObject *one, *two, *three;
one = g_object_new (my_singleton_object_get_type (), NULL);
g_assert_cmpint (G_OBJECT (one)->ref_count, ==, 1);
two = g_object_new (my_singleton_object_get_type (), NULL);
g_assert (one == two);
g_assert_cmpint (G_OBJECT (two)->ref_count, ==, 2);
three = g_object_new (my_singleton_object_get_type (), NULL);
g_assert (one == three);
g_assert_cmpint (G_OBJECT (three)->ref_count, ==, 3);
g_object_add_weak_pointer (G_OBJECT (one), (gpointer *)&one);
g_object_unref (one);
g_assert (one != NULL);
g_object_unref (three);
g_object_unref (two);
g_assert (one == NULL);
}
/* ----------------------------------- */
/* test_object_constructor_infanticide */
typedef GObject MyInfanticideObject;
typedef GObjectClass MyInfanticideObjectClass;
GType my_infanticide_object_get_type (void);
G_DEFINE_TYPE (MyInfanticideObject, my_infanticide_object, G_TYPE_OBJECT)
static void
my_infanticide_object_init (MyInfanticideObject *obj)
{
}
static GObject *
my_infanticide_object_constructor (GType type,
guint n_construct_properties,
GObjectConstructParam *construct_params)
{
GObject *object;
object = G_OBJECT_CLASS (my_infanticide_object_parent_class)->
constructor (type, n_construct_properties, construct_params);
g_object_unref (object);
return NULL;
}
static void
my_infanticide_object_class_init (MyInfanticideObjectClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->constructor = my_infanticide_object_constructor;
}
static void
test_object_constructor_infanticide (void)
{
GObject *obj;
int i;
#ifndef G_ENABLE_DEBUG
g_test_skip ("skip tests that rely on debug-only warnings");
return;
#endif
g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=661576");
for (i = 0; i < 1000; i++)
{
g_test_expect_message ("GLib-GObject", G_LOG_LEVEL_CRITICAL,
"*finalized while still in-construction*");
g_test_expect_message ("GLib-GObject", G_LOG_LEVEL_CRITICAL,
"*Custom constructor*returned NULL*");
obj = g_object_new (my_infanticide_object_get_type (), NULL);
g_assert_null (obj);
g_test_assert_expected_messages ();
}
}
/* --------------------------------- */
int
main (int argc, char *argv[])
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/object/constructor/singleton", test_object_constructor_singleton);
g_test_add_func ("/object/constructor/infanticide", test_object_constructor_infanticide);
return g_test_run ();
}
|