PSA: Testing GObject Lifecycles

It's a good idea to test that the lifecycle of your object is what you expect when writing your unit tests with GTest (and I hope you do). Here is a quick way to do that.

  1. static void
  2. my_object_test (void)
  3. {
  4. MyObject *obj = my_object_new();
  5. /* play with obj */
  6. g_object_add_weak_pointer(G_OBJECT(obj), (gpointer *)&obj);
  7. g_object_unref(obj);
  8. g_assert(!obj);
  9. }