[gimp] app: use a lot of g_clear_object() and g_clear_pointer()



commit 0cb3e75f791c16206db791fb27edbbc8c248221a
Author: Michael Natterer <mitch gimp org>
Date:   Sat Jul 15 18:38:01 2017 +0200

    app: use a lot of g_clear_object() and g_clear_pointer()
    
    More than 2000 lines of code less in app/, instead of
    
    if (instance->member)
      {
        g_object_unref/g_free/g_whatever (instance->member);
        instance->member = NULL;
      }
    
    we now simply use
    
    g_clear_object/pointer (&instance->member);

 app/actions/actions.c                          |    3 +-
 app/config/gimpcoreconfig.c                    |   11 +-
 app/config/gimpdialogconfig.c                  |   35 +----
 app/config/gimpdisplayconfig.c                 |    7 +-
 app/config/gimprc.c                            |   13 +--
 app/core/gimp-data-factories.c                 |   55 +------
 app/core/gimp-modules.c                        |    6 +-
 app/core/gimp.c                                |  135 +++---------------
 app/core/gimpbrush.c                           |  112 +++------------
 app/core/gimpbrushclipboard.c                  |   13 +--
 app/core/gimpbrushgenerated.c                  |    3 +-
 app/core/gimpbuffer.c                          |   16 +--
 app/core/gimpcontext.c                         |  180 ++++--------------------
 app/core/gimpdata.c                            |   24 +---
 app/core/gimpdatafactory.c                     |   30 +---
 app/core/gimpdrawable-floating-selection.c     |    7 +-
 app/core/gimpdrawable-shadow.c                 |    6 +-
 app/core/gimpdrawable.c                        |   19 +--
 app/core/gimpdrawablefilter.c                  |   20 +---
 app/core/gimpdrawablemodundo.c                 |    6 +-
 app/core/gimpdrawableundo.c                    |   13 +--
 app/core/gimpdynamics.c                        |   22 ++--
 app/core/gimpdynamicsoutput.c                  |   14 +-
 app/core/gimpfilter.c                          |    6 +-
 app/core/gimpfilteredcontainer.c               |    6 +-
 app/core/gimpfilterstack.c                     |    6 +-
 app/core/gimpgrouplayer.c                      |   16 +--
 app/core/gimpguideundo.c                       |    6 +-
 app/core/gimpidtable.c                         |    6 +-
 app/core/gimpimage-color-profile.c             |   33 +----
 app/core/gimpimage-colormap.c                  |    7 +-
 app/core/gimpimage.c                           |  137 +++---------------
 app/core/gimpimagefile.c                       |   42 +-----
 app/core/gimpimageundo.c                       |   34 +----
 app/core/gimpitem.c                            |    6 +-
 app/core/gimpitempropundo.c                    |   20 +---
 app/core/gimpitemtree.c                        |   13 +--
 app/core/gimpitemundo.c                        |    6 +-
 app/core/gimplayer.c                           |    6 +-
 app/core/gimplayermaskundo.c                   |    6 +-
 app/core/gimpmaskundo.c                        |    6 +-
 app/core/gimppaintinfo.c                       |    9 +-
 app/core/gimppattern.c                         |    6 +-
 app/core/gimppatternclipboard.c                |    6 +-
 app/core/gimppdbprogress.c                     |   20 +---
 app/core/gimpprojection.c                      |   34 +----
 app/core/gimpsamplepointundo.c                 |    6 +-
 app/core/gimpstrokeoptions.c                   |    6 +-
 app/core/gimpsubprogress.c                     |    6 +-
 app/core/gimpsymmetry-mandala.c                |   10 +-
 app/core/gimpsymmetry-mirror.c                 |   23 +---
 app/core/gimpsymmetry.c                        |    6 +-
 app/core/gimptaggedcontainer.c                 |    6 +-
 app/core/gimptemplate.c                        |   20 +---
 app/core/gimptoolinfo.c                        |   47 +-----
 app/core/gimptooloptions.c                     |    6 +-
 app/core/gimptoolpreset.c                      |    9 +-
 app/core/gimptreehandler.c                     |    7 +-
 app/core/gimpundo.c                            |    9 +-
 app/core/gimpundostack.c                       |    6 +-
 app/core/gimpviewable.c                        |   46 +-----
 app/dialogs/dialogs.c                          |    6 +-
 app/display/gimpcanvas.c                       |   12 +--
 app/display/gimpcanvasgrid.c                   |    6 +-
 app/display/gimpcanvasitem.c                   |    5 +-
 app/display/gimpcanvasproxygroup.c             |    6 +-
 app/display/gimpdisplay.c                      |    9 +-
 app/display/gimpdisplayshell-handlers.c        |   12 +--
 app/display/gimpdisplayshell-items.c           |    6 +-
 app/display/gimpdisplayshell-profile.c         |   23 +---
 app/display/gimpdisplayshell-selection.c       |   22 +---
 app/display/gimpdisplayshell.c                 |   81 +++--------
 app/display/gimpimagewindow.c                  |    6 +-
 app/display/gimpstatusbar.c                    |   37 +----
 app/display/gimptoolgui.c                      |   39 +-----
 app/display/gimptoolline.c                     |   29 +---
 app/display/gimptoolpolygon.c                  |    6 +-
 app/gegl/gimptilehandlervalidate.c             |   10 +-
 app/operations/gimpoperationcagecoefcalc.c     |    6 +-
 app/operations/gimpoperationcagetransform.c    |    6 +-
 app/operations/gimpoperationequalize.c         |   13 +--
 app/operations/gimpoperationhistogramsink.c    |    6 +-
 app/operations/gimpoperationpointfilter.c      |    6 +-
 app/operations/gimpoperationprofiletransform.c |   26 +---
 app/paint/gimp-paint.c                         |    3 +-
 app/paint/gimpairbrush.c                       |   14 +--
 app/paint/gimpbrushcore.c                      |   63 ++-------
 app/paint/gimpmybrushsurface.c                 |   42 +++---
 app/paint/gimppaintcore.c                      |   81 ++---------
 app/paint/gimpperspectiveclone.c               |   12 +-
 app/pdb/gimppdbcontext.c                       |   13 +--
 app/plug-in/gimpplugin-progress.c              |   13 +--
 app/plug-in/gimpplugin.c                       |   43 ++-----
 app/plug-in/gimppluginmanager-call.c           |    6 +-
 app/plug-in/gimppluginmanager.c                |   13 +--
 app/plug-in/gimppluginprocframe.c              |   32 +----
 app/text/gimp-fonts.c                          |    3 +-
 app/text/gimpfont.c                            |   13 +--
 app/text/gimptextlayer.c                       |    9 +-
 app/text/gimptextundo.c                        |    6 +-
 app/tools/gimpblendtool.c                      |   13 +--
 app/tools/gimpcagetool.c                       |   60 ++------
 app/tools/gimpcolorpickertool.c                |   13 +--
 app/tools/gimpdrawtool.c                       |    6 +-
 app/tools/gimpfiltertool.c                     |  120 +++-------------
 app/tools/gimpforegroundselecttool.c           |   33 +----
 app/tools/gimpiscissorstool.c                  |   13 +--
 app/tools/gimplevelstool.c                     |    6 +-
 app/tools/gimpnpointdeformationtool.c          |   25 +---
 app/tools/gimpoperationtool.c                  |   20 +--
 app/tools/gimprectangleoptions.c               |    4 +-
 app/tools/gimpregionselecttool.c               |   36 +----
 app/tools/gimpseamlessclonetool.c              |   24 +---
 app/tools/gimptexttool.c                       |   22 +---
 app/tools/gimptool.c                           |   41 +-----
 app/tools/gimptransformtool.c                  |   13 +--
 app/tools/gimpvectortool.c                     |    3 +-
 app/tools/gimpwarptool.c                       |   26 +---
 app/tools/tool_manager.c                       |    7 +-
 app/vectors/gimpvectorsmodundo.c               |    6 +-
 app/widgets/gimpaction.c                       |   20 +---
 app/widgets/gimpactionview.c                   |    9 +-
 app/widgets/gimpbuffersourcebox.c              |   20 +---
 app/widgets/gimpcellrendererviewable.c         |    6 +-
 app/widgets/gimpcircle.c                       |   18 +--
 app/widgets/gimpclipboard.c                    |   27 +---
 app/widgets/gimpcolordisplayeditor.c           |   20 +---
 app/widgets/gimpcolorframe.c                   |   18 +--
 app/widgets/gimpcolormapeditor.c               |    6 +-
 app/widgets/gimpcolorselectorpalette.c         |    3 +-
 app/widgets/gimpcombotagentry.c                |   12 +--
 app/widgets/gimpcontainereditor.c              |   33 +----
 app/widgets/gimpcontainerpopup.c               |   28 +---
 app/widgets/gimpcontainertreeview.c            |    6 +-
 app/widgets/gimpcontrollereditor.c             |    9 +-
 app/widgets/gimpcontrollerinfo.c               |   13 +--
 app/widgets/gimpcontrollerlist.c               |    6 +-
 app/widgets/gimpcontrollers.c                  |    4 +-
 app/widgets/gimpcurveview.c                    |   39 +-----
 app/widgets/gimpdasheditor.c                   |   13 +--
 app/widgets/gimpdatafactoryview.c              |   19 +--
 app/widgets/gimpdeviceinfo.c                   |   19 +--
 app/widgets/gimpdeviceinfoeditor.c             |    6 +-
 app/widgets/gimpdockbook.c                     |    9 +-
 app/widgets/gimpdockwindow.c                   |   26 +---
 app/widgets/gimpdynamicseditor.c               |    6 +-
 app/widgets/gimpdynamicsoutputeditor.c         |    6 +-
 app/widgets/gimpeditor.c                       |   21 +---
 app/widgets/gimpfgbgeditor.c                   |   35 +----
 app/widgets/gimpfgbgview.c                     |   12 +--
 app/widgets/gimpfilleditor.c                   |    6 +-
 app/widgets/gimpgrideditor.c                   |   13 +--
 app/widgets/gimphistogrameditor.c              |   28 +---
 app/widgets/gimpiconpicker.c                   |   40 +-----
 app/widgets/gimplanguageentry.c                |   12 +--
 app/widgets/gimppdbdialog.c                    |   32 +----
 app/widgets/gimppickablebutton.c               |    6 +-
 app/widgets/gimppickablepopup.c                |   13 +--
 app/widgets/gimppluginview.c                   |    6 +-
 app/widgets/gimpprocedureaction.c              |    6 +-
 app/widgets/gimpsettingsbox.c                  |   27 +---
 app/widgets/gimpsettingseditor.c               |   13 +--
 app/widgets/gimpspinscale.c                    |   50 +------
 app/widgets/gimptagentry.c                     |    5 +-
 app/widgets/gimptagpopup.c                     |   20 +---
 app/widgets/gimptemplateeditor.c               |    6 +-
 app/widgets/gimptexteditor.c                   |    7 +-
 app/widgets/gimptextstyleeditor.c              |   27 +---
 app/widgets/gimpthumbbox.c                     |    6 +-
 app/widgets/gimptoolbox.c                      |    6 +-
 app/widgets/gimptoolpreseteditor.c             |    6 +-
 app/widgets/gimpundoeditor.c                   |    9 +-
 app/widgets/gimpview.c                         |    6 +-
 app/widgets/gimpviewrenderer.c                 |   83 ++---------
 174 files changed, 678 insertions(+), 2859 deletions(-)
---
diff --git a/app/actions/actions.c b/app/actions/actions.c
index da61e03..ecae6d5 100644
--- a/app/actions/actions.c
+++ b/app/actions/actions.c
@@ -270,8 +270,7 @@ actions_exit (Gimp *gimp)
   g_return_if_fail (global_action_factory != NULL);
   g_return_if_fail (global_action_factory->gimp == gimp);
 
-  g_object_unref (global_action_factory);
-  global_action_factory = NULL;
+  g_clear_object (&global_action_factory);
 }
 
 Gimp *
diff --git a/app/config/gimpcoreconfig.c b/app/config/gimpcoreconfig.c
index 4e21577..31dec43 100644
--- a/app/config/gimpcoreconfig.c
+++ b/app/config/gimpcoreconfig.c
@@ -726,14 +726,9 @@ gimp_core_config_finalize (GObject *object)
   g_free (core_config->plug_in_rc_path);
   g_free (core_config->import_raw_plug_in);
 
-  if (core_config->default_image)
-    g_object_unref (core_config->default_image);
-
-  if (core_config->default_grid)
-    g_object_unref (core_config->default_grid);
-
-  if (core_config->color_management)
-    g_object_unref (core_config->color_management);
+  g_clear_object (&core_config->default_image);
+  g_clear_object (&core_config->default_grid);
+  g_clear_object (&core_config->color_management);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/config/gimpdialogconfig.c b/app/config/gimpdialogconfig.c
index 3171dbf..6588585 100644
--- a/app/config/gimpdialogconfig.c
+++ b/app/config/gimpdialogconfig.c
@@ -558,35 +558,12 @@ gimp_dialog_config_finalize (GObject *object)
 {
   GimpDialogConfig *config = GIMP_DIALOG_CONFIG (object);
 
-  if (config->layer_new_name)
-    {
-      g_free (config->layer_new_name);
-      config->layer_new_name = NULL;
-    }
+  g_clear_pointer (&config->layer_new_name,   g_free);
+  g_clear_pointer (&config->channel_new_name, g_free);
+  g_clear_pointer (&config->vectors_new_name, g_free);
 
-  if (config->channel_new_name)
-    {
-      g_free (config->channel_new_name);
-      config->channel_new_name = NULL;
-    }
-
-  if (config->vectors_new_name)
-    {
-      g_free (config->vectors_new_name);
-      config->vectors_new_name = NULL;
-    }
-
-  if (config->fill_options)
-    {
-      g_object_unref (config->fill_options);
-      config->fill_options = NULL;
-    }
-
-  if (config->stroke_options)
-    {
-      g_object_unref (config->stroke_options);
-      config->stroke_options = NULL;
-    }
+  g_clear_object (&config->fill_options);
+  g_clear_object (&config->stroke_options);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/config/gimpdisplayconfig.c b/app/config/gimpdisplayconfig.c
index eb458fc..9468b63 100644
--- a/app/config/gimpdisplayconfig.c
+++ b/app/config/gimpdisplayconfig.c
@@ -367,11 +367,8 @@ gimp_display_config_finalize (GObject *object)
   g_free (display_config->image_title_format);
   g_free (display_config->image_status_format);
 
-  if (display_config->default_view)
-    g_object_unref (display_config->default_view);
-
-  if (display_config->default_fullscreen_view)
-    g_object_unref (display_config->default_fullscreen_view);
+  g_clear_object (&display_config->default_view);
+  g_clear_object (&display_config->default_fullscreen_view);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/config/gimprc.c b/app/config/gimprc.c
index 998f784..16ee6d7 100644
--- a/app/config/gimprc.c
+++ b/app/config/gimprc.c
@@ -130,17 +130,8 @@ gimp_rc_finalize (GObject *object)
 {
   GimpRc *rc = GIMP_RC (object);
 
-  if (rc->system_gimprc)
-    {
-      g_object_unref (rc->system_gimprc);
-      rc->system_gimprc = NULL;
-    }
-
-  if (rc->user_gimprc)
-    {
-      g_object_unref (rc->user_gimprc);
-      rc->user_gimprc = NULL;
-    }
+  g_clear_object (&rc->system_gimprc);
+  g_clear_object (&rc->user_gimprc);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/core/gimp-data-factories.c b/app/core/gimp-data-factories.c
index 784906e..4738b74 100644
--- a/app/core/gimp-data-factories.c
+++ b/app/core/gimp-data-factories.c
@@ -250,53 +250,14 @@ gimp_data_factories_exit (Gimp *gimp)
 {
   g_return_if_fail (GIMP_IS_GIMP (gimp));
 
-  if (gimp->brush_factory)
-    {
-      g_object_unref (gimp->brush_factory);
-      gimp->brush_factory = NULL;
-    }
-
-  if (gimp->dynamics_factory)
-    {
-      g_object_unref (gimp->dynamics_factory);
-      gimp->dynamics_factory = NULL;
-    }
-
-  if (gimp->mybrush_factory)
-    {
-      g_object_unref (gimp->mybrush_factory);
-      gimp->mybrush_factory = NULL;
-    }
-
-  if (gimp->pattern_factory)
-    {
-      g_object_unref (gimp->pattern_factory);
-      gimp->pattern_factory = NULL;
-    }
-
-  if (gimp->gradient_factory)
-    {
-      g_object_unref (gimp->gradient_factory);
-      gimp->gradient_factory = NULL;
-    }
-
-  if (gimp->palette_factory)
-    {
-      g_object_unref (gimp->palette_factory);
-      gimp->palette_factory = NULL;
-    }
-
-  if (gimp->tool_preset_factory)
-    {
-      g_object_unref (gimp->tool_preset_factory);
-      gimp->tool_preset_factory = NULL;
-    }
-
-  if (gimp->tag_cache)
-    {
-      g_object_unref (gimp->tag_cache);
-      gimp->tag_cache = NULL;
-    }
+  g_clear_object (&gimp->brush_factory);
+  g_clear_object (&gimp->dynamics_factory);
+  g_clear_object (&gimp->mybrush_factory);
+  g_clear_object (&gimp->pattern_factory);
+  g_clear_object (&gimp->gradient_factory);
+  g_clear_object (&gimp->palette_factory);
+  g_clear_object (&gimp->tool_preset_factory);
+  g_clear_object (&gimp->tag_cache);
 }
 
 gint64
diff --git a/app/core/gimp-modules.c b/app/core/gimp-modules.c
index 23df89b..5adfdca 100644
--- a/app/core/gimp-modules.c
+++ b/app/core/gimp-modules.c
@@ -53,11 +53,7 @@ gimp_modules_exit (Gimp *gimp)
 {
   g_return_if_fail (GIMP_IS_GIMP (gimp));
 
-  if (gimp->module_db)
-    {
-      g_object_unref (gimp->module_db);
-      gimp->module_db = NULL;
-    }
+  g_clear_object (&gimp->module_db);
 }
 
 void
diff --git a/app/core/gimp.c b/app/core/gimp.c
index 25ee206..ef3c933 100644
--- a/app/core/gimp.c
+++ b/app/core/gimp.c
@@ -343,25 +343,12 @@ gimp_dispose (GObject *object)
 
   gimp_filter_history_clear (gimp);
 
-  if (gimp->edit_config)
-    {
-      g_object_unref (gimp->edit_config);
-      gimp->edit_config = NULL;
-    }
-
-  if (gimp->config)
-    {
-      g_object_unref (gimp->config);
-      gimp->config = NULL;
-    }
+  g_clear_object (&gimp->edit_config);
+  g_clear_object (&gimp->config);
 
   gimp_contexts_exit (gimp);
 
-  if (gimp->image_new_last_template)
-    {
-      g_object_unref (gimp->image_new_last_template);
-      gimp->image_new_last_template = NULL;
-    }
+  g_clear_object (&gimp->image_new_last_template);
 
   G_OBJECT_CLASS (parent_class)->dispose (object);
 }
@@ -388,17 +375,8 @@ gimp_finalize (GObject *object)
   standards = g_list_prepend (standards,
                               gimp_palette_get_standard (gimp->user_context));
 
-  if (gimp->templates)
-    {
-      g_object_unref (gimp->templates);
-      gimp->templates = NULL;
-    }
-
-  if (gimp->documents)
-    {
-      g_object_unref (gimp->documents);
-      gimp->documents = NULL;
-    }
+  g_clear_object (&gimp->templates);
+  g_clear_object (&gimp->documents);
 
   gimp_tool_info_set_standard (gimp, NULL);
 
@@ -406,92 +384,35 @@ gimp_finalize (GObject *object)
     {
       gimp_container_foreach (gimp->tool_info_list,
                               (GFunc) g_object_run_dispose, NULL);
-      g_object_unref (gimp->tool_info_list);
-      gimp->tool_info_list = NULL;
+      g_clear_object (&gimp->tool_info_list);
     }
 
   xcf_exit (gimp);
 
-  if (gimp->pdb)
-    {
-      g_object_unref (gimp->pdb);
-      gimp->pdb = NULL;
-    }
+  g_clear_object (&gimp->pdb);
 
   gimp_data_factories_exit (gimp);
 
   gimp_fonts_exit (gimp);
 
-  if (gimp->named_buffers)
-    {
-      g_object_unref (gimp->named_buffers);
-      gimp->named_buffers = NULL;
-    }
-
-  if (gimp->clipboard_buffer)
-    {
-      g_object_unref (gimp->clipboard_buffer);
-      gimp->clipboard_buffer = NULL;
-    }
-
-  if (gimp->clipboard_image)
-    {
-      g_object_unref (gimp->clipboard_image);
-      gimp->clipboard_image = NULL;
-    }
-
-  if (gimp->displays)
-    {
-      g_object_unref (gimp->displays);
-      gimp->displays = NULL;
-    }
-
-  if (gimp->item_table)
-    {
-      g_object_unref (gimp->item_table);
-      gimp->item_table = NULL;
-    }
-
-  if (gimp->image_table)
-    {
-      g_object_unref (gimp->image_table);
-      gimp->image_table = NULL;
-    }
-
-  if (gimp->images)
-    {
-      g_object_unref (gimp->images);
-      gimp->images = NULL;
-    }
-
-  if (gimp->plug_in_manager)
-    {
-      g_object_unref (gimp->plug_in_manager);
-      gimp->plug_in_manager = NULL;
-    }
+  g_clear_object (&gimp->named_buffers);
+  g_clear_object (&gimp->clipboard_buffer);
+  g_clear_object (&gimp->clipboard_image);
+  g_clear_object (&gimp->displays);
+  g_clear_object (&gimp->item_table);
+  g_clear_object (&gimp->image_table);
+  g_clear_object (&gimp->images);
+  g_clear_object (&gimp->plug_in_manager);
 
   if (gimp->module_db)
     gimp_modules_exit (gimp);
 
   gimp_paint_exit (gimp);
 
-  if (gimp->parasites)
-    {
-      g_object_unref (gimp->parasites);
-      gimp->parasites = NULL;
-    }
-
-  if (gimp->default_folder)
-    {
-      g_object_unref (gimp->default_folder);
-      gimp->default_folder = NULL;
-    }
+  g_clear_object (&gimp->parasites);
+  g_clear_object (&gimp->default_folder);
 
-  if (gimp->session_name)
-    {
-      g_free (gimp->session_name);
-      gimp->session_name = NULL;
-    }
+  g_clear_pointer (&gimp->session_name, g_free);
 
   if (gimp->context_list)
     {
@@ -1005,14 +926,8 @@ gimp_set_clipboard_image (Gimp      *gimp,
   if (image)
     g_object_ref (image);
 
-  if (gimp->clipboard_buffer)
-    {
-      g_object_unref (gimp->clipboard_buffer);
-      gimp->clipboard_buffer = NULL;
-    }
-
-  if (gimp->clipboard_image)
-    g_object_unref (gimp->clipboard_image);
+  g_clear_object (&gimp->clipboard_buffer);
+  g_clear_object (&gimp->clipboard_image);
 
   gimp->clipboard_image = image;
 
@@ -1038,14 +953,8 @@ gimp_set_clipboard_buffer (Gimp       *gimp,
   if (buffer)
     g_object_ref (buffer);
 
-  if (gimp->clipboard_image)
-    {
-      g_object_unref (gimp->clipboard_image);
-      gimp->clipboard_image = NULL;
-    }
-
-  if (gimp->clipboard_buffer)
-    g_object_unref (gimp->clipboard_buffer);
+  g_clear_object (&gimp->clipboard_image);
+  g_clear_object (&gimp->clipboard_buffer);
 
   gimp->clipboard_buffer = buffer;
 
diff --git a/app/core/gimpbrush.c b/app/core/gimpbrush.c
index 907244e..01a8166 100644
--- a/app/core/gimpbrush.c
+++ b/app/core/gimpbrush.c
@@ -182,46 +182,14 @@ gimp_brush_finalize (GObject *object)
 {
   GimpBrush *brush = GIMP_BRUSH (object);
 
-  if (brush->priv->mask)
-    {
-      gimp_temp_buf_unref (brush->priv->mask);
-      brush->priv->mask = NULL;
-    }
-
-  if (brush->priv->pixmap)
-    {
-      gimp_temp_buf_unref (brush->priv->pixmap);
-      brush->priv->pixmap = NULL;
-    }
-  if (brush->priv->blured_mask)
-    {
-      gimp_temp_buf_unref (brush->priv->blured_mask);
-      brush->priv->blured_mask = NULL;
-    }
+  g_clear_pointer (&brush->priv->mask,          gimp_temp_buf_unref);
+  g_clear_pointer (&brush->priv->pixmap,        gimp_temp_buf_unref);
+  g_clear_pointer (&brush->priv->blured_mask,   gimp_temp_buf_unref);
+  g_clear_pointer (&brush->priv->blured_pixmap, gimp_temp_buf_unref);
 
-  if (brush->priv->blured_pixmap)
-    {
-      gimp_temp_buf_unref (brush->priv->blured_pixmap);
-      brush->priv->blured_pixmap = NULL;
-    }
-
-  if (brush->priv->mask_cache)
-    {
-      g_object_unref (brush->priv->mask_cache);
-      brush->priv->mask_cache = NULL;
-    }
-
-  if (brush->priv->pixmap_cache)
-    {
-      g_object_unref (brush->priv->pixmap_cache);
-      brush->priv->pixmap_cache = NULL;
-    }
-
-  if (brush->priv->boundary_cache)
-    {
-      g_object_unref (brush->priv->boundary_cache);
-      brush->priv->boundary_cache = NULL;
-    }
+  g_clear_object (&brush->priv->mask_cache);
+  g_clear_object (&brush->priv->pixmap_cache);
+  g_clear_object (&brush->priv->boundary_cache);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -429,17 +397,8 @@ gimp_brush_dirty (GimpData *data)
   if (brush->priv->boundary_cache)
     gimp_brush_cache_clear (brush->priv->boundary_cache);
 
-   if (brush->priv->blured_mask)
-    {
-      gimp_temp_buf_unref (brush->priv->blured_mask);
-      brush->priv->blured_mask = NULL;
-    }
-
-  if (brush->priv->blured_pixmap)
-    {
-      gimp_temp_buf_unref (brush->priv->blured_pixmap);
-      brush->priv->blured_pixmap = NULL;
-    }
+  g_clear_pointer (&brush->priv->blured_mask,   gimp_temp_buf_unref);
+  g_clear_pointer (&brush->priv->blured_pixmap, gimp_temp_buf_unref);
 
   GIMP_DATA_CLASS (parent_class)->dirty (data);
 }
@@ -466,26 +425,12 @@ gimp_brush_real_begin_use (GimpBrush *brush)
 static void
 gimp_brush_real_end_use (GimpBrush *brush)
 {
-  g_object_unref (brush->priv->mask_cache);
-  brush->priv->mask_cache = NULL;
-
-  g_object_unref (brush->priv->pixmap_cache);
-  brush->priv->pixmap_cache = NULL;
-
-  g_object_unref (brush->priv->boundary_cache);
-  brush->priv->boundary_cache = NULL;
-
-  if (brush->priv->blured_mask)
-    {
-      gimp_temp_buf_unref (brush->priv->blured_mask);
-      brush->priv->blured_mask = NULL;
-    }
+  g_clear_object (&brush->priv->mask_cache);
+  g_clear_object (&brush->priv->pixmap_cache);
+  g_clear_object (&brush->priv->boundary_cache);
 
-  if (brush->priv->blured_pixmap)
-    {
-      gimp_temp_buf_unref (brush->priv->blured_pixmap);
-      brush->priv->blured_pixmap = NULL;
-    }
+  g_clear_pointer (&brush->priv->blured_mask,   gimp_temp_buf_unref);
+  g_clear_pointer (&brush->priv->blured_pixmap, gimp_temp_buf_unref);
 }
 
 static GimpBrush *
@@ -911,17 +856,8 @@ void
 gimp_brush_flush_blur_caches (GimpBrush *brush)
 {
 #if 0
-  if (brush->priv->blured_mask)
-    {
-      gimp_temp_buf_unref (brush->priv->blured_mask);
-      brush->priv->blured_mask = NULL;
-    }
-
-  if (brush->priv->blured_pixmap)
-    {
-      gimp_temp_buf_unref (brush->priv->blured_pixmap);
-      brush->priv->blured_pixmap = NULL;
-    }
+  g_clear_pointer (&brush->priv->blured_mask,   gimp_temp_buf_unref);
+  g_clear_pointer (&brush->priv->blured_pixmap, gimp_temp_buf_unref);
 
   if (brush->priv->mask_cache)
     gimp_brush_cache_clear (brush->priv->mask_cache);
@@ -946,14 +882,13 @@ gint
 gimp_brush_get_width (GimpBrush *brush)
 {
   g_return_val_if_fail (GIMP_IS_BRUSH (brush), 0);
+
   if (brush->priv->blured_mask)
-    {
-      return gimp_temp_buf_get_width (brush->priv->blured_mask);
-    }
+    return gimp_temp_buf_get_width (brush->priv->blured_mask);
+
   if (brush->priv->blured_pixmap)
-    {
-      return gimp_temp_buf_get_width (brush->priv->blured_pixmap);
-    }
+    return gimp_temp_buf_get_width (brush->priv->blured_pixmap);
+
   return gimp_temp_buf_get_width (brush->priv->mask);
 }
 
@@ -963,14 +898,11 @@ gimp_brush_get_height (GimpBrush *brush)
   g_return_val_if_fail (GIMP_IS_BRUSH (brush), 0);
 
   if (brush->priv->blured_mask)
-  {
     return gimp_temp_buf_get_height (brush->priv->blured_mask);
-  }
 
   if (brush->priv->blured_pixmap)
-  {
     return gimp_temp_buf_get_height (brush->priv->blured_pixmap);
-  }
+
   return gimp_temp_buf_get_height (brush->priv->mask);
 }
 
diff --git a/app/core/gimpbrushclipboard.c b/app/core/gimpbrushclipboard.c
index 7fc3c3e..13574f7 100644
--- a/app/core/gimpbrushclipboard.c
+++ b/app/core/gimpbrushclipboard.c
@@ -210,17 +210,8 @@ gimp_brush_clipboard_changed (Gimp      *gimp,
   gint        width;
   gint        height;
 
-  if (brush->priv->mask)
-    {
-      gimp_temp_buf_unref (brush->priv->mask);
-      brush->priv->mask = NULL;
-    }
-
-  if (brush->priv->pixmap)
-    {
-      gimp_temp_buf_unref (brush->priv->pixmap);
-      brush->priv->pixmap = NULL;
-    }
+  g_clear_pointer (&brush->priv->mask,   gimp_temp_buf_unref);
+  g_clear_pointer (&brush->priv->pixmap, gimp_temp_buf_unref);
 
   paste = gimp_get_clipboard_object (gimp);
 
diff --git a/app/core/gimpbrushgenerated.c b/app/core/gimpbrushgenerated.c
index c832688..8d9f8a9 100644
--- a/app/core/gimpbrushgenerated.c
+++ b/app/core/gimpbrushgenerated.c
@@ -254,8 +254,7 @@ gimp_brush_generated_dirty (GimpData *data)
   GimpBrushGenerated *brush  = GIMP_BRUSH_GENERATED (data);
   GimpBrush          *gbrush = GIMP_BRUSH (brush);
 
-  if (gbrush->priv->mask)
-    gimp_temp_buf_unref (gbrush->priv->mask);
+  g_clear_pointer (&gbrush->priv->mask, gimp_temp_buf_unref);
 
   gbrush->priv->mask = gimp_brush_generated_calc (brush,
                                                   brush->shape,
diff --git a/app/core/gimpbuffer.c b/app/core/gimpbuffer.c
index 4f8a179..7c93607 100644
--- a/app/core/gimpbuffer.c
+++ b/app/core/gimpbuffer.c
@@ -123,11 +123,7 @@ gimp_buffer_finalize (GObject *object)
 {
   GimpBuffer *buffer = GIMP_BUFFER (object);
 
-  if (buffer->buffer)
-    {
-      g_object_unref (buffer->buffer);
-      buffer->buffer = NULL;
-    }
+  g_clear_object (&buffer->buffer);
 
   gimp_buffer_set_color_profile (buffer, NULL);
 
@@ -528,16 +524,10 @@ gimp_buffer_set_color_profile (GimpBuffer       *buffer,
 
   if (profile != buffer->color_profile)
     {
-      if (buffer->color_profile)
-        {
-          g_object_unref (buffer->color_profile);
-          buffer->color_profile = NULL;
-        }
+      g_clear_object (&buffer->color_profile);
 
       if (profile)
-        {
-          buffer->color_profile = g_object_ref (profile);
-        }
+        buffer->color_profile = g_object_ref (profile);
     }
 }
 
diff --git a/app/core/gimpcontext.c b/app/core/gimpcontext.c
index fb8ea03..89b5f81 100644
--- a/app/core/gimpcontext.c
+++ b/app/core/gimpcontext.c
@@ -968,83 +968,19 @@ gimp_context_dispose (GObject *object)
       context->gimp = NULL;
     }
 
-  if (context->tool_info)
-    {
-      g_object_unref (context->tool_info);
-      context->tool_info = NULL;
-    }
-
-  if (context->paint_info)
-    {
-      g_object_unref (context->paint_info);
-      context->paint_info = NULL;
-    }
-
-  if (context->brush)
-    {
-      g_object_unref (context->brush);
-      context->brush = NULL;
-    }
-
-  if (context->dynamics)
-    {
-      g_object_unref (context->dynamics);
-      context->dynamics = NULL;
-    }
-
-  if (context->mybrush)
-    {
-      g_object_unref (context->mybrush);
-      context->mybrush = NULL;
-    }
-
-  if (context->pattern)
-    {
-      g_object_unref (context->pattern);
-      context->pattern = NULL;
-    }
-
-  if (context->gradient)
-    {
-      g_object_unref (context->gradient);
-      context->gradient = NULL;
-    }
-
-  if (context->palette)
-    {
-      g_object_unref (context->palette);
-      context->palette = NULL;
-    }
-
-  if (context->tool_preset)
-    {
-      g_object_unref (context->tool_preset);
-      context->tool_preset = NULL;
-    }
-
-  if (context->font)
-    {
-      g_object_unref (context->font);
-      context->font = NULL;
-    }
-
-  if (context->buffer)
-    {
-      g_object_unref (context->buffer);
-      context->buffer = NULL;
-    }
-
-  if (context->imagefile)
-    {
-      g_object_unref (context->imagefile);
-      context->imagefile = NULL;
-    }
-
-  if (context->template)
-    {
-      g_object_unref (context->template);
-      context->template = NULL;
-    }
+  g_clear_object (&context->tool_info);
+  g_clear_object (&context->paint_info);
+  g_clear_object (&context->brush);
+  g_clear_object (&context->dynamics);
+  g_clear_object (&context->mybrush);
+  g_clear_object (&context->pattern);
+  g_clear_object (&context->gradient);
+  g_clear_object (&context->palette);
+  g_clear_object (&context->tool_preset);
+  g_clear_object (&context->font);
+  g_clear_object (&context->buffer);
+  g_clear_object (&context->imagefile);
+  g_clear_object (&context->template);
 
   G_OBJECT_CLASS (parent_class)->dispose (object);
 }
@@ -1058,83 +994,19 @@ gimp_context_finalize (GObject *object)
   context->image   = NULL;
   context->display = NULL;
 
-  if (context->tool_name)
-    {
-      g_free (context->tool_name);
-      context->tool_name = NULL;
-    }
-
-  if (context->paint_name)
-    {
-      g_free (context->paint_name);
-      context->paint_name = NULL;
-    }
-
-  if (context->brush_name)
-    {
-      g_free (context->brush_name);
-      context->brush_name = NULL;
-    }
-
-  if (context->dynamics_name)
-    {
-      g_free (context->dynamics_name);
-      context->dynamics_name = NULL;
-    }
-
-  if (context->mybrush_name)
-    {
-      g_free (context->mybrush_name);
-      context->mybrush_name = NULL;
-    }
-
-  if (context->pattern_name)
-    {
-      g_free (context->pattern_name);
-      context->pattern_name = NULL;
-    }
-
-  if (context->gradient_name)
-    {
-      g_free (context->gradient_name);
-      context->gradient_name = NULL;
-    }
-
-  if (context->palette_name)
-    {
-      g_free (context->palette_name);
-      context->palette_name = NULL;
-    }
-
-  if (context->tool_preset_name)
-    {
-      g_free (context->tool_preset_name);
-      context->tool_preset_name = NULL;
-    }
-
-  if (context->font_name)
-    {
-      g_free (context->font_name);
-      context->font_name = NULL;
-    }
-
-  if (context->buffer_name)
-    {
-      g_free (context->buffer_name);
-      context->buffer_name = NULL;
-    }
-
-  if (context->imagefile_name)
-    {
-      g_free (context->imagefile_name);
-      context->imagefile_name = NULL;
-    }
-
-  if (context->template_name)
-    {
-      g_free (context->template_name);
-      context->template_name = NULL;
-    }
+  g_clear_pointer (&context->tool_name,        g_free);
+  g_clear_pointer (&context->paint_name,       g_free);
+  g_clear_pointer (&context->brush_name,       g_free);
+  g_clear_pointer (&context->dynamics_name,    g_free);
+  g_clear_pointer (&context->mybrush_name,     g_free);
+  g_clear_pointer (&context->pattern_name,     g_free);
+  g_clear_pointer (&context->gradient_name,    g_free);
+  g_clear_pointer (&context->palette_name,     g_free);
+  g_clear_pointer (&context->tool_preset_name, g_free);
+  g_clear_pointer (&context->font_name,        g_free);
+  g_clear_pointer (&context->buffer_name,      g_free);
+  g_clear_pointer (&context->imagefile_name,   g_free);
+  g_clear_pointer (&context->template_name,    g_free);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/core/gimpdata.c b/app/core/gimpdata.c
index dbd29b6..b3fc5d3 100644
--- a/app/core/gimpdata.c
+++ b/app/core/gimpdata.c
@@ -254,11 +254,7 @@ gimp_data_finalize (GObject *object)
 {
   GimpDataPrivate *private = GIMP_DATA_GET_PRIVATE (object);
 
-  if (private->file)
-    {
-      g_object_unref (private->file);
-      private->file = NULL;
-    }
+  g_clear_object (&private->file);
 
   if (private->tags)
     {
@@ -266,11 +262,7 @@ gimp_data_finalize (GObject *object)
       private->tags = NULL;
     }
 
-  if (private->identifier)
-    {
-      g_free (private->identifier);
-      private->identifier = NULL;
-    }
+  g_clear_pointer (&private->identifier, g_free);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -1106,11 +1098,7 @@ gimp_data_duplicate (GimpData *data)
                     "deletable", TRUE,
                     NULL);
 
-      if (private->file)
-        {
-          g_object_unref (private->file);
-          private->file = NULL;
-        }
+      g_clear_object (&private->file);
 
       return new;
     }
@@ -1140,11 +1128,7 @@ gimp_data_make_internal (GimpData    *data,
 
   private = GIMP_DATA_GET_PRIVATE (data);
 
-  if (private->file)
-    {
-      g_object_unref (private->file);
-      private->file = NULL;
-    }
+  g_clear_object (&private->file);
 
   private->identifier = g_strdup (identifier);
   private->writable   = FALSE;
diff --git a/app/core/gimpdatafactory.c b/app/core/gimpdatafactory.c
index ad4091a..eb5036f 100644
--- a/app/core/gimpdatafactory.c
+++ b/app/core/gimpdatafactory.c
@@ -144,29 +144,11 @@ gimp_data_factory_finalize (GObject *object)
 {
   GimpDataFactory *factory = GIMP_DATA_FACTORY (object);
 
-  if (factory->priv->container)
-    {
-      g_object_unref (factory->priv->container);
-      factory->priv->container = NULL;
-    }
-
-  if (factory->priv->container_obsolete)
-    {
-      g_object_unref (factory->priv->container_obsolete);
-      factory->priv->container_obsolete = NULL;
-    }
+  g_clear_object (&factory->priv->container);
+  g_clear_object (&factory->priv->container_obsolete);
 
-  if (factory->priv->path_property_name)
-    {
-      g_free (factory->priv->path_property_name);
-      factory->priv->path_property_name = NULL;
-    }
-
-  if (factory->priv->writable_property_name)
-    {
-      g_free (factory->priv->writable_property_name);
-      factory->priv->writable_property_name = NULL;
-    }
+  g_clear_pointer (&factory->priv->path_property_name,     g_free);
+  g_clear_pointer (&factory->priv->writable_property_name, g_free);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -372,7 +354,7 @@ gimp_data_factory_data_load (GimpDataFactory *factory,
                                         list->data);
     }
 
-  g_list_free_full (path, (GDestroyNotify) g_object_unref);
+  g_list_free_full (path,          (GDestroyNotify) g_object_unref);
   g_list_free_full (writable_path, (GDestroyNotify) g_object_unref);
 }
 
@@ -789,7 +771,7 @@ gimp_data_factory_get_save_dir (GimpDataFactory  *factory,
                    _("You don't have any writable data folder configured."));
     }
 
-  g_list_free_full (path, (GDestroyNotify) g_object_unref);
+  g_list_free_full (path,          (GDestroyNotify) g_object_unref);
   g_list_free_full (writable_path, (GDestroyNotify) g_object_unref);
 
   return writable_dir;
diff --git a/app/core/gimpdrawable-floating-selection.c b/app/core/gimpdrawable-floating-selection.c
index f0e10b6..381fb67 100644
--- a/app/core/gimpdrawable-floating-selection.c
+++ b/app/core/gimpdrawable-floating-selection.c
@@ -254,11 +254,8 @@ gimp_drawable_remove_fs_filter (GimpDrawable *drawable)
                                 fs->layer_offset_node, "input");
         }
 
-      g_object_unref (private->fs_filter);
-      private->fs_filter = NULL;
-
-      g_object_unref (private->fs_applicator);
-      private->fs_applicator = NULL;
+      g_clear_object (&private->fs_filter);
+      g_clear_object (&private->fs_applicator);
 
       private->fs_crop_node = NULL;
     }
diff --git a/app/core/gimpdrawable-shadow.c b/app/core/gimpdrawable-shadow.c
index 4b64c08..25b8dc9 100644
--- a/app/core/gimpdrawable-shadow.c
+++ b/app/core/gimpdrawable-shadow.c
@@ -71,11 +71,7 @@ gimp_drawable_free_shadow_buffer (GimpDrawable *drawable)
 {
   g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
 
-  if (drawable->private->shadow)
-    {
-      g_object_unref (drawable->private->shadow);
-      drawable->private->shadow = NULL;
-    }
+  g_clear_object (&drawable->private->shadow);
 }
 
 void
diff --git a/app/core/gimpdrawable.c b/app/core/gimpdrawable.c
index 131db44..220529d 100644
--- a/app/core/gimpdrawable.c
+++ b/app/core/gimpdrawable.c
@@ -312,25 +312,12 @@ gimp_drawable_finalize (GObject *object)
 {
   GimpDrawable *drawable = GIMP_DRAWABLE (object);
 
-  if (drawable->private->buffer)
-    {
-      g_object_unref (drawable->private->buffer);
-      drawable->private->buffer = NULL;
-    }
+  g_clear_object (&drawable->private->buffer);
 
   gimp_drawable_free_shadow_buffer (drawable);
 
-  if (drawable->private->source_node)
-    {
-      g_object_unref (drawable->private->source_node);
-      drawable->private->source_node = NULL;
-    }
-
-  if (drawable->private->filter_stack)
-    {
-      g_object_unref (drawable->private->filter_stack);
-      drawable->private->filter_stack = NULL;
-    }
+  g_clear_object (&drawable->private->source_node);
+  g_clear_object (&drawable->private->filter_stack);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/core/gimpdrawablefilter.c b/app/core/gimpdrawablefilter.c
index ca62996..96a8813 100644
--- a/app/core/gimpdrawablefilter.c
+++ b/app/core/gimpdrawablefilter.c
@@ -173,23 +173,9 @@ gimp_drawable_filter_finalize (GObject *object)
 {
   GimpDrawableFilter *drawable_filter = GIMP_DRAWABLE_FILTER (object);
 
-  if (drawable_filter->operation)
-    {
-      g_object_unref (drawable_filter->operation);
-      drawable_filter->operation = NULL;
-    }
-
-  if (drawable_filter->applicator)
-    {
-      g_object_unref (drawable_filter->applicator);
-      drawable_filter->applicator = NULL;
-    }
-
-  if (drawable_filter->drawable)
-    {
-      g_object_unref (drawable_filter->drawable);
-      drawable_filter->drawable = NULL;
-    }
+  g_clear_object (&drawable_filter->operation);
+  g_clear_object (&drawable_filter->applicator);
+  g_clear_object (&drawable_filter->drawable);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/core/gimpdrawablemodundo.c b/app/core/gimpdrawablemodundo.c
index c0e8dcf..393e310 100644
--- a/app/core/gimpdrawablemodundo.c
+++ b/app/core/gimpdrawablemodundo.c
@@ -208,11 +208,7 @@ gimp_drawable_mod_undo_free (GimpUndo     *undo,
 {
   GimpDrawableModUndo *drawable_mod_undo = GIMP_DRAWABLE_MOD_UNDO (undo);
 
-  if (drawable_mod_undo->buffer)
-    {
-      g_object_unref (drawable_mod_undo->buffer);
-      drawable_mod_undo->buffer = NULL;
-    }
+  g_clear_object (&drawable_mod_undo->buffer);
 
   GIMP_UNDO_CLASS (parent_class)->free (undo, undo_mode);
 }
diff --git a/app/core/gimpdrawableundo.c b/app/core/gimpdrawableundo.c
index 1af5aeb..a2ac359 100644
--- a/app/core/gimpdrawableundo.c
+++ b/app/core/gimpdrawableundo.c
@@ -201,17 +201,8 @@ gimp_drawable_undo_free (GimpUndo     *undo,
 {
   GimpDrawableUndo *drawable_undo = GIMP_DRAWABLE_UNDO (undo);
 
-  if (drawable_undo->buffer)
-    {
-      g_object_unref (drawable_undo->buffer);
-      drawable_undo->buffer = NULL;
-    }
-
-  if (drawable_undo->applied_buffer)
-    {
-      g_object_unref (drawable_undo->applied_buffer);
-      drawable_undo->applied_buffer = NULL;
-    }
+  g_clear_object (&drawable_undo->buffer);
+  g_clear_object (&drawable_undo->applied_buffer);
 
   GIMP_UNDO_CLASS (parent_class)->free (undo, undo_mode);
 }
diff --git a/app/core/gimpdynamics.c b/app/core/gimpdynamics.c
index 64dcd4f..c858259 100644
--- a/app/core/gimpdynamics.c
+++ b/app/core/gimpdynamics.c
@@ -270,17 +270,17 @@ gimp_dynamics_finalize (GObject *object)
 {
   GimpDynamicsPrivate *private = GET_PRIVATE (object);
 
-  g_object_unref (private->opacity_output);
-  g_object_unref (private->force_output);
-  g_object_unref (private->hardness_output);
-  g_object_unref (private->rate_output);
-  g_object_unref (private->flow_output);
-  g_object_unref (private->size_output);
-  g_object_unref (private->aspect_ratio_output);
-  g_object_unref (private->color_output);
-  g_object_unref (private->angle_output);
-  g_object_unref (private->jitter_output);
-  g_object_unref (private->spacing_output);
+  g_clear_object (&private->opacity_output);
+  g_clear_object (&private->force_output);
+  g_clear_object (&private->hardness_output);
+  g_clear_object (&private->rate_output);
+  g_clear_object (&private->flow_output);
+  g_clear_object (&private->size_output);
+  g_clear_object (&private->aspect_ratio_output);
+  g_clear_object (&private->color_output);
+  g_clear_object (&private->angle_output);
+  g_clear_object (&private->jitter_output);
+  g_clear_object (&private->spacing_output);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/core/gimpdynamicsoutput.c b/app/core/gimpdynamicsoutput.c
index 51b9b7f..1389643 100644
--- a/app/core/gimpdynamicsoutput.c
+++ b/app/core/gimpdynamicsoutput.c
@@ -252,13 +252,13 @@ gimp_dynamics_output_finalize (GObject *object)
 {
   GimpDynamicsOutputPrivate *private = GET_PRIVATE (object);
 
-  g_object_unref (private->pressure_curve);
-  g_object_unref (private->velocity_curve);
-  g_object_unref (private->direction_curve);
-  g_object_unref (private->tilt_curve);
-  g_object_unref (private->wheel_curve);
-  g_object_unref (private->random_curve);
-  g_object_unref (private->fade_curve);
+  g_clear_object (&private->pressure_curve);
+  g_clear_object (&private->velocity_curve);
+  g_clear_object (&private->direction_curve);
+  g_clear_object (&private->tilt_curve);
+  g_clear_object (&private->wheel_curve);
+  g_clear_object (&private->random_curve);
+  g_clear_object (&private->fade_curve);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/core/gimpfilter.c b/app/core/gimpfilter.c
index c14b031..b2ad85f 100644
--- a/app/core/gimpfilter.c
+++ b/app/core/gimpfilter.c
@@ -138,11 +138,7 @@ gimp_filter_finalize (GObject *object)
 {
   GimpFilterPrivate *private = GET_PRIVATE (object);
 
-  if (private->node)
-    {
-      g_object_unref (private->node);
-      private->node = NULL;
-    }
+  g_clear_object (&private->node);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/core/gimpfilteredcontainer.c b/app/core/gimpfilteredcontainer.c
index 24ec1a7..78e5c71 100644
--- a/app/core/gimpfilteredcontainer.c
+++ b/app/core/gimpfilteredcontainer.c
@@ -168,11 +168,7 @@ gimp_filtered_container_finalize (GObject *object)
 {
   GimpFilteredContainer *filtered_container = GIMP_FILTERED_CONTAINER (object);
 
-  if (filtered_container->src_container)
-    {
-      g_object_unref (filtered_container->src_container);
-      filtered_container->src_container = NULL;
-    }
+  g_clear_object (&filtered_container->src_container);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/core/gimpfilterstack.c b/app/core/gimpfilterstack.c
index 6aacc26..a3cb8a1 100644
--- a/app/core/gimpfilterstack.c
+++ b/app/core/gimpfilterstack.c
@@ -96,11 +96,7 @@ gimp_filter_stack_finalize (GObject *object)
 {
   GimpFilterStack *stack = GIMP_FILTER_STACK (object);
 
-  if (stack->graph)
-    {
-      g_object_unref (stack->graph);
-      stack->graph = NULL;
-    }
+  g_clear_object (&stack->graph);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/core/gimpgrouplayer.c b/app/core/gimpgrouplayer.c
index 331e872..1c311ab 100644
--- a/app/core/gimpgrouplayer.c
+++ b/app/core/gimpgrouplayer.c
@@ -311,21 +311,11 @@ gimp_group_layer_finalize (GObject *object)
                                             gimp_group_layer_stack_update,
                                             object);
 
-      g_object_unref (private->children);
-      private->children = NULL;
+      g_clear_object (&private->children);
     }
 
-  if (private->projection)
-    {
-      g_object_unref (private->projection);
-      private->projection = NULL;
-    }
-
-  if (private->graph)
-    {
-      g_object_unref (private->graph);
-      private->graph = NULL;
-    }
+  g_clear_object (&private->projection);
+  g_clear_object (&private->graph);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/core/gimpguideundo.c b/app/core/gimpguideundo.c
index 37b83d3..8d2f077 100644
--- a/app/core/gimpguideundo.c
+++ b/app/core/gimpguideundo.c
@@ -181,11 +181,7 @@ gimp_guide_undo_free (GimpUndo     *undo,
 {
   GimpGuideUndo *guide_undo = GIMP_GUIDE_UNDO (undo);
 
-  if (guide_undo->guide)
-    {
-      g_object_unref (guide_undo->guide);
-      guide_undo->guide = NULL;
-    }
+  g_clear_object (&guide_undo->guide);
 
   GIMP_UNDO_CLASS (parent_class)->free (undo, undo_mode);
 }
diff --git a/app/core/gimpidtable.c b/app/core/gimpidtable.c
index 563a08c..4f19bb8 100644
--- a/app/core/gimpidtable.c
+++ b/app/core/gimpidtable.c
@@ -81,11 +81,7 @@ gimp_id_table_finalize (GObject *object)
 {
   GimpIdTable *id_table = GIMP_ID_TABLE (object);
 
-  if (id_table->priv->id_table)
-    {
-      g_hash_table_unref (id_table->priv->id_table);
-      id_table->priv->id_table = NULL;
-    }
+  g_clear_pointer (&id_table->priv->id_table, g_hash_table_unref);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/core/gimpimage-color-profile.c b/app/core/gimpimage-color-profile.c
index e502bd3..7115b9e 100644
--- a/app/core/gimpimage-color-profile.c
+++ b/app/core/gimpimage-color-profile.c
@@ -628,11 +628,7 @@ _gimp_image_free_color_profile (GimpImage *image)
 {
   GimpImagePrivate *private = GIMP_IMAGE_GET_PRIVATE (image);
 
-  if (private->color_profile)
-    {
-      g_object_unref (private->color_profile);
-      private->color_profile = NULL;
-    }
+  g_clear_object (&private->color_profile);
 
   _gimp_image_free_color_transforms (image);
 }
@@ -642,29 +638,10 @@ _gimp_image_free_color_transforms (GimpImage *image)
 {
   GimpImagePrivate *private = GIMP_IMAGE_GET_PRIVATE (image);
 
-  if (private->transform_to_srgb_u8)
-    {
-      g_object_unref (private->transform_to_srgb_u8);
-      private->transform_to_srgb_u8 = NULL;
-    }
-
-  if (private->transform_from_srgb_u8)
-    {
-      g_object_unref (private->transform_from_srgb_u8);
-      private->transform_from_srgb_u8 = NULL;
-    }
-
-  if (private->transform_to_srgb_double)
-    {
-      g_object_unref (private->transform_to_srgb_double);
-      private->transform_to_srgb_double = NULL;
-    }
-
-  if (private->transform_from_srgb_double)
-    {
-      g_object_unref (private->transform_from_srgb_double);
-      private->transform_from_srgb_double = NULL;
-    }
+  g_clear_object (&private->transform_to_srgb_u8);
+  g_clear_object (&private->transform_from_srgb_u8);
+  g_clear_object (&private->transform_to_srgb_double);
+  g_clear_object (&private->transform_from_srgb_double);
 
   private->color_transforms_created = FALSE;
 }
diff --git a/app/core/gimpimage-colormap.c b/app/core/gimpimage-colormap.c
index 23a6617..6b53d6c 100644
--- a/app/core/gimpimage-colormap.c
+++ b/app/core/gimpimage-colormap.c
@@ -126,11 +126,8 @@ gimp_image_colormap_free (GimpImage *image)
   g_return_if_fail (private->colormap != NULL);
   g_return_if_fail (GIMP_IS_PALETTE (private->palette));
 
-  g_free (private->colormap);
-  private->colormap = NULL;
-
-  g_object_unref (private->palette);
-  private->palette = NULL;
+  g_clear_pointer (&private->colormap, g_free);
+  g_clear_object (&private->palette);
 
   /* don't touch the image's babl_palettes because we might still have
    * buffers with that palette on the undo stack, and on undoing the
diff --git a/app/core/gimpimage.c b/app/core/gimpimage.c
index 58749c9..7a4ce51 100644
--- a/app/core/gimpimage.c
+++ b/app/core/gimpimage.c
@@ -1027,18 +1027,9 @@ gimp_image_finalize (GObject *object)
   GimpImage        *image   = GIMP_IMAGE (object);
   GimpImagePrivate *private = GIMP_IMAGE_GET_PRIVATE (image);
 
-  if (private->projection)
-    {
-      g_object_unref (private->projection);
-      private->projection = NULL;
-    }
-
-  if (private->graph)
-    {
-      g_object_unref (private->graph);
-      private->graph = NULL;
-      private->visible_mask = NULL;
-    }
+  g_clear_object (&private->projection);
+  g_clear_object (&private->graph);
+  private->visible_mask = NULL;
 
   if (private->colormap)
     gimp_image_colormap_free (image);
@@ -1046,74 +1037,24 @@ gimp_image_finalize (GObject *object)
   if (private->color_profile)
     _gimp_image_free_color_profile (image);
 
-  if (private->metadata)
-    {
-      g_object_unref (private->metadata);
-      private->metadata = NULL;
-    }
-
-  if (private->file)
-    {
-      g_object_unref (private->file);
-      private->file = NULL;
-    }
-
-  if (private->imported_file)
-    {
-      g_object_unref (private->imported_file);
-      private->imported_file = NULL;
-    }
+  g_clear_object (&private->metadata);
+  g_clear_object (&private->file);
+  g_clear_object (&private->imported_file);
+  g_clear_object (&private->exported_file);
+  g_clear_object (&private->save_a_copy_file);
+  g_clear_object (&private->untitled_file);
+  g_clear_object (&private->layers);
+  g_clear_object (&private->channels);
+  g_clear_object (&private->vectors);
 
-  if (private->exported_file)
-    {
-      g_object_unref (private->exported_file);
-      private->exported_file = NULL;
-    }
-
-  if (private->save_a_copy_file)
-    {
-      g_object_unref (private->save_a_copy_file);
-      private->save_a_copy_file = NULL;
-    }
-
-  if (private->untitled_file)
-    {
-      g_object_unref (private->untitled_file);
-      private->untitled_file = NULL;
-    }
-
-  if (private->layers)
-    {
-      g_object_unref (private->layers);
-      private->layers = NULL;
-    }
-  if (private->channels)
-    {
-      g_object_unref (private->channels);
-      private->channels = NULL;
-    }
-  if (private->vectors)
-    {
-      g_object_unref (private->vectors);
-      private->vectors = NULL;
-    }
   if (private->layer_stack)
     {
       g_slist_free (private->layer_stack);
       private->layer_stack = NULL;
     }
 
-  if (private->selection_mask)
-    {
-      g_object_unref (private->selection_mask);
-      private->selection_mask = NULL;
-    }
-
-  if (private->parasites)
-    {
-      g_object_unref (private->parasites);
-      private->parasites = NULL;
-    }
+  g_clear_object (&private->selection_mask);
+  g_clear_object (&private->parasites);
 
   if (private->guides)
     {
@@ -1127,11 +1068,7 @@ gimp_image_finalize (GObject *object)
       private->symmetries = NULL;
     }
 
-  if (private->grid)
-    {
-      g_object_unref (private->grid);
-      private->grid = NULL;
-    }
+  g_clear_object (&private->grid);
 
   if (private->sample_points)
     {
@@ -1140,16 +1077,8 @@ gimp_image_finalize (GObject *object)
       private->sample_points = NULL;
     }
 
-  if (private->undo_stack)
-    {
-      g_object_unref (private->undo_stack);
-      private->undo_stack = NULL;
-    }
-  if (private->redo_stack)
-    {
-      g_object_unref (private->redo_stack);
-      private->redo_stack = NULL;
-    }
+  g_clear_object (&private->undo_stack);
+  g_clear_object (&private->redo_stack);
 
   if (image->gimp && image->gimp->image_table)
     {
@@ -1157,17 +1086,8 @@ gimp_image_finalize (GObject *object)
       image->gimp = NULL;
     }
 
-  if (private->display_name)
-    {
-      g_free (private->display_name);
-      private->display_name = NULL;
-    }
-
-  if (private->display_path)
-    {
-      g_free (private->display_path);
-      private->display_path = NULL;
-    }
+  g_clear_pointer (&private->display_name, g_free);
+  g_clear_pointer (&private->display_path, g_free);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -1182,17 +1102,8 @@ gimp_image_name_changed (GimpObject *object)
   if (GIMP_OBJECT_CLASS (parent_class)->name_changed)
     GIMP_OBJECT_CLASS (parent_class)->name_changed (object);
 
-  if (private->display_name)
-    {
-      g_free (private->display_name);
-      private->display_name = NULL;
-    }
-
-  if (private->display_path)
-    {
-      g_free (private->display_path);
-      private->display_path = NULL;
-    }
+  g_clear_pointer (&private->display_name, g_free);
+  g_clear_pointer (&private->display_path, g_free);
 
   /* We never want the empty string as a name, so change empty strings
    * to NULL strings (without emitting the "name-changed" signal
@@ -1205,11 +1116,7 @@ gimp_image_name_changed (GimpObject *object)
       name = NULL;
     }
 
-  if (private->file)
-    {
-      g_object_unref (private->file);
-      private->file = NULL;
-    }
+  g_clear_object (&private->file);
 
   if (name)
     private->file = g_file_new_for_uri (name);
diff --git a/app/core/gimpimagefile.c b/app/core/gimpimagefile.c
index 022b1b7..1fe456e 100644
--- a/app/core/gimpimagefile.c
+++ b/app/core/gimpimagefile.c
@@ -183,8 +183,7 @@ gimp_imagefile_dispose (GObject *object)
   if (private->icon_cancellable)
     {
       g_cancellable_cancel (private->icon_cancellable);
-      g_object_unref (private->icon_cancellable);
-      private->icon_cancellable = NULL;
+      g_clear_object (&private->icon_cancellable);
     }
 
   G_OBJECT_CLASS (parent_class)->dispose (object);
@@ -203,23 +202,9 @@ gimp_imagefile_finalize (GObject *object)
       private->description = NULL;
     }
 
-  if (private->thumbnail)
-    {
-      g_object_unref (private->thumbnail);
-      private->thumbnail = NULL;
-    }
-
-  if (private->icon)
-    {
-      g_object_unref (private->icon);
-      private->icon = NULL;
-    }
-
-  if (private->file)
-    {
-      g_object_unref (private->file);
-      private->file = NULL;
-    }
+  g_clear_object (&private->thumbnail);
+  g_clear_object (&private->icon);
+  g_clear_object (&private->file);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -234,11 +219,7 @@ gimp_imagefile_name_changed (GimpObject *object)
 
   gimp_thumbnail_set_uri (private->thumbnail, gimp_object_get_name (object));
 
-  if (private->file)
-    {
-      g_object_unref (private->file);
-      private->file = NULL;
-    }
+  g_clear_object (&private->file);
 
   if (gimp_object_get_name (object))
     private->file = g_file_new_for_uri (gimp_object_get_name (object));
@@ -680,11 +661,8 @@ gimp_imagefile_info_changed (GimpImagefile *imagefile)
 
       private->description = NULL;
     }
-  if (private->icon)
-    {
-      g_object_unref (GET_PRIVATE (imagefile)->icon);
-      private->icon = NULL;
-    }
+
+  g_clear_object (&private->icon);
 
   g_signal_emit (imagefile, gimp_imagefile_signals[INFO_CHANGED], 0);
 }
@@ -740,11 +718,7 @@ gimp_imagefile_icon_callback (GObject      *source_object,
       g_object_unref (file_info);
     }
 
-  if (private->icon_cancellable)
-    {
-      g_object_unref (private->icon_cancellable);
-      private->icon_cancellable = NULL;
-    }
+  g_clear_object (&private->icon_cancellable);
 
   if (private->icon)
     gimp_viewable_invalidate_preview (GIMP_VIEWABLE (imagefile));
diff --git a/app/core/gimpimageundo.c b/app/core/gimpimageundo.c
index b433e3d..b46c3e9 100644
--- a/app/core/gimpimageundo.c
+++ b/app/core/gimpimageundo.c
@@ -533,35 +533,11 @@ gimp_image_undo_free (GimpUndo     *undo,
 {
   GimpImageUndo *image_undo = GIMP_IMAGE_UNDO (undo);
 
-  if (image_undo->grid)
-    {
-      g_object_unref (image_undo->grid);
-      image_undo->grid = NULL;
-    }
-
-  if (image_undo->colormap)
-    {
-      g_free (image_undo->colormap);
-      image_undo->colormap = NULL;
-    }
-
-  if (image_undo->metadata)
-    {
-      g_object_unref (image_undo->metadata);
-      image_undo->metadata = NULL;
-    }
-
-  if (image_undo->parasite_name)
-    {
-      g_free (image_undo->parasite_name);
-      image_undo->parasite_name = NULL;
-    }
-
-  if (image_undo->parasite)
-    {
-      gimp_parasite_free (image_undo->parasite);
-      image_undo->parasite = NULL;
-    }
+  g_clear_object (&image_undo->grid);
+  g_clear_pointer (&image_undo->colormap, g_free);
+  g_clear_object (&image_undo->metadata);
+  g_clear_pointer (&image_undo->parasite_name, g_free);
+  g_clear_pointer (&image_undo->parasite, gimp_parasite_free);
 
   GIMP_UNDO_CLASS (parent_class)->free (undo, undo_mode);
 }
diff --git a/app/core/gimpitem.c b/app/core/gimpitem.c
index ab36f52..a400ea2 100644
--- a/app/core/gimpitem.c
+++ b/app/core/gimpitem.c
@@ -362,11 +362,7 @@ gimp_item_finalize (GObject *object)
       private->image = NULL;
     }
 
-  if (private->parasites)
-    {
-      g_object_unref (private->parasites);
-      private->parasites = NULL;
-    }
+  g_clear_object (&private->parasites);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/core/gimpitempropundo.c b/app/core/gimpitempropundo.c
index 023e8ee..f3a10ae 100644
--- a/app/core/gimpitempropundo.c
+++ b/app/core/gimpitempropundo.c
@@ -350,23 +350,9 @@ gimp_item_prop_undo_free (GimpUndo     *undo,
 {
   GimpItemPropUndo *item_prop_undo = GIMP_ITEM_PROP_UNDO (undo);
 
-  if (item_prop_undo->name)
-    {
-      g_free (item_prop_undo->name);
-      item_prop_undo->name = NULL;
-    }
-
-  if (item_prop_undo->parasite_name)
-    {
-      g_free (item_prop_undo->parasite_name);
-      item_prop_undo->parasite_name = NULL;
-    }
-
-  if (item_prop_undo->parasite)
-    {
-      gimp_parasite_free (item_prop_undo->parasite);
-      item_prop_undo->parasite = NULL;
-    }
+  g_clear_pointer (&item_prop_undo->name,          g_free);
+  g_clear_pointer (&item_prop_undo->parasite_name, g_free);
+  g_clear_pointer (&item_prop_undo->parasite,      gimp_parasite_free);
 
   GIMP_UNDO_CLASS (parent_class)->free (undo, undo_mode);
 }
diff --git a/app/core/gimpitemtree.c b/app/core/gimpitemtree.c
index 540e048..187168b 100644
--- a/app/core/gimpitemtree.c
+++ b/app/core/gimpitemtree.c
@@ -168,17 +168,8 @@ gimp_item_tree_finalize (GObject *object)
   GimpItemTree        *tree    = GIMP_ITEM_TREE (object);
   GimpItemTreePrivate *private = GIMP_ITEM_TREE_GET_PRIVATE (tree);
 
-  if (private->name_hash)
-    {
-      g_hash_table_unref (private->name_hash);
-      private->name_hash = NULL;
-    }
-
-  if (tree->container)
-    {
-      g_object_unref (tree->container);
-      tree->container = NULL;
-    }
+  g_clear_pointer (&private->name_hash, g_hash_table_unref);
+  g_clear_object (&tree->container);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/core/gimpitemundo.c b/app/core/gimpitemundo.c
index 34c9539..6e1b155 100644
--- a/app/core/gimpitemundo.c
+++ b/app/core/gimpitemundo.c
@@ -133,11 +133,7 @@ gimp_item_undo_free (GimpUndo     *undo,
 {
   GimpItemUndo *item_undo = GIMP_ITEM_UNDO (undo);
 
-  if (item_undo->item)
-    {
-      g_object_unref (item_undo->item);
-      item_undo->item = NULL;
-    }
+  g_clear_object (&item_undo->item);
 
   GIMP_UNDO_CLASS (parent_class)->free (undo, undo_mode);
 }
diff --git a/app/core/gimplayer.c b/app/core/gimplayer.c
index b8d61b4..c4c9d74 100644
--- a/app/core/gimplayer.c
+++ b/app/core/gimplayer.c
@@ -643,11 +643,7 @@ gimp_layer_finalize (GObject *object)
 {
   GimpLayer *layer = GIMP_LAYER (object);
 
-  if (layer->mask)
-    {
-      g_object_unref (layer->mask);
-      layer->mask = NULL;
-    }
+  g_clear_object (&layer->mask);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/core/gimplayermaskundo.c b/app/core/gimplayermaskundo.c
index 1892f4c..93b3bf5 100644
--- a/app/core/gimplayermaskundo.c
+++ b/app/core/gimplayermaskundo.c
@@ -189,11 +189,7 @@ gimp_layer_mask_undo_free (GimpUndo     *undo,
 {
   GimpLayerMaskUndo *layer_mask_undo = GIMP_LAYER_MASK_UNDO (undo);
 
-  if (layer_mask_undo->layer_mask)
-    {
-      g_object_unref (layer_mask_undo->layer_mask);
-      layer_mask_undo->layer_mask = NULL;
-    }
+  g_clear_object (&layer_mask_undo->layer_mask);
 
   GIMP_UNDO_CLASS (parent_class)->free (undo, undo_mode);
 }
diff --git a/app/core/gimpmaskundo.c b/app/core/gimpmaskundo.c
index 558d2cb..d1477bd 100644
--- a/app/core/gimpmaskundo.c
+++ b/app/core/gimpmaskundo.c
@@ -280,11 +280,7 @@ gimp_mask_undo_free (GimpUndo     *undo,
 {
   GimpMaskUndo *mask_undo = GIMP_MASK_UNDO (undo);
 
-  if (mask_undo->buffer)
-    {
-      g_object_unref (mask_undo->buffer);
-      mask_undo->buffer = NULL;
-    }
+  g_clear_object (&mask_undo->buffer);
 
   GIMP_UNDO_CLASS (parent_class)->free (undo, undo_mode);
 }
diff --git a/app/core/gimppaintinfo.c b/app/core/gimppaintinfo.c
index c4743e6..313cc56 100644
--- a/app/core/gimppaintinfo.c
+++ b/app/core/gimppaintinfo.c
@@ -68,8 +68,7 @@ gimp_paint_info_dispose (GObject *object)
   if (paint_info->paint_options)
     {
       g_object_run_dispose (G_OBJECT (paint_info->paint_options));
-      g_object_unref (paint_info->paint_options);
-      paint_info->paint_options = NULL;
+      g_clear_object (&paint_info->paint_options);
     }
 
   G_OBJECT_CLASS (parent_class)->dispose (object);
@@ -80,11 +79,7 @@ gimp_paint_info_finalize (GObject *object)
 {
   GimpPaintInfo *paint_info = GIMP_PAINT_INFO (object);
 
-  if (paint_info->blurb)
-    {
-      g_free (paint_info->blurb);
-      paint_info->blurb = NULL;
-    }
+  g_clear_pointer (&paint_info->blurb, g_free);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/core/gimppattern.c b/app/core/gimppattern.c
index 98a1a73..c51baf6 100644
--- a/app/core/gimppattern.c
+++ b/app/core/gimppattern.c
@@ -101,11 +101,7 @@ gimp_pattern_finalize (GObject *object)
 {
   GimpPattern *pattern = GIMP_PATTERN (object);
 
-  if (pattern->mask)
-    {
-      gimp_temp_buf_unref (pattern->mask);
-      pattern->mask = NULL;
-    }
+  g_clear_pointer (&pattern->mask, gimp_temp_buf_unref);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/core/gimppatternclipboard.c b/app/core/gimppatternclipboard.c
index dbdac0f..2aca138 100644
--- a/app/core/gimppatternclipboard.c
+++ b/app/core/gimppatternclipboard.c
@@ -181,11 +181,7 @@ gimp_pattern_clipboard_changed (Gimp        *gimp,
   GimpObject *paste;
   GeglBuffer *buffer = NULL;
 
-  if (pattern->mask)
-    {
-      gimp_temp_buf_unref (pattern->mask);
-      pattern->mask = NULL;
-    }
+  g_clear_pointer (&pattern->mask, gimp_temp_buf_unref);
 
   paste = gimp_get_clipboard_object (gimp);
 
diff --git a/app/core/gimppdbprogress.c b/app/core/gimppdbprogress.c
index b8d9468..2b64251 100644
--- a/app/core/gimppdbprogress.c
+++ b/app/core/gimppdbprogress.c
@@ -196,23 +196,9 @@ gimp_pdb_progress_finalize (GObject *object)
 {
   GimpPdbProgress *progress = GIMP_PDB_PROGRESS (object);
 
-  if (progress->pdb)
-    {
-      g_object_unref (progress->pdb);
-      progress->pdb = NULL;
-    }
-
-  if (progress->context)
-    {
-      g_object_unref (progress->context);
-      progress->context = NULL;
-    }
-
-  if (progress->callback_name)
-    {
-      g_free (progress->callback_name);
-      progress->callback_name = NULL;
-    }
+  g_clear_object (&progress->pdb);
+  g_clear_object (&progress->context);
+  g_clear_pointer (&progress->callback_name, g_free);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/core/gimpprojection.c b/app/core/gimpprojection.c
index 58bb61b..8f119b2 100644
--- a/app/core/gimpprojection.c
+++ b/app/core/gimpprojection.c
@@ -564,8 +564,7 @@ gimp_projection_stop_rendering (GimpProjection *proj)
             cairo_region_copy (chunk_render->update_region);
         }
 
-      cairo_region_destroy (chunk_render->update_region);
-      chunk_render->update_region = NULL;
+      g_clear_pointer (&chunk_render->update_region, cairo_region_destroy);
     }
 
   rect.x      = chunk_render->x;
@@ -622,17 +621,8 @@ gimp_projection_free_buffer (GimpProjection  *proj)
   if (proj->priv->chunk_render.idle_id)
     gimp_projection_chunk_render_stop (proj);
 
-  if (proj->priv->update_region)
-    {
-      cairo_region_destroy (proj->priv->update_region);
-      proj->priv->update_region = NULL;
-    }
-
-  if (proj->priv->chunk_render.update_region)
-    {
-      cairo_region_destroy (proj->priv->chunk_render.update_region);
-      proj->priv->chunk_render.update_region = NULL;
-    }
+  g_clear_pointer (&proj->priv->update_region, cairo_region_destroy);
+  g_clear_pointer (&proj->priv->chunk_render.update_region, cairo_region_destroy);
 
   if (proj->priv->buffer)
     {
@@ -640,15 +630,10 @@ gimp_projection_free_buffer (GimpProjection  *proj)
         gegl_buffer_remove_handler (proj->priv->buffer,
                                     proj->priv->validate_handler);
 
-      g_object_unref (proj->priv->buffer);
-      proj->priv->buffer = NULL;
+      g_clear_object (&proj->priv->buffer);
     }
 
-  if (proj->priv->validate_handler)
-    {
-      g_object_unref (proj->priv->validate_handler);
-      proj->priv->validate_handler = NULL;
-    }
+  g_clear_object (&proj->priv->validate_handler);
 }
 
 static void
@@ -715,8 +700,7 @@ gimp_projection_flush_whenever (GimpProjection *proj,
         }
 
       /*  Free the update region  */
-      cairo_region_destroy (proj->priv->update_region);
-      proj->priv->update_region = NULL;
+      g_clear_pointer (&proj->priv->update_region, cairo_region_destroy);
     }
   else if (! now && proj->priv->invalidate_preview)
     {
@@ -906,8 +890,7 @@ gimp_projection_chunk_render_next_area (GimpProjection *proj)
 
   if (cairo_region_is_empty (chunk_render->update_region))
     {
-      cairo_region_destroy (chunk_render->update_region);
-      chunk_render->update_region = NULL;
+      g_clear_pointer (&chunk_render->update_region, cairo_region_destroy);
 
       return FALSE;
     }
@@ -926,8 +909,7 @@ gimp_projection_chunk_render_next_area (GimpProjection *proj)
 
   if (cairo_region_is_empty (chunk_render->update_region))
     {
-      cairo_region_destroy (chunk_render->update_region);
-      chunk_render->update_region = NULL;
+      g_clear_pointer (&chunk_render->update_region, cairo_region_destroy);
     }
 
   chunk_render->x      = rect.x;
diff --git a/app/core/gimpsamplepointundo.c b/app/core/gimpsamplepointundo.c
index d12f7ed..4032a47 100644
--- a/app/core/gimpsamplepointundo.c
+++ b/app/core/gimpsamplepointundo.c
@@ -181,11 +181,7 @@ gimp_sample_point_undo_free (GimpUndo     *undo,
 {
   GimpSamplePointUndo *sample_point_undo = GIMP_SAMPLE_POINT_UNDO (undo);
 
-  if (sample_point_undo->sample_point)
-    {
-      gimp_sample_point_unref (sample_point_undo->sample_point);
-      sample_point_undo->sample_point = NULL;
-    }
+  g_clear_pointer (&sample_point_undo->sample_point, gimp_sample_point_unref);
 
   GIMP_UNDO_CLASS (parent_class)->free (undo, undo_mode);
 }
diff --git a/app/core/gimpstrokeoptions.c b/app/core/gimpstrokeoptions.c
index 84debc4..bac3d2a 100644
--- a/app/core/gimpstrokeoptions.c
+++ b/app/core/gimpstrokeoptions.c
@@ -259,11 +259,7 @@ gimp_stroke_options_finalize (GObject *object)
       private->dash_info = NULL;
     }
 
-  if (private->paint_options)
-    {
-      g_object_unref (private->paint_options);
-      private->paint_options = NULL;
-    }
+  g_clear_object (&private->paint_options);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/core/gimpsubprogress.c b/app/core/gimpsubprogress.c
index bcad694..6256d83 100644
--- a/app/core/gimpsubprogress.c
+++ b/app/core/gimpsubprogress.c
@@ -76,11 +76,7 @@ gimp_sub_progress_finalize (GObject *object)
 {
   GimpSubProgress *sub = GIMP_SUB_PROGRESS (object);
 
-  if (sub->progress)
-    {
-      g_object_unref (sub->progress);
-      sub->progress = NULL;
-    }
+  g_clear_object (&sub->progress);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/core/gimpsymmetry-mandala.c b/app/core/gimpsymmetry-mandala.c
index 7c812f6..6c5c989 100644
--- a/app/core/gimpsymmetry-mandala.c
+++ b/app/core/gimpsymmetry-mandala.c
@@ -176,13 +176,8 @@ gimp_mandala_finalize (GObject *object)
 {
   GimpMandala  *mandala = GIMP_MANDALA (object);
 
-  if (mandala->horizontal_guide)
-    g_object_unref (mandala->horizontal_guide);
-  mandala->horizontal_guide = NULL;
-
-  if (mandala->vertical_guide)
-    g_object_unref (mandala->vertical_guide);
-  mandala->vertical_guide = NULL;
+  g_clear_object (&mandala->horizontal_guide);
+  g_clear_object (&mandala->vertical_guide);
 
   if (mandala->ops)
     {
@@ -523,6 +518,7 @@ gimp_mandala_get_operation (GimpSymmetry *sym,
                   if (iter->data)
                     g_object_unref (G_OBJECT (iter->data));
                 }
+
               g_list_free (mandala->ops);
               mandala->ops = NULL;
             }
diff --git a/app/core/gimpsymmetry-mirror.c b/app/core/gimpsymmetry-mirror.c
index 1e7c8a6..0c6010e 100644
--- a/app/core/gimpsymmetry-mirror.c
+++ b/app/core/gimpsymmetry-mirror.c
@@ -202,25 +202,12 @@ gimp_mirror_finalize (GObject *object)
 {
   GimpMirror *mirror = GIMP_MIRROR (object);
 
-  if (mirror->horizontal_guide)
-    g_object_unref (mirror->horizontal_guide);
-  mirror->horizontal_guide = NULL;
+  g_clear_object (&mirror->horizontal_guide);
+  g_clear_object (&mirror->vertical_guide);
 
-  if (mirror->vertical_guide)
-    g_object_unref (mirror->vertical_guide);
-  mirror->vertical_guide = NULL;
-
-  if (mirror->horizontal_op)
-    g_object_unref (mirror->horizontal_op);
-  mirror->horizontal_op = NULL;
-
-  if (mirror->vertical_op)
-    g_object_unref (mirror->vertical_op);
-  mirror->vertical_op = NULL;
-
-  if (mirror->central_op)
-    g_object_unref (mirror->central_op);
-  mirror->central_op = NULL;
+  g_clear_object (&mirror->horizontal_op);
+  g_clear_object (&mirror->vertical_op);
+  g_clear_object (&mirror->central_op);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/core/gimpsymmetry.c b/app/core/gimpsymmetry.c
index b92019b..d78aeaf 100644
--- a/app/core/gimpsymmetry.c
+++ b/app/core/gimpsymmetry.c
@@ -158,11 +158,9 @@ gimp_symmetry_finalize (GObject *object)
 {
   GimpSymmetry *sym = GIMP_SYMMETRY (object);
 
-  if (sym->drawable)
-    g_object_unref (sym->drawable);
+  g_clear_object (&sym->drawable);
 
-  g_free (sym->origin);
-  sym->origin = NULL;
+  g_clear_pointer (&sym->origin, g_free);
 
   g_list_free_full (sym->strokes, g_free);
   sym->strokes = NULL;
diff --git a/app/core/gimptaggedcontainer.c b/app/core/gimptaggedcontainer.c
index 56f6d1a..5ee62a0 100644
--- a/app/core/gimptaggedcontainer.c
+++ b/app/core/gimptaggedcontainer.c
@@ -130,11 +130,7 @@ gimp_tagged_container_dispose (GObject *object)
       tagged_container->filter = NULL;
     }
 
-  if (tagged_container->tag_ref_counts)
-    {
-      g_hash_table_unref (tagged_container->tag_ref_counts);
-      tagged_container->tag_ref_counts = NULL;
-    }
+  g_clear_pointer (&tagged_container->tag_ref_counts, g_hash_table_unref);
 
   G_OBJECT_CLASS (parent_class)->dispose (object);
 }
diff --git a/app/core/gimptemplate.c b/app/core/gimptemplate.c
index f6957eb..44495f0 100644
--- a/app/core/gimptemplate.c
+++ b/app/core/gimptemplate.c
@@ -259,23 +259,9 @@ gimp_template_finalize (GObject *object)
 {
   GimpTemplatePrivate *private = GET_PRIVATE (object);
 
-  if (private->color_profile)
-    {
-      g_object_unref (private->color_profile);
-      private->color_profile = NULL;
-    }
-
-  if (private->comment)
-    {
-      g_free (private->comment);
-      private->comment = NULL;
-    }
-
-  if (private->filename)
-    {
-      g_free (private->filename);
-      private->filename = NULL;
-    }
+  g_clear_object (&private->color_profile);
+  g_clear_pointer (&private->comment,  g_free);
+  g_clear_pointer (&private->filename, g_free);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/core/gimptoolinfo.c b/app/core/gimptoolinfo.c
index 5edb83a..aff4ee9 100644
--- a/app/core/gimptoolinfo.c
+++ b/app/core/gimptoolinfo.c
@@ -98,15 +98,10 @@ gimp_tool_info_dispose (GObject *object)
   if (tool_info->tool_options)
     {
       g_object_run_dispose (G_OBJECT (tool_info->tool_options));
-      g_object_unref (tool_info->tool_options);
-      tool_info->tool_options = NULL;
+      g_clear_object (&tool_info->tool_options);
     }
 
-  if (tool_info->presets)
-    {
-      g_object_unref (tool_info->presets);
-      tool_info->presets = NULL;
-    }
+  g_clear_object (&tool_info->presets);
 
   G_OBJECT_CLASS (parent_class)->dispose (object);
 }
@@ -116,38 +111,12 @@ gimp_tool_info_finalize (GObject *object)
 {
   GimpToolInfo *tool_info = GIMP_TOOL_INFO (object);
 
-  if (tool_info->label)
-    {
-      g_free (tool_info->label);
-      tool_info->label = NULL;
-    }
-  if (tool_info->tooltip)
-    {
-      g_free (tool_info->tooltip);
-      tool_info->tooltip = NULL;
-    }
-
-  if (tool_info->menu_label)
-    {
-      g_free (tool_info->menu_label);
-      tool_info->menu_label = NULL;
-    }
-  if (tool_info->menu_accel)
-    {
-      g_free (tool_info->menu_accel);
-      tool_info->menu_accel = NULL;
-    }
-
-  if (tool_info->help_domain)
-    {
-      g_free (tool_info->help_domain);
-      tool_info->help_domain = NULL;
-    }
-  if (tool_info->help_id)
-    {
-      g_free (tool_info->help_id);
-      tool_info->help_id = NULL;
-    }
+  g_clear_pointer (&tool_info->label,       g_free);
+  g_clear_pointer (&tool_info->tooltip,     g_free);
+  g_clear_pointer (&tool_info->menu_label,  g_free);
+  g_clear_pointer (&tool_info->menu_accel,  g_free);
+  g_clear_pointer (&tool_info->help_domain, g_free);
+  g_clear_pointer (&tool_info->help_id,     g_free);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/core/gimptooloptions.c b/app/core/gimptooloptions.c
index b4b4be8..17bf430 100644
--- a/app/core/gimptooloptions.c
+++ b/app/core/gimptooloptions.c
@@ -106,11 +106,7 @@ gimp_tool_options_dispose (GObject *object)
 {
   GimpToolOptions *options = GIMP_TOOL_OPTIONS (object);
 
-  if (options->tool_info)
-    {
-      g_object_unref (options->tool_info);
-      options->tool_info = NULL;
-    }
+  g_clear_object (&options->tool_info);
 
   G_OBJECT_CLASS (parent_class)->dispose (object);
 }
diff --git a/app/core/gimptoolpreset.c b/app/core/gimptoolpreset.c
index 42d02c0..81eebc7 100644
--- a/app/core/gimptoolpreset.c
+++ b/app/core/gimptoolpreset.c
@@ -495,12 +495,11 @@ gimp_tool_preset_set_options (GimpToolPreset  *preset,
                                             gimp_tool_preset_options_notify,
                                             preset);
 
-       g_signal_handlers_disconnect_by_func (preset->tool_options,
-                                             gimp_tool_preset_options_prop_name_changed,
-                                             preset);
+      g_signal_handlers_disconnect_by_func (preset->tool_options,
+                                            gimp_tool_preset_options_prop_name_changed,
+                                            preset);
 
-      g_object_unref (preset->tool_options);
-      preset->tool_options = NULL;
+      g_clear_object (&preset->tool_options);
     }
 
   if (options)
diff --git a/app/core/gimptreehandler.c b/app/core/gimptreehandler.c
index f8020a6..59b9566 100644
--- a/app/core/gimptreehandler.c
+++ b/app/core/gimptreehandler.c
@@ -89,11 +89,8 @@ gimp_tree_handler_dispose (GObject *object)
       if (! gimp_container_frozen (handler->container))
         gimp_tree_handler_remove_container (handler, handler->container);
 
-      g_object_unref (handler->container);
-      handler->container = NULL;
-
-      g_free (handler->signal_name);
-      handler->signal_name = NULL;
+      g_clear_object (&handler->container);
+      g_clear_pointer (&handler->signal_name, g_free);
     }
 
   G_OBJECT_CLASS (parent_class)->dispose (object);
diff --git a/app/core/gimpundo.c b/app/core/gimpundo.c
index ef14b5d..875899a 100644
--- a/app/core/gimpundo.c
+++ b/app/core/gimpundo.c
@@ -198,11 +198,7 @@ gimp_undo_finalize (GObject *object)
       undo->preview_idle_id = 0;
     }
 
-  if (undo->preview)
-    {
-      gimp_temp_buf_unref (undo->preview);
-      undo->preview = NULL;
-    }
+  g_clear_pointer (&undo->preview, gimp_temp_buf_unref);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -513,8 +509,7 @@ gimp_undo_refresh_preview (GimpUndo    *undo,
 
   if (undo->preview)
     {
-      gimp_temp_buf_unref (undo->preview);
-      undo->preview = NULL;
+      g_clear_pointer (&undo->preview, gimp_temp_buf_unref);
       gimp_undo_create_preview (undo, context, FALSE);
     }
 }
diff --git a/app/core/gimpundostack.c b/app/core/gimpundostack.c
index 4aff7a9..9f11166 100644
--- a/app/core/gimpundostack.c
+++ b/app/core/gimpundostack.c
@@ -71,11 +71,7 @@ gimp_undo_stack_finalize (GObject *object)
 {
   GimpUndoStack *stack = GIMP_UNDO_STACK (object);
 
-  if (stack->undos)
-    {
-      g_object_unref (stack->undos);
-      stack->undos = NULL;
-    }
+  g_clear_object (&stack->undos);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/core/gimpviewable.c b/app/core/gimpviewable.c
index b6d7ee2..d3be5d5 100644
--- a/app/core/gimpviewable.c
+++ b/app/core/gimpviewable.c
@@ -230,29 +230,10 @@ gimp_viewable_finalize (GObject *object)
 {
   GimpViewablePrivate *private = GET_PRIVATE (object);
 
-  if (private->icon_name)
-    {
-      g_free (private->icon_name);
-      private->icon_name = NULL;
-    }
-
-  if (private->icon_pixbuf)
-    {
-      g_object_unref (private->icon_pixbuf);
-      private->icon_pixbuf = NULL;
-    }
-
-  if (private->preview_temp_buf)
-    {
-      gimp_temp_buf_unref (private->preview_temp_buf);
-      private->preview_temp_buf = NULL;
-    }
-
-  if (private->preview_pixbuf)
-    {
-      g_object_unref (private->preview_pixbuf);
-      private->preview_pixbuf = NULL;
-    }
+  g_clear_pointer (&private->icon_name, g_free);
+  g_clear_object (&private->icon_pixbuf);
+  g_clear_pointer (&private->preview_temp_buf, gimp_temp_buf_unref);
+  g_clear_object (&private->preview_pixbuf);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -341,17 +322,8 @@ gimp_viewable_real_invalidate_preview (GimpViewable *viewable)
 {
   GimpViewablePrivate *private = GET_PRIVATE (viewable);
 
-  if (private->preview_temp_buf)
-    {
-      gimp_temp_buf_unref (private->preview_temp_buf);
-      private->preview_temp_buf = NULL;
-    }
-
-  if (private->preview_pixbuf)
-    {
-      g_object_unref (private->preview_pixbuf);
-      private->preview_pixbuf = NULL;
-    }
+  g_clear_pointer (&private->preview_temp_buf, gimp_temp_buf_unref);
+  g_clear_object (&private->preview_pixbuf);
 }
 
 static void
@@ -848,8 +820,7 @@ gimp_viewable_get_preview (GimpViewable *viewable,
           return private->preview_temp_buf;
         }
 
-      gimp_temp_buf_unref (private->preview_temp_buf);
-      private->preview_temp_buf = NULL;
+      g_clear_pointer (&private->preview_temp_buf, gimp_temp_buf_unref);
     }
 
   if (viewable_class->get_new_preview)
@@ -1006,8 +977,7 @@ gimp_viewable_get_pixbuf (GimpViewable *viewable,
           return private->preview_pixbuf;
         }
 
-      g_object_unref (private->preview_pixbuf);
-      private->preview_pixbuf = NULL;
+      g_clear_object (&private->preview_pixbuf);
     }
 
   if (viewable_class->get_new_pixbuf)
diff --git a/app/dialogs/dialogs.c b/app/dialogs/dialogs.c
index 6112808..f9e6a97 100644
--- a/app/dialogs/dialogs.c
+++ b/app/dialogs/dialogs.c
@@ -562,11 +562,7 @@ dialogs_exit (Gimp *gimp)
       gimp_dialog_factory_set_singleton (NULL);
     }
 
-  if (global_recent_docks)
-    {
-      g_object_unref (global_recent_docks);
-      global_recent_docks = NULL;
-    }
+  g_clear_object (&global_recent_docks);
 }
 
 static void
diff --git a/app/display/gimpcanvas.c b/app/display/gimpcanvas.c
index 44965ec..9c51194 100644
--- a/app/display/gimpcanvas.c
+++ b/app/display/gimpcanvas.c
@@ -148,11 +148,7 @@ gimp_canvas_unrealize (GtkWidget *widget)
 {
   GimpCanvas *canvas = GIMP_CANVAS (widget);
 
-  if (canvas->layout)
-    {
-      g_object_unref (canvas->layout);
-      canvas->layout = NULL;
-    }
+  g_clear_object (&canvas->layout);
 
   GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
 }
@@ -165,11 +161,7 @@ gimp_canvas_style_set (GtkWidget *widget,
 
   GTK_WIDGET_CLASS (parent_class)->style_set (widget, prev_style);
 
-  if (canvas->layout)
-    {
-      g_object_unref (canvas->layout);
-      canvas->layout = NULL;
-    }
+  g_clear_object (&canvas->layout);
 }
 
 static gboolean
diff --git a/app/display/gimpcanvasgrid.c b/app/display/gimpcanvasgrid.c
index 09038d9..e292f7a 100644
--- a/app/display/gimpcanvasgrid.c
+++ b/app/display/gimpcanvasgrid.c
@@ -123,11 +123,7 @@ gimp_canvas_grid_finalize (GObject *object)
 {
   GimpCanvasGridPrivate *private = GET_PRIVATE (object);
 
-  if (private->grid)
-    {
-      g_object_unref (private->grid);
-      private->grid = NULL;
-    }
+  g_clear_object (&private->grid);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/display/gimpcanvasitem.c b/app/display/gimpcanvasitem.c
index f7ea879..d216936 100644
--- a/app/display/gimpcanvasitem.c
+++ b/app/display/gimpcanvasitem.c
@@ -517,10 +517,9 @@ gimp_canvas_item_end_change (GimpCanvasItem *item)
               cairo_region_destroy (region);
             }
         }
-      else if (private->change_region)
+      else
         {
-          cairo_region_destroy (private->change_region);
-          private->change_region = NULL;
+          g_clear_pointer (&private->change_region, cairo_region_destroy);
         }
     }
 }
diff --git a/app/display/gimpcanvasproxygroup.c b/app/display/gimpcanvasproxygroup.c
index 60e7335..9ed00e1 100644
--- a/app/display/gimpcanvasproxygroup.c
+++ b/app/display/gimpcanvasproxygroup.c
@@ -96,11 +96,7 @@ gimp_canvas_proxy_group_finalize (GObject *object)
 {
   GimpCanvasProxyGroupPrivate *private = GET_PRIVATE (object);
 
-  if (private->proxy_hash)
-    {
-      g_hash_table_unref (private->proxy_hash);
-      private->proxy_hash = NULL;
-    }
+  g_clear_pointer (&private->proxy_hash, g_hash_table_unref);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/display/gimpdisplay.c b/app/display/gimpdisplay.c
index 2691de0..5d6b8ed 100644
--- a/app/display/gimpdisplay.c
+++ b/app/display/gimpdisplay.c
@@ -650,11 +650,7 @@ gimp_display_set_image (GimpDisplay *display,
 
       gimp_display_disconnect (display);
 
-      if (private->update_region)
-        {
-          cairo_region_destroy (private->update_region);
-          private->update_region = NULL;
-        }
+      g_clear_pointer (&private->update_region, cairo_region_destroy);
 
       gimp_image_dec_display_count (private->image);
 
@@ -851,8 +847,7 @@ gimp_display_flush_whenever (GimpDisplay *display,
                                    rect.height);
         }
 
-      cairo_region_destroy (private->update_region);
-      private->update_region = NULL;
+      g_clear_pointer (&private->update_region, cairo_region_destroy);
     }
 
   if (now)
diff --git a/app/display/gimpdisplayshell-handlers.c b/app/display/gimpdisplayshell-handlers.c
index 0d94957..dacae45 100644
--- a/app/display/gimpdisplayshell-handlers.c
+++ b/app/display/gimpdisplayshell-handlers.c
@@ -976,11 +976,7 @@ gimp_display_shell_check_notify_handler (GObject          *config,
   GimpCanvasPaddingMode padding_mode;
   GimpRGB               padding_color;
 
-  if (shell->checkerboard)
-    {
-      cairo_pattern_destroy (shell->checkerboard);
-      shell->checkerboard = NULL;
-    }
+  g_clear_pointer (&shell->checkerboard, cairo_pattern_destroy);
 
   gimp_display_shell_get_padding (shell, &padding_mode, &padding_color);
 
@@ -1011,11 +1007,7 @@ gimp_display_shell_nav_size_notify_handler (GObject          *config,
                                             GParamSpec       *param_spec,
                                             GimpDisplayShell *shell)
 {
-  if (shell->nav_popup)
-    {
-      gtk_widget_destroy (shell->nav_popup);
-      shell->nav_popup = NULL;
-    }
+  g_clear_pointer (&shell->nav_popup, gtk_widget_destroy);
 }
 
 static void
diff --git a/app/display/gimpdisplayshell-items.c b/app/display/gimpdisplayshell-items.c
index c92ff5b..d20d4ca 100644
--- a/app/display/gimpdisplayshell-items.c
+++ b/app/display/gimpdisplayshell-items.c
@@ -120,8 +120,7 @@ gimp_display_shell_items_free (GimpDisplayShell *shell)
                                             gimp_display_shell_item_update,
                                             shell);
 
-      g_object_unref (shell->canvas_item);
-      shell->canvas_item = NULL;
+      g_clear_object (&shell->canvas_item);
 
       shell->passe_partout  = NULL;
       shell->preview_items  = NULL;
@@ -139,8 +138,7 @@ gimp_display_shell_items_free (GimpDisplayShell *shell)
                                             gimp_display_shell_unrotated_item_update,
                                             shell);
 
-      g_object_unref (shell->unrotated_item);
-      shell->unrotated_item = NULL;
+      g_clear_object (&shell->unrotated_item);
 
       shell->cursor = NULL;
     }
diff --git a/app/display/gimpdisplayshell-profile.c b/app/display/gimpdisplayshell-profile.c
index dc56d34..272b215 100644
--- a/app/display/gimpdisplayshell-profile.c
+++ b/app/display/gimpdisplayshell-profile.c
@@ -76,11 +76,7 @@ gimp_display_shell_profile_init (GimpDisplayShell *shell)
 void
 gimp_display_shell_profile_finalize (GimpDisplayShell *shell)
 {
-  if (shell->color_config)
-    {
-      g_object_unref (shell->color_config);
-      shell->color_config = NULL;
-    }
+  g_clear_object (&shell->color_config);
 
   gimp_display_shell_profile_free (shell);
 }
@@ -187,19 +183,10 @@ gimp_display_shell_profile_can_convert_to_u8 (GimpDisplayShell *shell)
 static void
 gimp_display_shell_profile_free (GimpDisplayShell *shell)
 {
-  if (shell->profile_transform)
-    {
-      g_object_unref (shell->profile_transform);
-      shell->profile_transform = NULL;
-    }
-
-  if (shell->profile_buffer)
-    {
-      g_object_unref (shell->profile_buffer);
-      shell->profile_buffer = NULL;
-      shell->profile_data   = NULL;
-      shell->profile_stride = 0;
-    }
+  g_clear_object (&shell->profile_transform);
+  g_clear_object (&shell->profile_buffer);
+  shell->profile_data   = NULL;
+  shell->profile_stride = 0;
 }
 
 static void
diff --git a/app/display/gimpdisplayshell-selection.c b/app/display/gimpdisplayshell-selection.c
index ee517d6..8d7ea07 100644
--- a/app/display/gimpdisplayshell-selection.c
+++ b/app/display/gimpdisplayshell-selection.c
@@ -401,25 +401,13 @@ selection_generate_segs (Selection *selection)
 static void
 selection_free_segs (Selection *selection)
 {
-  if (selection->segs_in)
-    {
-      g_free (selection->segs_in);
-      selection->segs_in   = NULL;
-      selection->n_segs_in = 0;
-    }
+  g_clear_pointer (&selection->segs_in, g_free);
+  selection->n_segs_in = 0;
 
-  if (selection->segs_out)
-    {
-      g_free (selection->segs_out);
-      selection->segs_out   = NULL;
-      selection->n_segs_out = 0;
-    }
+  g_clear_pointer (&selection->segs_out, g_free);
+  selection->segs_out = NULL;
 
-  if (selection->segs_in_mask)
-    {
-      cairo_pattern_destroy (selection->segs_in_mask);
-      selection->segs_in_mask = NULL;
-    }
+  g_clear_pointer (&selection->segs_in_mask, cairo_pattern_destroy);
 }
 
 static gboolean
diff --git a/app/display/gimpdisplayshell.c b/app/display/gimpdisplayshell.c
index 6f56761..4c91f20 100644
--- a/app/display/gimpdisplayshell.c
+++ b/app/display/gimpdisplayshell.c
@@ -819,47 +819,22 @@ gimp_display_shell_dispose (GObject *object)
       shell->filter_idle_id = 0;
     }
 
-  if (shell->mask_surface)
-    {
-      cairo_surface_destroy (shell->mask_surface);
-      shell->mask_surface = NULL;
-    }
-
-  if (shell->checkerboard)
-    {
-      cairo_pattern_destroy (shell->checkerboard);
-      shell->checkerboard = NULL;
-    }
+  g_clear_pointer (&shell->mask_surface, cairo_surface_destroy);
+  g_clear_pointer (&shell->checkerboard, cairo_pattern_destroy);
 
   gimp_display_shell_profile_finalize (shell);
 
-  if (shell->filter_buffer)
-    {
-      g_object_unref (shell->filter_buffer);
-      shell->filter_buffer = NULL;
-      shell->filter_data   = NULL;
-      shell->filter_stride = 0;
-    }
+  g_clear_object (&shell->filter_buffer);
+  shell->filter_data   = NULL;
+  shell->filter_stride = 0;
 
-  if (shell->mask)
-    {
-      g_object_unref (shell->mask);
-      shell->mask = NULL;
-    }
+  g_clear_object (&shell->mask);
 
   gimp_display_shell_items_free (shell);
 
-  if (shell->motion_buffer)
-    {
-      g_object_unref (shell->motion_buffer);
-      shell->motion_buffer = NULL;
-    }
+  g_clear_object (&shell->motion_buffer);
 
-  if (shell->zoom_focus_pointer_queue)
-    {
-      g_queue_free (shell->zoom_focus_pointer_queue);
-      shell->zoom_focus_pointer_queue = NULL;
-    }
+  g_clear_pointer (&shell->zoom_focus_pointer_queue, g_queue_free);
 
   if (shell->title_idle_id)
     {
@@ -873,11 +848,7 @@ gimp_display_shell_dispose (GObject *object)
       shell->fill_idle_id = 0;
     }
 
-  if (shell->nav_popup)
-    {
-      gtk_widget_destroy (shell->nav_popup);
-      shell->nav_popup = NULL;
-    }
+  g_clear_pointer (&shell->nav_popup, gtk_widget_destroy);
 
   if (shell->blink_timeout_id)
     {
@@ -895,31 +866,15 @@ gimp_display_shell_finalize (GObject *object)
 {
   GimpDisplayShell *shell = GIMP_DISPLAY_SHELL (object);
 
-  g_object_unref (shell->zoom);
-
-  if (shell->rotate_transform)
-    g_free (shell->rotate_transform);
-
-  if (shell->rotate_untransform)
-    g_free (shell->rotate_untransform);
-
-  if (shell->options)
-    g_object_unref (shell->options);
-
-  if (shell->fullscreen_options)
-    g_object_unref (shell->fullscreen_options);
-
-  if (shell->no_image_options)
-    g_object_unref (shell->no_image_options);
-
-  if (shell->title)
-    g_free (shell->title);
-
-  if (shell->status)
-    g_free (shell->status);
-
-  if (shell->icon)
-    g_object_unref (shell->icon);
+  g_clear_object (&shell->zoom);
+  g_clear_pointer (&shell->rotate_transform,   g_free);
+  g_clear_pointer (&shell->rotate_untransform, g_free);
+  g_clear_object (&shell->options);
+  g_clear_object (&shell->fullscreen_options);
+  g_clear_object (&shell->no_image_options);
+  g_clear_pointer (&shell->title,  g_free);
+  g_clear_pointer (&shell->status, g_free);
+  g_clear_object (&shell->icon);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/display/gimpimagewindow.c b/app/display/gimpimagewindow.c
index b0371d1..eb2e827 100644
--- a/app/display/gimpimagewindow.c
+++ b/app/display/gimpimagewindow.c
@@ -528,11 +528,7 @@ gimp_image_window_dispose (GObject *object)
       private->dialog_factory = NULL;
     }
 
-  if (private->menubar_manager)
-    {
-      g_object_unref (private->menubar_manager);
-      private->menubar_manager = NULL;
-    }
+  g_clear_object (&private->menubar_manager);
 
   if (private->update_ui_manager_idle_id)
     {
diff --git a/app/display/gimpstatusbar.c b/app/display/gimpstatusbar.c
index df247e4..d15cdea 100644
--- a/app/display/gimpstatusbar.c
+++ b/app/display/gimpstatusbar.c
@@ -309,27 +309,14 @@ gimp_statusbar_finalize (GObject *object)
 {
   GimpStatusbar *statusbar = GIMP_STATUSBAR (object);
 
-  if (statusbar->icon)
-    {
-      g_object_unref (statusbar->icon);
-      statusbar->icon = NULL;
-    }
-
-  if (statusbar->icon_hash)
-    {
-      g_hash_table_unref (statusbar->icon_hash);
-      statusbar->icon_hash = NULL;
-    }
+  g_clear_object (&statusbar->icon);
+  g_clear_pointer (&statusbar->icon_hash, g_hash_table_unref);
 
   g_slist_free_full (statusbar->messages,
                      (GDestroyNotify) gimp_statusbar_msg_free);
   statusbar->messages = NULL;
 
-  if (statusbar->context_ids)
-    {
-      g_hash_table_destroy (statusbar->context_ids);
-      statusbar->context_ids = NULL;
-    }
+  g_clear_pointer (&statusbar->context_ids, g_hash_table_destroy);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -343,11 +330,7 @@ gimp_statusbar_screen_changed (GtkWidget *widget,
   if (GTK_WIDGET_CLASS (parent_class)->screen_changed)
     GTK_WIDGET_CLASS (parent_class)->screen_changed (widget, previous);
 
-  if (statusbar->icon_hash)
-    {
-      g_hash_table_unref (statusbar->icon_hash);
-      statusbar->icon_hash = NULL;
-    }
+  g_clear_pointer (&statusbar->icon_hash, g_hash_table_unref);
 }
 
 static void
@@ -358,11 +341,7 @@ gimp_statusbar_style_set (GtkWidget *widget,
 
   GTK_WIDGET_CLASS (parent_class)->style_set (widget, prev_style);
 
-  if (statusbar->icon_hash)
-    {
-      g_hash_table_unref (statusbar->icon_hash);
-      statusbar->icon_hash = NULL;
-    }
+  g_clear_pointer (&statusbar->icon_hash, g_hash_table_unref);
 }
 
 static void
@@ -656,11 +635,7 @@ gimp_statusbar_set_text (GimpStatusbar *statusbar,
     }
   else
     {
-      if (statusbar->icon)
-        {
-          g_object_unref (statusbar->icon);
-          statusbar->icon = NULL;
-        }
+      g_clear_object (&statusbar->icon);
 
       if (icon_name)
         statusbar->icon = gimp_statusbar_load_icon (statusbar, icon_name);
diff --git a/app/display/gimptoolgui.c b/app/display/gimptoolgui.c
index a8bd141..597a9b9 100644
--- a/app/display/gimptoolgui.c
+++ b/app/display/gimptoolgui.c
@@ -155,20 +155,12 @@ gimp_tool_gui_dispose (GObject *object)
 {
   GimpToolGuiPrivate *private = GET_PRIVATE (object);
 
-  if (private->tool_info)
-    {
-      g_object_unref (private->tool_info);
-      private->tool_info = NULL;
-    }
+  g_clear_object (&private->tool_info);
 
   if (private->shell)
     gimp_tool_gui_set_shell (GIMP_TOOL_GUI (object), NULL);
 
-  if (private->vbox)
-    {
-      g_object_unref (private->vbox);
-      private->vbox = NULL;
-    }
+  g_clear_object (&private->vbox);
 
   if (private->dialog)
     {
@@ -191,29 +183,10 @@ gimp_tool_gui_finalize (GObject *object)
 {
   GimpToolGuiPrivate *private = GET_PRIVATE (object);
 
-  if (private->title)
-    {
-      g_free (private->title);
-      private->title = NULL;
-    }
-
-  if (private->description)
-    {
-      g_free (private->description);
-      private->description = NULL;
-    }
-
-  if (private->icon_name)
-    {
-      g_free (private->icon_name);
-      private->icon_name = NULL;
-    }
-
-  if (private->help_id)
-    {
-      g_free (private->help_id);
-      private->help_id = NULL;
-    }
+  g_clear_pointer (&private->title,       g_free);
+  g_clear_pointer (&private->description, g_free);
+  g_clear_pointer (&private->icon_name,   g_free);
+  g_clear_pointer (&private->help_id,     g_free);
 
   if (private->response_entries)
     {
diff --git a/app/display/gimptoolline.c b/app/display/gimptoolline.c
index aea3adb..36596b2 100644
--- a/app/display/gimptoolline.c
+++ b/app/display/gimptoolline.c
@@ -226,7 +226,7 @@ static void
 gimp_tool_line_init (GimpToolLine *line)
 {
   GimpToolLinePrivate *private;
-  
+
   private = line->private = G_TYPE_INSTANCE_GET_PRIVATE (line,
                                                          GIMP_TYPE_TOOL_LINE,
                                                          GimpToolLinePrivate);
@@ -301,29 +301,10 @@ gimp_tool_line_finalize (GObject *object)
   GimpToolLine        *line    = GIMP_TOOL_LINE (object);
   GimpToolLinePrivate *private = line->private;
 
-  if (private->sliders)
-    {
-      g_array_unref (private->sliders);
-      private->sliders = NULL;
-    }
-
-  if (private->status_title)
-    {
-      g_free (private->status_title);
-      private->status_title = NULL;
-    }
-
-  if (private->slider_handle_circles)
-    {
-      g_array_unref (private->slider_handle_circles);
-      private->slider_handle_circles = NULL;
-    }
-
-  if (private->slider_handle_grips)
-    {
-      g_array_unref (private->slider_handle_grips);
-      private->slider_handle_grips = NULL;
-    }
+  g_clear_pointer (&private->sliders, g_array_unref);
+  g_clear_pointer (&private->status_title, g_free);
+  g_clear_pointer (&private->slider_handle_circles, g_array_unref);
+  g_clear_pointer (&private->slider_handle_grips, g_array_unref);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/display/gimptoolpolygon.c b/app/display/gimptoolpolygon.c
index 800304a..618f60b 100644
--- a/app/display/gimptoolpolygon.c
+++ b/app/display/gimptoolpolygon.c
@@ -262,11 +262,7 @@ gimp_tool_polygon_finalize (GObject *object)
   g_free (private->saved_points_lower_segment);
   g_free (private->saved_points_higher_segment);
 
-  if (private->handles)
-    {
-      g_ptr_array_unref (private->handles);
-      private->handles = NULL;
-    }
+  g_clear_pointer (&private->handles, g_ptr_array_unref);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/gegl/gimptilehandlervalidate.c b/app/gegl/gimptilehandlervalidate.c
index 7eacffd..033975a 100644
--- a/app/gegl/gimptilehandlervalidate.c
+++ b/app/gegl/gimptilehandlervalidate.c
@@ -114,14 +114,8 @@ gimp_tile_handler_validate_finalize (GObject *object)
 {
   GimpTileHandlerValidate *validate = GIMP_TILE_HANDLER_VALIDATE (object);
 
-  if (validate->graph)
-    {
-      g_object_unref (validate->graph);
-      validate->graph = NULL;
-    }
-
-  cairo_region_destroy (validate->dirty_region);
-  validate->dirty_region = NULL;
+  g_clear_object (&validate->graph);
+  g_clear_pointer (&validate->dirty_region, cairo_region_destroy);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/operations/gimpoperationcagecoefcalc.c b/app/operations/gimpoperationcagecoefcalc.c
index af7ec32..7e9bba4 100644
--- a/app/operations/gimpoperationcagecoefcalc.c
+++ b/app/operations/gimpoperationcagecoefcalc.c
@@ -100,11 +100,7 @@ gimp_operation_cage_coef_calc_finalize (GObject *object)
 {
   GimpOperationCageCoefCalc *self = GIMP_OPERATION_CAGE_COEF_CALC (object);
 
-  if (self->config)
-    {
-      g_object_unref (self->config);
-      self->config = NULL;
-    }
+  g_clear_object (&self->config);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/operations/gimpoperationcagetransform.c b/app/operations/gimpoperationcagetransform.c
index 27d238a..a370d83 100644
--- a/app/operations/gimpoperationcagetransform.c
+++ b/app/operations/gimpoperationcagetransform.c
@@ -141,11 +141,7 @@ gimp_operation_cage_transform_finalize (GObject *object)
 {
   GimpOperationCageTransform *self = GIMP_OPERATION_CAGE_TRANSFORM (object);
 
-  if (self->config)
-    {
-      g_object_unref (self->config);
-      self->config = NULL;
-    }
+  g_clear_object (&self->config);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/operations/gimpoperationequalize.c b/app/operations/gimpoperationequalize.c
index 14dce6a..71b8dda 100644
--- a/app/operations/gimpoperationequalize.c
+++ b/app/operations/gimpoperationequalize.c
@@ -105,17 +105,10 @@ gimp_operation_equalize_finalize (GObject *object)
 {
   GimpOperationEqualize *self = GIMP_OPERATION_EQUALIZE (object);
 
-  if (self->values)
-    {
-      g_free (self->values);
-      self->values = NULL;
-    }
+  g_clear_pointer (&self->values, g_free);
+  g_clear_object (&self->histogram);
 
-  if (self->histogram)
-    {
-      g_object_unref (self->histogram);
-      self->histogram = NULL;
-    }
+  G_OBJECT_CLASS (parent_class)->finalize (object);
 }
 
 static void
diff --git a/app/operations/gimpoperationhistogramsink.c b/app/operations/gimpoperationhistogramsink.c
index f4c4a10..aad6757 100644
--- a/app/operations/gimpoperationhistogramsink.c
+++ b/app/operations/gimpoperationhistogramsink.c
@@ -113,11 +113,7 @@ gimp_operation_histogram_sink_finalize (GObject *object)
 {
   GimpOperationHistogramSink *sink = GIMP_OPERATION_HISTOGRAM_SINK (object);
 
-  if (sink->histogram)
-    {
-      g_object_unref (sink->histogram);
-      sink->histogram = NULL;
-    }
+  g_clear_object (&sink->histogram);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/operations/gimpoperationpointfilter.c b/app/operations/gimpoperationpointfilter.c
index 234c183..568e9b0 100644
--- a/app/operations/gimpoperationpointfilter.c
+++ b/app/operations/gimpoperationpointfilter.c
@@ -58,11 +58,7 @@ gimp_operation_point_filter_finalize (GObject *object)
 {
   GimpOperationPointFilter *self = GIMP_OPERATION_POINT_FILTER (object);
 
-  if (self->config)
-    {
-      g_object_unref (self->config);
-      self->config = NULL;
-    }
+  g_clear_object (&self->config);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/operations/gimpoperationprofiletransform.c b/app/operations/gimpoperationprofiletransform.c
index b35b763..cdcd50f 100644
--- a/app/operations/gimpoperationprofiletransform.c
+++ b/app/operations/gimpoperationprofiletransform.c
@@ -150,23 +150,9 @@ gimp_operation_profile_transform_finalize (GObject *object)
 {
   GimpOperationProfileTransform *self = GIMP_OPERATION_PROFILE_TRANSFORM (object);
 
-  if (self->src_profile)
-    {
-      g_object_unref (self->src_profile);
-      self->src_profile = NULL;
-    }
-
-  if (self->dest_profile)
-    {
-      g_object_unref (self->dest_profile);
-      self->dest_profile = NULL;
-    }
-
-  if (self->transform)
-    {
-      g_object_unref (self->transform);
-      self->transform = NULL;
-    }
+  g_clear_object (&self->src_profile);
+  g_clear_object (&self->dest_profile);
+  g_clear_object (&self->transform);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -260,11 +246,7 @@ gimp_operation_profile_transform_prepare (GeglOperation *operation)
 {
   GimpOperationProfileTransform *self = GIMP_OPERATION_PROFILE_TRANSFORM (operation);
 
-  if (self->transform)
-    {
-      g_object_unref (self->transform);
-      self->transform = NULL;
-    }
+  g_clear_object (&self->transform);
 
   if (! self->src_format)
     self->src_format = babl_format ("RGBA float");
diff --git a/app/paint/gimp-paint.c b/app/paint/gimp-paint.c
index deab18a..5818331 100644
--- a/app/paint/gimp-paint.c
+++ b/app/paint/gimp-paint.c
@@ -102,8 +102,7 @@ gimp_paint_exit (Gimp *gimp)
     {
       gimp_container_foreach (gimp->paint_info_list,
                               (GFunc) g_object_run_dispose, NULL);
-      g_object_unref (gimp->paint_info_list);
-      gimp->paint_info_list = NULL;
+      g_clear_object (&gimp->paint_info_list);
     }
 }
 
diff --git a/app/paint/gimpairbrush.c b/app/paint/gimpairbrush.c
index dea4b8c..8ba5e97 100644
--- a/app/paint/gimpairbrush.c
+++ b/app/paint/gimpairbrush.c
@@ -96,11 +96,7 @@ gimp_airbrush_finalize (GObject *object)
       airbrush->timeout_id = 0;
     }
 
-  if (airbrush->sym)
-    {
-      g_object_unref (airbrush->sym);
-      airbrush->sym = NULL;
-    }
+  g_clear_object (&airbrush->sym);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -171,16 +167,12 @@ gimp_airbrush_paint (GimpPaintCore    *paint_core,
       break;
 
     case GIMP_PAINT_STATE_FINISH:
-      if (airbrush->sym)
-        {
-          g_object_unref (airbrush->sym);
-          airbrush->sym = NULL;
-        }
-
       GIMP_PAINT_CORE_CLASS (parent_class)->paint (paint_core, drawable,
                                                    paint_options,
                                                    sym,
                                                    paint_state, time);
+
+      g_clear_object (&airbrush->sym);
       break;
     }
 }
diff --git a/app/paint/gimpbrushcore.c b/app/paint/gimpbrushcore.c
index 6e8ec5b..5453766 100644
--- a/app/paint/gimpbrushcore.c
+++ b/app/paint/gimpbrushcore.c
@@ -245,33 +245,17 @@ gimp_brush_core_finalize (GObject *object)
   GimpBrushCore *core = GIMP_BRUSH_CORE (object);
   gint           i, j;
 
-  if (core->pressure_brush)
-    {
-      gimp_temp_buf_unref (core->pressure_brush);
-      core->pressure_brush = NULL;
-    }
+  g_clear_pointer (&core->pressure_brush, gimp_temp_buf_unref);
 
   for (i = 0; i < BRUSH_CORE_SOLID_SUBSAMPLE; i++)
     for (j = 0; j < BRUSH_CORE_SOLID_SUBSAMPLE; j++)
-      if (core->solid_brushes[i][j])
-        {
-          gimp_temp_buf_unref (core->solid_brushes[i][j]);
-          core->solid_brushes[i][j] = NULL;
-        }
+      g_clear_pointer (&core->solid_brushes[i][j], gimp_temp_buf_unref);
 
-  if (core->rand)
-    {
-      g_rand_free (core->rand);
-      core->rand = NULL;
-    }
+  g_clear_pointer (&core->rand, g_rand_free);
 
   for (i = 0; i < KERNEL_SUBSAMPLE + 1; i++)
     for (j = 0; j < KERNEL_SUBSAMPLE + 1; j++)
-      if (core->subsample_brushes[i][j])
-        {
-          gimp_temp_buf_unref (core->subsample_brushes[i][j]);
-          core->subsample_brushes[i][j] = NULL;
-        }
+      g_clear_pointer (&core->subsample_brushes[i][j], gimp_temp_buf_unref);
 
   if (core->main_brush)
     {
@@ -279,15 +263,10 @@ gimp_brush_core_finalize (GObject *object)
                                             gimp_brush_core_invalidate_cache,
                                             core);
       gimp_brush_end_use (core->main_brush);
-      g_object_unref (core->main_brush);
-      core->main_brush = NULL;
+      g_clear_object (&core->main_brush);
     }
 
-  if (core->dynamics)
-    {
-      g_object_unref (core->dynamics);
-      core->dynamics = NULL;
-    }
+  g_clear_object (&core->dynamics);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -884,8 +863,7 @@ gimp_brush_core_get_paint_buffer (GimpPaintCore    *paint_core,
       *paint_buffer_x = x1;
       *paint_buffer_y = y1;
 
-      if (paint_core->paint_buffer)
-        g_object_unref (paint_core->paint_buffer);
+      g_clear_object (&paint_core->paint_buffer);
 
       paint_core->paint_buffer = gimp_temp_buf_create_buffer (temp_buf);
 
@@ -910,8 +888,7 @@ gimp_brush_core_real_set_brush (GimpBrushCore *core,
                                             gimp_brush_core_invalidate_cache,
                                             core);
       gimp_brush_end_use (core->main_brush);
-      g_object_unref (core->main_brush);
-      core->main_brush = NULL;
+      g_clear_object (&core->main_brush);
     }
 
   core->main_brush = brush;
@@ -933,13 +910,10 @@ gimp_brush_core_real_set_dynamics (GimpBrushCore *core,
   if (dynamics == core->dynamics)
     return;
 
-  if (core->dynamics)
-    g_object_unref (core->dynamics);
-
-  core->dynamics = dynamics;
+  g_clear_object (&core->dynamics);
 
-  if (core->dynamics)
-    g_object_ref (core->dynamics);
+  if (dynamics)
+    core->dynamics = g_object_ref (dynamics);
 }
 
 void
@@ -1158,11 +1132,7 @@ gimp_brush_core_subsample_mask (GimpBrushCore     *core,
     {
       for (i = 0; i < KERNEL_SUBSAMPLE + 1; i++)
         for (j = 0; j < KERNEL_SUBSAMPLE + 1; j++)
-          if (core->subsample_brushes[i][j])
-            {
-              gimp_temp_buf_unref (core->subsample_brushes[i][j]);
-              core->subsample_brushes[i][j] = NULL;
-            }
+          g_clear_pointer (&core->subsample_brushes[i][j], gimp_temp_buf_unref);
 
       core->last_subsample_brush_mask = mask;
       core->subsample_cache_invalid   = FALSE;
@@ -1249,8 +1219,7 @@ gimp_brush_core_pressurize_mask (GimpBrushCore     *core,
   if ((gint) (pressure * 100 + 0.5) == 50)
     return subsample_mask;
 
-  if (core->pressure_brush)
-    gimp_temp_buf_unref (core->pressure_brush);
+  g_clear_pointer (&core->pressure_brush, gimp_temp_buf_unref);
 
   core->pressure_brush =
     gimp_temp_buf_new (gimp_temp_buf_get_width  (brush_mask) + 2,
@@ -1388,11 +1357,7 @@ gimp_brush_core_solidify_mask (GimpBrushCore     *core,
     {
       for (i = 0; i < BRUSH_CORE_SOLID_SUBSAMPLE; i++)
         for (j = 0; j < BRUSH_CORE_SOLID_SUBSAMPLE; j++)
-          if (core->solid_brushes[i][j])
-            {
-              gimp_temp_buf_unref (core->solid_brushes[i][j]);
-              core->solid_brushes[i][j] = NULL;
-            }
+          g_clear_pointer (&core->solid_brushes[i][j], gimp_temp_buf_unref);
 
       core->last_solid_brush_mask = brush_mask;
       core->solid_cache_invalid   = FALSE;
diff --git a/app/paint/gimpmybrushsurface.c b/app/paint/gimpmybrushsurface.c
index 6ba0c95..63c0441 100644
--- a/app/paint/gimpmybrushsurface.c
+++ b/app/paint/gimpmybrushsurface.c
@@ -508,10 +508,11 @@ gimp_mypaint_surface_end_atomic (MyPaintSurface   *base_surface,
                                  MyPaintRectangle *roi)
 {
   GimpMybrushSurface *surface = (GimpMybrushSurface *)base_surface;
-  roi->x = surface->dirty.x;
-  roi->y = surface->dirty.y;
-  roi->width = surface->dirty.width;
-  roi->height = surface->dirty.height;
+
+  roi->x         = surface->dirty.x;
+  roi->y         = surface->dirty.y;
+  roi->width     = surface->dirty.width;
+  roi->height    = surface->dirty.height;
   surface->dirty = *GEGL_RECTANGLE (0, 0, 0, 0);
 }
 
@@ -519,11 +520,9 @@ static void
 gimp_mypaint_surface_destroy (MyPaintSurface *base_surface)
 {
   GimpMybrushSurface *surface = (GimpMybrushSurface *)base_surface;
-  g_object_unref (surface->buffer);
-  surface->buffer = NULL;
-  if (surface->paint_mask)
-    g_object_unref (surface->paint_mask);
-  surface->paint_mask = NULL;
+
+  g_clear_object (&surface->buffer);
+  g_clear_object (&surface->paint_mask);
 }
 
 GimpMybrushSurface *
@@ -534,21 +533,22 @@ gimp_mypaint_surface_new (GeglBuffer        *buffer,
                           gint               paint_mask_y)
 {
   GimpMybrushSurface *surface = g_malloc0 (sizeof (GimpMybrushSurface));
+
   mypaint_surface_init ((MyPaintSurface *)surface);
-  surface->surface.get_color = gimp_mypaint_surface_get_color;
-  surface->surface.draw_dab = gimp_mypaint_surface_draw_dab;
+
+  surface->surface.get_color    = gimp_mypaint_surface_get_color;
+  surface->surface.draw_dab     = gimp_mypaint_surface_draw_dab;
   surface->surface.begin_atomic = gimp_mypaint_surface_begin_atomic;
-  surface->surface.end_atomic = gimp_mypaint_surface_end_atomic;
-  surface->surface.destroy = gimp_mypaint_surface_destroy;
-  surface->component_mask = component_mask;
-  surface->buffer = g_object_ref (buffer);
+  surface->surface.end_atomic   = gimp_mypaint_surface_end_atomic;
+  surface->surface.destroy      = gimp_mypaint_surface_destroy;
+  surface->component_mask       = component_mask;
+  surface->buffer               = g_object_ref (buffer);
   if (paint_mask)
-    surface->paint_mask = g_object_ref (paint_mask);
-  else
-    surface->paint_mask = NULL;
-  surface->paint_mask_x = paint_mask_x;
-  surface->paint_mask_y = paint_mask_y;
-  surface->dirty = *GEGL_RECTANGLE (0, 0, 0, 0);
+    surface->paint_mask         = g_object_ref (paint_mask);
+
+  surface->paint_mask_x         = paint_mask_x;
+  surface->paint_mask_y         = paint_mask_y;
+  surface->dirty                = *GEGL_RECTANGLE (0, 0, 0, 0);
 
   return surface;
 }
diff --git a/app/paint/gimppaintcore.c b/app/paint/gimppaintcore.c
index ab1316a..3488902 100644
--- a/app/paint/gimppaintcore.c
+++ b/app/paint/gimppaintcore.c
@@ -162,8 +162,7 @@ gimp_paint_core_finalize (GObject *object)
 
   gimp_paint_core_cleanup (core);
 
-  g_free (core->undo_desc);
-  core->undo_desc = NULL;
+  g_clear_pointer (&core->undo_desc, g_free);
 
   if (core->stroke_buffer)
     {
@@ -394,11 +393,7 @@ gimp_paint_core_start (GimpPaintCore     *core,
   core->undo_buffer = gegl_buffer_dup (gimp_drawable_get_buffer (drawable));
 
   /*  Allocate the saved proj structure  */
-  if (core->saved_proj_buffer)
-    {
-      g_object_unref (core->saved_proj_buffer);
-      core->saved_proj_buffer = NULL;
-    }
+  g_clear_object (&core->saved_proj_buffer);
 
   if (core->use_saved_proj)
     {
@@ -466,11 +461,7 @@ gimp_paint_core_start (GimpPaintCore     *core,
     }
   else
     {
-      if (core->comp_buffer)
-        {
-          g_object_unref (core->comp_buffer);
-          core->comp_buffer = NULL;
-        }
+      g_clear_object (&core->comp_buffer);
 
       /* Allocate the scratch buffer if there's a component mask */
       if (gimp_drawable_get_active_mask (drawable) != GIMP_COMPONENT_MASK_ALL)
@@ -507,11 +498,7 @@ gimp_paint_core_finish (GimpPaintCore *core,
   g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
   g_return_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)));
 
-  if (core->applicator)
-    {
-      g_object_unref (core->applicator);
-      core->applicator = NULL;
-    }
+  g_clear_object (&core->applicator);
 
   if (core->stroke_buffer)
     {
@@ -519,17 +506,8 @@ gimp_paint_core_finish (GimpPaintCore *core,
       core->stroke_buffer = NULL;
     }
 
-  if (core->mask_buffer)
-    {
-      g_object_unref (core->mask_buffer);
-      core->mask_buffer = NULL;
-    }
-
-  if (core->comp_buffer)
-    {
-      g_object_unref (core->comp_buffer);
-      core->comp_buffer = NULL;
-    }
+  g_clear_object (&core->mask_buffer);
+  g_clear_object (&core->comp_buffer);
 
   image = gimp_item_get_image (GIMP_ITEM (drawable));
 
@@ -576,14 +554,8 @@ gimp_paint_core_finish (GimpPaintCore *core,
       gimp_image_undo_group_end (image);
     }
 
-  g_object_unref (core->undo_buffer);
-  core->undo_buffer = NULL;
-
-  if (core->saved_proj_buffer)
-    {
-      g_object_unref (core->saved_proj_buffer);
-      core->saved_proj_buffer = NULL;
-    }
+  g_clear_object (&core->undo_buffer);
+  g_clear_object (&core->saved_proj_buffer);
 
   gimp_viewable_preview_thaw (GIMP_VIEWABLE (drawable));
 }
@@ -620,14 +592,8 @@ gimp_paint_core_cancel (GimpPaintCore *core,
                         GEGL_RECTANGLE (x, y, width, height));
     }
 
-  g_object_unref (core->undo_buffer);
-  core->undo_buffer = NULL;
-
-  if (core->saved_proj_buffer)
-    {
-      g_object_unref (core->saved_proj_buffer);
-      core->saved_proj_buffer = NULL;
-    }
+  g_clear_object (&core->undo_buffer);
+  g_clear_object (&core->saved_proj_buffer);
 
   gimp_drawable_update (drawable, x, y, width, height);
 
@@ -639,29 +605,10 @@ gimp_paint_core_cleanup (GimpPaintCore *core)
 {
   g_return_if_fail (GIMP_IS_PAINT_CORE (core));
 
-  if (core->undo_buffer)
-    {
-      g_object_unref (core->undo_buffer);
-      core->undo_buffer = NULL;
-    }
-
-  if (core->saved_proj_buffer)
-    {
-      g_object_unref (core->saved_proj_buffer);
-      core->saved_proj_buffer = NULL;
-    }
-
-  if (core->canvas_buffer)
-    {
-      g_object_unref (core->canvas_buffer);
-      core->canvas_buffer = NULL;
-    }
-
-  if (core->paint_buffer)
-    {
-      g_object_unref (core->paint_buffer);
-      core->paint_buffer = NULL;
-    }
+  g_clear_object (&core->undo_buffer);
+  g_clear_object (&core->saved_proj_buffer);
+  g_clear_object (&core->canvas_buffer);
+  g_clear_object (&core->paint_buffer);
 }
 
 void
diff --git a/app/paint/gimpperspectiveclone.c b/app/paint/gimpperspectiveclone.c
index 1e58c0b..4eafd68 100644
--- a/app/paint/gimpperspectiveclone.c
+++ b/app/paint/gimpperspectiveclone.c
@@ -331,14 +331,10 @@ gimp_perspective_clone_paint (GimpPaintCore    *paint_core,
       break;
 
     case GIMP_PAINT_STATE_FINISH:
-      if (clone->node)
-        {
-          g_object_unref (clone->node);
-          clone->node           = NULL;
-          clone->crop           = NULL;
-          clone->transform_node = NULL;
-          clone->dest_node      = NULL;
-        }
+      g_clear_object (&clone->node);
+      clone->crop           = NULL;
+      clone->transform_node = NULL;
+      clone->dest_node      = NULL;
       break;
 
     default:
diff --git a/app/pdb/gimppdbcontext.c b/app/pdb/gimppdbcontext.c
index 673a5e8..abdcb3e 100644
--- a/app/pdb/gimppdbcontext.c
+++ b/app/pdb/gimppdbcontext.c
@@ -260,17 +260,8 @@ gimp_pdb_context_finalize (GObject *object)
 {
   GimpPDBContext *context = GIMP_PDB_CONTEXT (object);
 
-  if (context->paint_options_list)
-    {
-      g_object_unref (context->paint_options_list);
-      context->paint_options_list = NULL;
-    }
-
-  if (context->stroke_options)
-    {
-      g_object_unref (context->stroke_options);
-      context->stroke_options = NULL;
-    }
+  g_clear_object (&context->paint_options_list);
+  g_clear_object (&context->stroke_options);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/plug-in/gimpplugin-progress.c b/app/plug-in/gimpplugin-progress.c
index 207d6b8..ec65143 100644
--- a/app/plug-in/gimpplugin-progress.c
+++ b/app/plug-in/gimpplugin-progress.c
@@ -170,8 +170,7 @@ gimp_plug_in_progress_end (GimpPlugIn          *plug_in,
       if (proc_frame->progress_created)
         {
           gimp_free_progress (plug_in->manager->gimp, proc_frame->progress);
-          g_object_unref (proc_frame->progress);
-          proc_frame->progress = NULL;
+          g_clear_object (&proc_frame->progress);
         }
     }
 }
@@ -275,11 +274,7 @@ gimp_plug_in_progress_install (GimpPlugIn  *plug_in,
     {
       gimp_plug_in_progress_end (plug_in, proc_frame);
 
-      if (proc_frame->progress)
-        {
-          g_object_unref (proc_frame->progress);
-          proc_frame->progress = NULL;
-        }
+      g_clear_object (&proc_frame->progress);
     }
 
   proc_frame->progress = g_object_new (GIMP_TYPE_PDB_PROGRESS,
@@ -307,8 +302,8 @@ gimp_plug_in_progress_uninstall (GimpPlugIn  *plug_in,
   if (GIMP_IS_PDB_PROGRESS (proc_frame->progress))
     {
       gimp_plug_in_progress_end (plug_in, proc_frame);
-      g_object_unref (proc_frame->progress);
-      proc_frame->progress = NULL;
+
+      g_clear_object (&proc_frame->progress);
 
       return TRUE;
     }
diff --git a/app/plug-in/gimpplugin.c b/app/plug-in/gimpplugin.c
index 2d6bf77..fabb7dd 100644
--- a/app/plug-in/gimpplugin.c
+++ b/app/plug-in/gimpplugin.c
@@ -168,7 +168,7 @@ gimp_plug_in_finalize (GObject *object)
 {
   GimpPlugIn *plug_in = GIMP_PLUG_IN (object);
 
-  g_object_unref (plug_in->file);
+  g_clear_object (&plug_in->file);
 
   gimp_plug_in_proc_frame_dispose (&plug_in->main_proc_frame, plug_in);
 
@@ -378,11 +378,8 @@ gimp_plug_in_open (GimpPlugIn         *plug_in,
       goto cleanup;
     }
 
-  g_io_channel_unref (plug_in->his_read);
-  plug_in->his_read = NULL;
-
-  g_io_channel_unref (plug_in->his_write);
-  plug_in->his_write = NULL;
+  g_clear_pointer (&plug_in->his_read,  g_io_channel_unref);
+  g_clear_pointer (&plug_in->his_write, g_io_channel_unref);
 
   if (! synchronous)
     {
@@ -513,26 +510,10 @@ gimp_plug_in_close (GimpPlugIn *plug_in,
     }
 
   /* Close the pipes. */
-  if (plug_in->my_read != NULL)
-    {
-      g_io_channel_unref (plug_in->my_read);
-      plug_in->my_read = NULL;
-    }
-  if (plug_in->my_write != NULL)
-    {
-      g_io_channel_unref (plug_in->my_write);
-      plug_in->my_write = NULL;
-    }
-  if (plug_in->his_read != NULL)
-    {
-      g_io_channel_unref (plug_in->his_read);
-      plug_in->his_read = NULL;
-    }
-  if (plug_in->his_write != NULL)
-    {
-      g_io_channel_unref (plug_in->his_write);
-      plug_in->his_write = NULL;
-    }
+  g_clear_pointer (&plug_in->my_read,   g_io_channel_unref);
+  g_clear_pointer (&plug_in->my_write,  g_io_channel_unref);
+  g_clear_pointer (&plug_in->his_read,  g_io_channel_unref);
+  g_clear_pointer (&plug_in->his_write, g_io_channel_unref);
 
   gimp_wire_clear_error ();
 
@@ -754,11 +735,8 @@ gimp_plug_in_prep_for_exec (gpointer data)
 {
   GimpPlugIn *plug_in = data;
 
-  g_io_channel_unref (plug_in->my_read);
-  plug_in->my_read  = NULL;
-
-  g_io_channel_unref (plug_in->my_write);
-  plug_in->my_write  = NULL;
+  g_clear_pointer (&plug_in->my_read,  g_io_channel_unref);
+  g_clear_pointer (&plug_in->my_write, g_io_channel_unref);
 }
 
 #endif
@@ -830,8 +808,7 @@ gimp_plug_in_main_loop (GimpPlugIn *plug_in)
   g_main_loop_run (proc_frame->main_loop);
   gimp_threads_enter (plug_in->manager->gimp);
 
-  g_main_loop_unref (proc_frame->main_loop);
-  proc_frame->main_loop = NULL;
+  g_clear_pointer (&proc_frame->main_loop, g_main_loop_unref);
 }
 
 void
diff --git a/app/plug-in/gimppluginmanager-call.c b/app/plug-in/gimppluginmanager-call.c
index c528df1..a78a45d 100644
--- a/app/plug-in/gimppluginmanager-call.c
+++ b/app/plug-in/gimppluginmanager-call.c
@@ -263,8 +263,7 @@ gimp_plug_in_manager_call_run (GimpPlugInManager   *manager,
 
           /*  main_loop is quit in gimp_plug_in_handle_extension_ack()  */
 
-          g_main_loop_unref (plug_in->ext_main_loop);
-          plug_in->ext_main_loop = NULL;
+          g_clear_pointer (&plug_in->ext_main_loop, g_main_loop_unref);
         }
 
       /* If this plug-in is requested to run synchronously,
@@ -282,8 +281,7 @@ gimp_plug_in_manager_call_run (GimpPlugInManager   *manager,
 
           /*  main_loop is quit in gimp_plug_in_handle_proc_return()  */
 
-          g_main_loop_unref (proc_frame->main_loop);
-          proc_frame->main_loop = NULL;
+          g_clear_pointer (&proc_frame->main_loop, g_main_loop_unref);
 
           return_vals = gimp_plug_in_proc_frame_get_return_values (proc_frame);
         }
diff --git a/app/plug-in/gimppluginmanager.c b/app/plug-in/gimppluginmanager.c
index c16d430..b1e75df 100644
--- a/app/plug-in/gimppluginmanager.c
+++ b/app/plug-in/gimppluginmanager.c
@@ -170,17 +170,8 @@ gimp_plug_in_manager_finalize (GObject *object)
       manager->plug_in_defs = NULL;
     }
 
-  if (manager->environ_table)
-    {
-      g_object_unref (manager->environ_table);
-      manager->environ_table = NULL;
-    }
-
-  if (manager->interpreter_db)
-    {
-      g_object_unref (manager->interpreter_db);
-      manager->interpreter_db = NULL;
-    }
+  g_clear_object (&manager->environ_table);
+  g_clear_object (&manager->interpreter_db);
 
   if (manager->debug)
     {
diff --git a/app/plug-in/gimppluginprocframe.c b/app/plug-in/gimppluginprocframe.c
index be3e723..56a91fc 100644
--- a/app/plug-in/gimppluginprocframe.c
+++ b/app/plug-in/gimppluginprocframe.c
@@ -100,11 +100,7 @@ gimp_plug_in_proc_frame_dispose (GimpPlugInProcFrame *proc_frame,
     {
       gimp_plug_in_progress_end (plug_in, proc_frame);
 
-      if (proc_frame->progress)
-        {
-          g_object_unref (proc_frame->progress);
-          proc_frame->progress = NULL;
-        }
+      g_clear_object (&proc_frame->progress);
     }
 
   if (proc_frame->context_stack)
@@ -114,32 +110,14 @@ gimp_plug_in_proc_frame_dispose (GimpPlugInProcFrame *proc_frame,
       proc_frame->context_stack = NULL;
     }
 
-  if (proc_frame->main_context)
-    {
-      g_object_unref (proc_frame->main_context);
-      proc_frame->main_context = NULL;
-    }
-
-  if (proc_frame->return_vals)
-    {
-      gimp_value_array_unref (proc_frame->return_vals);
-      proc_frame->return_vals = NULL;
-    }
-
-  if (proc_frame->main_loop)
-    {
-      g_main_loop_unref (proc_frame->main_loop);
-      proc_frame->main_loop = NULL;
-    }
+  g_clear_object (&proc_frame->main_context);
+  g_clear_pointer (&proc_frame->return_vals, gimp_value_array_unref);
+  g_clear_pointer (&proc_frame->main_loop, g_main_loop_unref);
 
   if (proc_frame->image_cleanups || proc_frame->item_cleanups)
     gimp_plug_in_cleanup (plug_in, proc_frame);
 
-  if (proc_frame->procedure)
-    {
-      g_object_unref (proc_frame->procedure);
-      proc_frame->procedure = NULL;
-    }
+  g_clear_object (&proc_frame->procedure);
 }
 
 GimpPlugInProcFrame *
diff --git a/app/text/gimp-fonts.c b/app/text/gimp-fonts.c
index b3a6095..d757f86 100644
--- a/app/text/gimp-fonts.c
+++ b/app/text/gimp-fonts.c
@@ -77,8 +77,7 @@ gimp_fonts_exit (Gimp *gimp)
                                               G_CALLBACK (gimp_fonts_load),
                                               gimp);
 
-      g_object_unref (gimp->fonts);
-      gimp->fonts = NULL;
+      g_clear_object (&gimp->fonts);
     }
 }
 
diff --git a/app/text/gimpfont.c b/app/text/gimpfont.c
index b76abfa..ea7e833 100644
--- a/app/text/gimpfont.c
+++ b/app/text/gimpfont.c
@@ -142,17 +142,8 @@ gimp_font_finalize (GObject *object)
 {
   GimpFont *font = GIMP_FONT (object);
 
-  if (font->pango_context)
-    {
-      g_object_unref (font->pango_context);
-      font->pango_context = NULL;
-    }
-
-  if (font->popup_layout)
-    {
-      g_object_unref (font->popup_layout);
-      font->popup_layout = NULL;
-    }
+  g_clear_object (&font->pango_context);
+  g_clear_object (&font->popup_layout);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/text/gimptextlayer.c b/app/text/gimptextlayer.c
index 2c1b39b..877e3b0 100644
--- a/app/text/gimptextlayer.c
+++ b/app/text/gimptextlayer.c
@@ -192,11 +192,7 @@ gimp_text_layer_finalize (GObject *object)
 {
   GimpTextLayer *layer = GIMP_TEXT_LAYER (object);
 
-  if (layer->text)
-    {
-      g_object_unref (layer->text);
-      layer->text = NULL;
-    }
+  g_clear_object (&layer->text);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -472,8 +468,7 @@ gimp_text_layer_set_text (GimpTextLayer *layer,
                                             G_CALLBACK (gimp_text_layer_text_changed),
                                             layer);
 
-      g_object_unref (layer->text);
-      layer->text = NULL;
+      g_clear_object (&layer->text);
     }
 
   if (text)
diff --git a/app/text/gimptextundo.c b/app/text/gimptextundo.c
index 5f03330..af934a5 100644
--- a/app/text/gimptextundo.c
+++ b/app/text/gimptextundo.c
@@ -292,11 +292,7 @@ gimp_text_undo_free (GimpUndo     *undo,
 {
   GimpTextUndo *text_undo = GIMP_TEXT_UNDO (undo);
 
-  if (text_undo->text)
-    {
-      g_object_unref (text_undo->text);
-      text_undo->text = NULL;
-    }
+  g_clear_object (&text_undo->text);
 
   if (text_undo->pspec)
     {
diff --git a/app/tools/gimpblendtool.c b/app/tools/gimpblendtool.c
index 2396e8a..c161e1a 100644
--- a/app/tools/gimpblendtool.c
+++ b/app/tools/gimpblendtool.c
@@ -634,9 +634,7 @@ gimp_blend_tool_halt (GimpBlendTool *blend_tool)
 
   if (blend_tool->graph)
     {
-      g_object_unref (blend_tool->graph);
-      blend_tool->graph = NULL;
-
+      g_clear_object (&blend_tool->graph);
       blend_tool->render_node    = NULL;
 #if 0
       blend_tool->subtract_node  = NULL;
@@ -645,11 +643,7 @@ gimp_blend_tool_halt (GimpBlendTool *blend_tool)
       blend_tool->dist_node      = NULL;
     }
 
-  if (blend_tool->dist_buffer)
-    {
-      g_object_unref (blend_tool->dist_buffer);
-      blend_tool->dist_buffer = NULL;
-    }
+  g_clear_object (&blend_tool->dist_buffer);
 
   if (blend_tool->filter)
     {
@@ -702,8 +696,7 @@ gimp_blend_tool_commit (GimpBlendTool *blend_tool)
 
       gimp_drawable_filter_commit (blend_tool->filter,
                                    GIMP_PROGRESS (tool), FALSE);
-      g_object_unref (blend_tool->filter);
-      blend_tool->filter = NULL;
+      g_clear_object (&blend_tool->filter);
 
       gimp_tool_control_pop_preserve (tool->control);
 
diff --git a/app/tools/gimpcagetool.c b/app/tools/gimpcagetool.c
index fe706af..033db15 100644
--- a/app/tools/gimpcagetool.c
+++ b/app/tools/gimpcagetool.c
@@ -920,32 +920,22 @@ gimp_cage_tool_start (GimpCageTool *ct,
   tool->display  = display;
   tool->drawable = drawable;
 
-  if (ct->config)
-    {
-      g_object_unref (ct->config);
-      ct->config = NULL;
-    }
+  g_clear_object (&ct->config);
 
-  if (ct->coef)
-    {
-      g_object_unref (ct->coef);
-      ct->dirty_coef = TRUE;
-      ct->coef = NULL;
-    }
+  g_clear_object (&ct->coef);
+  ct->dirty_coef = TRUE;
 
   if (ct->filter)
     {
       gimp_drawable_filter_abort (ct->filter);
-      g_object_unref (ct->filter);
-      ct->filter = NULL;
+      g_clear_object (&ct->filter);
     }
 
   if (ct->render_node)
     {
-      g_object_unref (ct->render_node);
-      ct->render_node = NULL;
-      ct->coef_node   = NULL;
-      ct->cage_node   = NULL;
+      g_clear_object (&ct->render_node);
+      ct->coef_node = NULL;
+      ct->cage_node = NULL;
     }
 
   ct->config          = g_object_new (GIMP_TYPE_CAGE_CONFIG, NULL);
@@ -967,33 +957,18 @@ gimp_cage_tool_halt (GimpCageTool *ct)
 {
   GimpTool *tool = GIMP_TOOL (ct);
 
-  if (ct->config)
-    {
-      g_object_unref (ct->config);
-      ct->config = NULL;
-    }
-
-  if (ct->coef)
-    {
-      g_object_unref (ct->coef);
-      ct->coef = NULL;
-    }
-
-  if (ct->render_node)
-    {
-      g_object_unref (ct->render_node);
-      ct->render_node = NULL;
-      ct->coef_node   = NULL;
-      ct->cage_node   = NULL;
-    }
+  g_clear_object (&ct->config);
+  g_clear_object (&ct->coef);
+  g_clear_object (&ct->render_node);
+  ct->coef_node = NULL;
+  ct->cage_node = NULL;
 
   if (ct->filter)
     {
       gimp_tool_control_push_preserve (tool->control, TRUE);
 
       gimp_drawable_filter_abort (ct->filter);
-      g_object_unref (ct->filter);
-      ct->filter = NULL;
+      g_clear_object (&ct->filter);
 
       gimp_tool_control_pop_preserve (tool->control);
 
@@ -1019,8 +994,7 @@ gimp_cage_tool_commit (GimpCageTool *ct)
       gimp_tool_control_push_preserve (tool->control, TRUE);
 
       gimp_drawable_filter_commit (ct->filter, GIMP_PROGRESS (tool), FALSE);
-      g_object_unref (ct->filter);
-      ct->filter = NULL;
+      g_clear_object (&ct->filter);
 
       gimp_tool_control_pop_preserve (tool->control);
 
@@ -1158,11 +1132,7 @@ gimp_cage_tool_compute_coef (GimpCageTool *ct)
   progress = gimp_progress_start (GIMP_PROGRESS (ct), FALSE,
                                   _("Computing Cage Coefficients"));
 
-  if (ct->coef)
-    {
-      g_object_unref (ct->coef);
-      ct->coef = NULL;
-    }
+  g_clear_object (&ct->coef);
 
   format = babl_format_n (babl_type ("float"),
                           gimp_cage_config_get_n_points (config) * 2);
diff --git a/app/tools/gimpcolorpickertool.c b/app/tools/gimpcolorpickertool.c
index d7790ff..ee15b66 100644
--- a/app/tools/gimpcolorpickertool.c
+++ b/app/tools/gimpcolorpickertool.c
@@ -154,15 +154,10 @@ gimp_color_picker_tool_dispose (GObject *object)
 {
   GimpColorPickerTool *picker_tool = GIMP_COLOR_PICKER_TOOL (object);
 
-  if (picker_tool->gui)
-    {
-      g_object_unref (picker_tool->gui);
-
-      picker_tool->gui          = NULL;
-      picker_tool->color_area   = NULL;
-      picker_tool->color_frame1 = NULL;
-      picker_tool->color_frame2 = NULL;
-    }
+  g_clear_object (&picker_tool->gui);
+  picker_tool->color_area   = NULL;
+  picker_tool->color_frame1 = NULL;
+  picker_tool->color_frame2 = NULL;
 
   G_OBJECT_CLASS (parent_class)->dispose (object);
 }
diff --git a/app/tools/gimpdrawtool.c b/app/tools/gimpdrawtool.c
index 5108c29..3ab5886 100644
--- a/app/tools/gimpdrawtool.c
+++ b/app/tools/gimpdrawtool.c
@@ -480,15 +480,13 @@ gimp_draw_tool_undraw (GimpDrawTool *draw_tool)
       if (draw_tool->preview)
         {
           gimp_display_shell_remove_preview_item (shell, draw_tool->preview);
-          g_object_unref (draw_tool->preview);
-          draw_tool->preview = NULL;
+          g_clear_object (&draw_tool->preview);
         }
 
       if (draw_tool->item)
         {
           gimp_display_shell_remove_tool_item (shell, draw_tool->item);
-          g_object_unref (draw_tool->item);
-          draw_tool->item = NULL;
+          g_clear_object (&draw_tool->item);
         }
     }
 }
diff --git a/app/tools/gimpfiltertool.c b/app/tools/gimpfiltertool.c
index 736aaae..9281326 100644
--- a/app/tools/gimpfiltertool.c
+++ b/app/tools/gimpfiltertool.c
@@ -234,43 +234,14 @@ gimp_filter_tool_finalize (GObject *object)
 {
   GimpFilterTool *filter_tool = GIMP_FILTER_TOOL (object);
 
-  if (filter_tool->operation)
-    {
-      g_object_unref (filter_tool->operation);
-      filter_tool->operation = NULL;
-    }
-
-  if (filter_tool->config)
-    {
-      g_object_unref (filter_tool->config);
-      filter_tool->config = NULL;
-    }
-
-  if (filter_tool->default_config)
-    {
-      g_object_unref (filter_tool->default_config);
-      filter_tool->default_config = NULL;
-    }
-
-  if (filter_tool->settings)
-    {
-      g_object_unref (filter_tool->settings);
-      filter_tool->settings = NULL;
-    }
-
-  if (filter_tool->description)
-    {
-      g_free (filter_tool->description);
-      filter_tool->description = NULL;
-    }
-
-  if (filter_tool->gui)
-    {
-      g_object_unref (filter_tool->gui);
-      filter_tool->gui          = NULL;
-      filter_tool->settings_box = NULL;
-      filter_tool->region_combo = NULL;
-    }
+  g_clear_object (&filter_tool->operation);
+  g_clear_object (&filter_tool->config);
+  g_clear_object (&filter_tool->default_config);
+  g_clear_object (&filter_tool->settings);
+  g_clear_pointer (&filter_tool->description, g_free);
+  g_clear_object (&filter_tool->gui);
+  filter_tool->settings_box = NULL;
+  filter_tool->region_combo = NULL;
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -942,50 +913,30 @@ gimp_filter_tool_halt (GimpFilterTool *filter_tool)
 {
   GimpTool *tool = GIMP_TOOL (filter_tool);
 
-  if (filter_tool->gui)
-    {
-      g_object_unref (filter_tool->gui);
-      filter_tool->gui          = NULL;
-      filter_tool->settings_box = NULL;
-      filter_tool->region_combo = NULL;
-    }
+  g_clear_object (&filter_tool->gui);
+  filter_tool->settings_box = NULL;
+  filter_tool->region_combo = NULL;
 
   if (filter_tool->filter)
     {
       gimp_drawable_filter_abort (filter_tool->filter);
-      g_object_unref (filter_tool->filter);
-      filter_tool->filter = NULL;
+      g_clear_object (&filter_tool->filter);
 
       gimp_filter_tool_remove_guide (filter_tool);
     }
 
-  if (filter_tool->operation)
-    {
-      g_object_unref (filter_tool->operation);
-      filter_tool->operation = NULL;
-    }
+  g_clear_object (&filter_tool->operation);
 
   if (filter_tool->config)
     {
       g_signal_handlers_disconnect_by_func (filter_tool->config,
                                             gimp_filter_tool_config_notify,
                                             filter_tool);
-
-      g_object_unref (filter_tool->config);
-      filter_tool->config = NULL;
+      g_clear_object (&filter_tool->config);
     }
 
-  if (filter_tool->default_config)
-    {
-      g_object_unref (filter_tool->default_config);
-      filter_tool->default_config = NULL;
-    }
-
-  if (filter_tool->settings)
-    {
-      g_object_unref (filter_tool->settings);
-      filter_tool->settings = NULL;
-    }
+  g_clear_object (&filter_tool->default_config);
+  g_clear_object (&filter_tool->settings);
 
   if (gimp_draw_tool_is_active (GIMP_DRAW_TOOL (tool)))
     gimp_draw_tool_stop (GIMP_DRAW_TOOL (tool));
@@ -1015,8 +966,7 @@ gimp_filter_tool_commit (GimpFilterTool *filter_tool)
 
       gimp_drawable_filter_commit (filter_tool->filter,
                                    GIMP_PROGRESS (tool), TRUE);
-      g_object_unref (filter_tool->filter);
-      filter_tool->filter = NULL;
+      g_clear_object (&filter_tool->filter);
 
       gimp_tool_control_pop_preserve (tool->control);
 
@@ -1238,8 +1188,7 @@ gimp_filter_tool_guide_removed (GimpGuide      *guide,
                                         gimp_filter_tool_guide_moved,
                                         filter_tool);
 
-  g_object_unref (filter_tool->preview_guide);
-  filter_tool->preview_guide = NULL;
+  g_clear_object (&filter_tool->preview_guide);
 
   g_object_set (options,
                 "preview-split", FALSE,
@@ -1373,43 +1322,22 @@ gimp_filter_tool_get_operation (GimpFilterTool *filter_tool)
   if (filter_tool->filter)
     {
       gimp_drawable_filter_abort (filter_tool->filter);
-      g_object_unref (filter_tool->filter);
-      filter_tool->filter = NULL;
+      g_clear_object (&filter_tool->filter);
     }
 
-  if (filter_tool->operation)
-    {
-      g_object_unref (filter_tool->operation);
-      filter_tool->operation = NULL;
-    }
+  g_clear_object (&filter_tool->operation);
 
   if (filter_tool->config)
     {
       g_signal_handlers_disconnect_by_func (filter_tool->config,
                                             gimp_filter_tool_config_notify,
                                             filter_tool);
-
-      g_object_unref (filter_tool->config);
-      filter_tool->config = NULL;
-    }
-
-  if (filter_tool->default_config)
-    {
-      g_object_unref (filter_tool->default_config);
-      filter_tool->default_config = NULL;
+      g_clear_object (&filter_tool->config);
     }
 
-  if (filter_tool->settings)
-    {
-      g_object_unref (filter_tool->settings);
-      filter_tool->settings = NULL;
-    }
-
-  if (filter_tool->description)
-    {
-      g_free (filter_tool->description);
-      filter_tool->description = NULL;
-    }
+  g_clear_object (&filter_tool->default_config);
+  g_clear_object (&filter_tool->settings);
+  g_clear_pointer (&filter_tool->description, g_free);
 
   operation_name = klass->get_operation (filter_tool,
                                          &filter_tool->description);
diff --git a/app/tools/gimpforegroundselecttool.c b/app/tools/gimpforegroundselecttool.c
index 2f036e5..60cfaf2 100644
--- a/app/tools/gimpforegroundselecttool.c
+++ b/app/tools/gimpforegroundselecttool.c
@@ -254,12 +254,8 @@ gimp_foreground_select_tool_finalize (GObject *object)
 {
   GimpForegroundSelectTool *fg_select = GIMP_FOREGROUND_SELECT_TOOL (object);
 
-  if (fg_select->gui)
-    {
-      g_object_unref (fg_select->gui);
-      fg_select->gui            = NULL;
-      fg_select->preview_toggle = NULL;
-    }
+  g_clear_object (&fg_select->gui);
+  fg_select->preview_toggle = NULL;
 
   if (fg_select->stroke)
     g_warning ("%s: stroke should be NULL at this point", G_STRLOC);
@@ -912,17 +908,8 @@ gimp_foreground_select_tool_halt (GimpForegroundSelectTool *fg_select)
 {
   GimpTool *tool = GIMP_TOOL (fg_select);
 
-  if (fg_select->trimap)
-    {
-      g_object_unref (fg_select->trimap);
-      fg_select->trimap = NULL;
-    }
-
-  if (fg_select->mask)
-    {
-      g_object_unref (fg_select->mask);
-      fg_select->mask = NULL;
-    }
+  g_clear_object (&fg_select->trimap);
+  g_clear_object (&fg_select->mask);
 
   if (fg_select->undo_stack)
     {
@@ -1054,11 +1041,7 @@ gimp_foreground_select_tool_preview (GimpForegroundSelectTool *fg_select)
 
   options  = GIMP_FOREGROUND_SELECT_TOOL_GET_OPTIONS (tool);
 
-  if (fg_select->mask)
-    {
-      g_object_unref (fg_select->mask);
-      fg_select->mask = NULL;
-    }
+  g_clear_object (&fg_select->mask);
 
   fg_select->mask = gimp_drawable_foreground_extract (drawable,
                                                       options->engine,
@@ -1307,11 +1290,7 @@ gimp_foreground_select_undo_pop (StrokeUndo *undo,
 static void
 gimp_foreground_select_undo_free (StrokeUndo *undo)
 {
-  if (undo->saved_trimap)
-    {
-      g_object_unref (undo->saved_trimap);
-      undo->saved_trimap = NULL;
-    }
+  g_clear_object (&undo->saved_trimap);
 
   g_slice_free (StrokeUndo, undo);
 }
diff --git a/app/tools/gimpiscissorstool.c b/app/tools/gimpiscissorstool.c
index a4df02f..a8d9a6c 100644
--- a/app/tools/gimpiscissorstool.c
+++ b/app/tools/gimpiscissorstool.c
@@ -1242,17 +1242,8 @@ gimp_iscissors_tool_halt (GimpIscissorsTool *iscissors,
       iscissors->redo_stack = NULL;
     }
 
-  if (iscissors->gradient_map)
-    {
-      g_object_unref (iscissors->gradient_map);
-      iscissors->gradient_map = NULL;
-    }
-
-  if (iscissors->mask)
-    {
-      g_object_unref (iscissors->mask);
-      iscissors->mask = NULL;
-    }
+  g_clear_object (&iscissors->gradient_map);
+  g_clear_object (&iscissors->mask);
 }
 
 static void
diff --git a/app/tools/gimplevelstool.c b/app/tools/gimplevelstool.c
index 906240c..4aa04c7 100644
--- a/app/tools/gimplevelstool.c
+++ b/app/tools/gimplevelstool.c
@@ -168,11 +168,7 @@ gimp_levels_tool_finalize (GObject *object)
 {
   GimpLevelsTool *tool = GIMP_LEVELS_TOOL (object);
 
-  if (tool->histogram)
-    {
-      g_object_unref (tool->histogram);
-      tool->histogram = NULL;
-    }
+  g_clear_object (&tool->histogram);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/tools/gimpnpointdeformationtool.c b/app/tools/gimpnpointdeformationtool.c
index 2711189..ec51033 100644
--- a/app/tools/gimpnpointdeformationtool.c
+++ b/app/tools/gimpnpointdeformationtool.c
@@ -367,26 +367,13 @@ gimp_n_point_deformation_tool_halt (GimpNPointDeformationTool *npd_tool)
 
   gimp_n_point_deformation_tool_clear_selected_points_list (npd_tool);
 
-  if (npd_tool->graph)
-    {
-      g_object_unref (npd_tool->graph);
-      npd_tool->graph = NULL;
-      npd_tool->source = NULL;
-      npd_tool->npd_node = NULL;
-      npd_tool->sink = NULL;
-    }
-
-  if (npd_tool->preview_buffer)
-    {
-      g_object_unref (npd_tool->preview_buffer);
-      npd_tool->preview_buffer = NULL;
-    }
+  g_clear_object (&npd_tool->graph);
+  npd_tool->source   = NULL;
+  npd_tool->npd_node = NULL;
+  npd_tool->sink     = NULL;
 
-  if (npd_tool->lattice_points)
-    {
-      g_free (npd_tool->lattice_points);
-      npd_tool->lattice_points = NULL;
-    }
+  g_clear_object (&npd_tool->preview_buffer);
+  g_clear_pointer (&npd_tool->lattice_points, g_free);
 
   tool->display  = NULL;
   tool->drawable = NULL;
diff --git a/app/tools/gimpoperationtool.c b/app/tools/gimpoperationtool.c
index 574cb06..d11ec22 100644
--- a/app/tools/gimpoperationtool.c
+++ b/app/tools/gimpoperationtool.c
@@ -166,17 +166,8 @@ gimp_operation_tool_finalize (GObject *object)
 {
   GimpOperationTool *op_tool = GIMP_OPERATION_TOOL (object);
 
-  if (op_tool->operation)
-    {
-      g_free (op_tool->operation);
-      op_tool->operation = NULL;
-    }
-
-  if (op_tool->description)
-    {
-      g_free (op_tool->description);
-      op_tool->description = NULL;
-    }
+  g_clear_pointer (&op_tool->operation,   g_free);
+  g_clear_pointer (&op_tool->description, g_free);
 
   g_list_free_full (op_tool->aux_inputs,
                     (GDestroyNotify) gimp_operation_tool_aux_input_free);
@@ -697,11 +688,8 @@ gimp_operation_tool_set_operation (GimpOperationTool *op_tool,
   tool        = GIMP_TOOL (op_tool);
   filter_tool = GIMP_FILTER_TOOL (op_tool);
 
-  if (op_tool->operation)
-    g_free (op_tool->operation);
-
-  if (op_tool->description)
-    g_free (op_tool->description);
+  g_free (op_tool->operation);
+  g_free (op_tool->description);
 
   op_tool->operation   = g_strdup (operation);
   op_tool->description = g_strdup (description);
diff --git a/app/tools/gimprectangleoptions.c b/app/tools/gimprectangleoptions.c
index 492980a..84ba632 100644
--- a/app/tools/gimprectangleoptions.c
+++ b/app/tools/gimprectangleoptions.c
@@ -328,8 +328,8 @@ gimp_rectangle_options_iface_base_init (GimpRectangleOptionsInterface *iface)
 static void
 gimp_rectangle_options_private_finalize (GimpRectangleOptionsPrivate *private)
 {
-  g_object_unref (private->aspect_history);
-  g_object_unref (private->size_history);
+  g_clear_object (&private->aspect_history);
+  g_clear_object (&private->size_history);
 
   g_slice_free (GimpRectangleOptionsPrivate, private);
 }
diff --git a/app/tools/gimpregionselecttool.c b/app/tools/gimpregionselecttool.c
index 4bc4a1a..f1604a8 100644
--- a/app/tools/gimpregionselecttool.c
+++ b/app/tools/gimpregionselecttool.c
@@ -118,18 +118,10 @@ gimp_region_select_tool_finalize (GObject *object)
 {
   GimpRegionSelectTool *region_sel = GIMP_REGION_SELECT_TOOL (object);
 
-  if (region_sel->region_mask)
-    {
-      g_object_unref (region_sel->region_mask);
-      region_sel->region_mask = NULL;
-    }
+  g_clear_object (&region_sel->region_mask);
 
-  if (region_sel->segs)
-    {
-      g_free (region_sel->segs);
-      region_sel->segs   = NULL;
-      region_sel->n_segs = 0;
-    }
+  g_clear_pointer (&region_sel->segs, g_free);
+  region_sel->n_segs = 0;
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -233,18 +225,10 @@ gimp_region_select_tool_button_release (GimpTool              *tool,
         }
     }
 
-  if (region_sel->region_mask)
-    {
-      g_object_unref (region_sel->region_mask);
-      region_sel->region_mask = NULL;
-    }
+  g_clear_object (&region_sel->region_mask);
 
-  if (region_sel->segs)
-    {
-      g_free (region_sel->segs);
-      region_sel->segs   = NULL;
-      region_sel->n_segs = 0;
-    }
+  g_clear_pointer (&region_sel->segs, g_free);
+  region_sel->n_segs = 0;
 
   /*  Restore the original threshold  */
   g_object_set (options,
@@ -362,12 +346,8 @@ gimp_region_select_tool_get_mask (GimpRegionSelectTool *region_sel,
 
   gimp_display_shell_set_override_cursor (shell, (GimpCursorType) GDK_WATCH);
 
-  if (region_sel->segs)
-    {
-      g_free (region_sel->segs);
-      region_sel->segs   = NULL;
-      region_sel->n_segs = 0;
-    }
+  g_clear_pointer (&region_sel->segs, g_free);
+  region_sel->n_segs = 0;
 
   if (region_sel->region_mask)
     g_object_unref (region_sel->region_mask);
diff --git a/app/tools/gimpseamlessclonetool.c b/app/tools/gimpseamlessclonetool.c
index e0562fd..10755d8 100644
--- a/app/tools/gimpseamlessclonetool.c
+++ b/app/tools/gimpseamlessclonetool.c
@@ -344,26 +344,16 @@ gimp_seamless_clone_tool_stop (GimpSeamlessCloneTool *sc,
     {
       sc->tool_state = SC_STATE_INIT;
 
-      if (sc->paste)
-        {
-          g_object_unref (sc->paste);
-          sc->paste = NULL;
-        }
-
-      if (sc->render_node)
-        {
-          g_object_unref (sc->render_node);
-          sc->render_node = NULL;
-          sc->sc_node     = NULL;
-        }
+      g_clear_object (&sc->paste);
+      g_clear_object (&sc->render_node);
+      sc->sc_node = NULL;
     }
 
   /* This should always happen, even when we just switch a display */
   if (sc->filter)
     {
       gimp_drawable_filter_abort (sc->filter);
-      g_object_unref (sc->filter);
-      sc->filter = NULL;
+      g_clear_object (&sc->filter);
 
       if (GIMP_TOOL (sc)->display)
         gimp_image_flush (gimp_display_get_image (GIMP_TOOL (sc)->display));
@@ -382,8 +372,7 @@ gimp_seamless_clone_tool_commit (GimpSeamlessCloneTool *sc)
       gimp_tool_control_push_preserve (tool->control, TRUE);
 
       gimp_drawable_filter_commit (sc->filter, GIMP_PROGRESS (tool), FALSE);
-      g_object_unref (sc->filter);
-      sc->filter = NULL;
+      g_clear_object (&sc->filter);
 
       gimp_tool_control_pop_preserve (tool->control);
 
@@ -532,8 +521,7 @@ gimp_seamless_clone_tool_key_press (GimpTool    *tool,
            *       invalidating and re-rendering all now (expensive and
            *       perhaps useless */
           gimp_drawable_filter_commit (sct->filter, GIMP_PROGRESS (tool), FALSE);
-          g_object_unref (sct->filter);
-          sct->filter = NULL;
+          g_clear_object (&sct->filter);
 
           gimp_tool_control_set_preserve (tool->control, FALSE);
 
diff --git a/app/tools/gimptexttool.c b/app/tools/gimptexttool.c
index f9a118e..6eb7001 100644
--- a/app/tools/gimptexttool.c
+++ b/app/tools/gimptexttool.c
@@ -279,17 +279,8 @@ gimp_text_tool_finalize (GObject *object)
 {
   GimpTextTool *text_tool = GIMP_TEXT_TOOL (object);
 
-  if (text_tool->proxy)
-    {
-      g_object_unref (text_tool->proxy);
-      text_tool->proxy = NULL;
-    }
-
-  if (text_tool->buffer)
-    {
-      g_object_unref (text_tool->buffer);
-      text_tool->buffer = NULL;
-    }
+  g_clear_object (&text_tool->proxy);
+  g_clear_object (&text_tool->buffer);
 
   gimp_text_tool_editor_finalize (text_tool);
 
@@ -1133,8 +1124,7 @@ gimp_text_tool_connect (GimpTextTool  *text_tool,
           if (text_tool->pending)
             gimp_text_tool_apply (text_tool, TRUE);
 
-          g_object_unref (text_tool->text);
-          text_tool->text = NULL;
+          g_clear_object (&text_tool->text);
 
           g_object_set (text_tool->proxy,
                         "text",   NULL,
@@ -1804,11 +1794,7 @@ gimp_text_tool_buffer_end_edit (GimpTextBuffer *buffer,
 void
 gimp_text_tool_clear_layout (GimpTextTool *text_tool)
 {
-  if (text_tool->layout)
-    {
-      g_object_unref (text_tool->layout);
-      text_tool->layout = NULL;
-    }
+  g_clear_object (&text_tool->layout);
 }
 
 gboolean
diff --git a/app/tools/gimptool.c b/app/tools/gimptool.c
index b61ecec..11080e0 100644
--- a/app/tools/gimptool.c
+++ b/app/tools/gimptool.c
@@ -228,41 +228,12 @@ gimp_tool_finalize (GObject *object)
 {
   GimpTool *tool = GIMP_TOOL (object);
 
-  if (tool->tool_info)
-    {
-      g_object_unref (tool->tool_info);
-      tool->tool_info = NULL;
-    }
-
-  if (tool->label)
-    {
-      g_free (tool->label);
-      tool->label = NULL;
-    }
-
-  if (tool->undo_desc)
-    {
-      g_free (tool->undo_desc);
-      tool->undo_desc = NULL;
-    }
-
-  if (tool->icon_name)
-    {
-      g_free (tool->icon_name);
-      tool->icon_name = NULL;
-    }
-
-  if (tool->help_id)
-    {
-      g_free (tool->help_id);
-      tool->help_id = NULL;
-    }
-
-  if (tool->control)
-    {
-      g_object_unref (tool->control);
-      tool->control = NULL;
-    }
+  g_clear_object (&tool->tool_info);
+  g_clear_object (&tool->control);
+  g_clear_pointer (&tool->label,     g_free);
+  g_clear_pointer (&tool->undo_desc, g_free);
+  g_clear_pointer (&tool->icon_name, g_free);
+  g_clear_pointer (&tool->help_id,   g_free);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/tools/gimptransformtool.c b/app/tools/gimptransformtool.c
index f8bc991..e914e5c 100644
--- a/app/tools/gimptransformtool.c
+++ b/app/tools/gimptransformtool.c
@@ -228,17 +228,8 @@ gimp_transform_tool_finalize (GObject *object)
 {
   GimpTransformTool *tr_tool = GIMP_TRANSFORM_TOOL (object);
 
-  if (tr_tool->gui)
-    {
-      g_object_unref (tr_tool->gui);
-      tr_tool->gui = NULL;
-     }
-
-  if (tr_tool->strokes)
-    {
-      g_ptr_array_unref (tr_tool->strokes);
-      tr_tool->strokes = NULL;
-    }
+  g_clear_object (&tr_tool->gui);
+  g_clear_pointer (&tr_tool->strokes, g_ptr_array_unref);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/tools/gimpvectortool.c b/app/tools/gimpvectortool.c
index 9f8a46b..458d7ac 100644
--- a/app/tools/gimpvectortool.c
+++ b/app/tools/gimpvectortool.c
@@ -567,8 +567,7 @@ gimp_vector_tool_set_vectors (GimpVectorTool *vector_tool,
                                             gimp_vector_tool_vectors_removed,
                                             vector_tool);
 
-      g_object_unref (vector_tool->vectors);
-      vector_tool->vectors = NULL;
+      g_clear_object (&vector_tool->vectors);
 
       if (options->to_selection_button)
         {
diff --git a/app/tools/gimpwarptool.c b/app/tools/gimpwarptool.c
index 40765dc..ef31e30 100644
--- a/app/tools/gimpwarptool.c
+++ b/app/tools/gimpwarptool.c
@@ -305,8 +305,7 @@ gimp_warp_tool_button_release (GimpTool              *tool,
   g_printerr ("%s\n", gegl_path_to_string (wt->current_stroke));
 #endif
 
-  g_object_unref (wt->current_stroke);
-  wt->current_stroke = NULL;
+  g_clear_object (&wt->current_stroke);
 
   if (release_type == GIMP_BUTTON_RELEASE_CANCEL)
     {
@@ -712,24 +711,15 @@ gimp_warp_tool_halt (GimpWarpTool *wt)
   GimpTool        *tool    = GIMP_TOOL (wt);
   GimpWarpOptions *options = GIMP_WARP_TOOL_GET_OPTIONS (wt);
 
-  if (wt->coords_buffer)
-    {
-      g_object_unref (wt->coords_buffer);
-      wt->coords_buffer = NULL;
-    }
+  g_clear_object (&wt->coords_buffer);
 
-  if (wt->graph)
-    {
-      g_object_unref (wt->graph);
-      wt->graph       = NULL;
-      wt->render_node = NULL;
-    }
+  g_clear_object (&wt->graph);
+  wt->render_node = NULL;
 
   if (wt->filter)
     {
       gimp_drawable_filter_abort (wt->filter);
-      g_object_unref (wt->filter);
-      wt->filter = NULL;
+      g_clear_object (&wt->filter);
 
       gimp_image_flush (gimp_display_get_image (tool->display));
     }
@@ -768,8 +758,7 @@ gimp_warp_tool_commit (GimpWarpTool *wt)
       gimp_warp_tool_set_sampler (wt, /* commit = */ TRUE);
 
       gimp_drawable_filter_commit (wt->filter, GIMP_PROGRESS (tool), FALSE);
-      g_object_unref (wt->filter);
-      wt->filter = NULL;
+      g_clear_object (&wt->filter);
 
       gimp_tool_control_pop_preserve (tool->control);
 
@@ -1092,8 +1081,7 @@ gimp_warp_tool_animate (GimpWarpTool *wt)
   if (wt->filter)
     {
       gimp_drawable_filter_abort (wt->filter);
-      g_object_unref (wt->filter);
-      wt->filter = NULL;
+      g_clear_object (&wt->filter);
     }
 
   gimp_warp_tool_set_sampler (wt, /* commit = */ TRUE);
diff --git a/app/tools/tool_manager.c b/app/tools/tool_manager.c
index e058433..74309f0 100644
--- a/app/tools/tool_manager.c
+++ b/app/tools/tool_manager.c
@@ -151,11 +151,8 @@ tool_manager_exit (Gimp *gimp)
   gimp_container_remove_handler (gimp->images,
                                  tool_manager->image_dirty_handler_id);
 
-  if (tool_manager->active_tool)
-    g_object_unref (tool_manager->active_tool);
-
-  if (tool_manager->shared_paint_options)
-    g_object_unref (tool_manager->shared_paint_options);
+  g_clear_object (&tool_manager->active_tool);
+  g_clear_object (&tool_manager->shared_paint_options);
 
   g_slice_free (GimpToolManager, tool_manager);
 }
diff --git a/app/vectors/gimpvectorsmodundo.c b/app/vectors/gimpvectorsmodundo.c
index 1ae4730..86b8b6d 100644
--- a/app/vectors/gimpvectorsmodundo.c
+++ b/app/vectors/gimpvectorsmodundo.c
@@ -135,11 +135,7 @@ gimp_vectors_mod_undo_free (GimpUndo     *undo,
 {
   GimpVectorsModUndo *vectors_mod_undo = GIMP_VECTORS_MOD_UNDO (undo);
 
-  if (vectors_mod_undo->vectors)
-    {
-      g_object_unref (vectors_mod_undo->vectors);
-      vectors_mod_undo->vectors = NULL;
-    }
+  g_clear_object (&vectors_mod_undo->vectors);
 
   GIMP_UNDO_CLASS (parent_class)->free (undo, undo_mode);
 }
diff --git a/app/widgets/gimpaction.c b/app/widgets/gimpaction.c
index 54e2334..d02b8ec 100644
--- a/app/widgets/gimpaction.c
+++ b/app/widgets/gimpaction.c
@@ -159,23 +159,9 @@ gimp_action_finalize (GObject *object)
 {
   GimpAction *action = GIMP_ACTION (object);
 
-  if (action->context)
-    {
-      g_object_unref (action->context);
-      action->context = NULL;
-    }
-
-  if (action->color)
-    {
-      g_free (action->color);
-      action->color = NULL;
-    }
-
-  if (action->viewable)
-    {
-      g_object_unref (action->viewable);
-      action->viewable = NULL;
-    }
+  g_clear_object (&action->context);
+  g_clear_pointer (&action->color, g_free);
+  g_clear_object (&action->viewable);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/widgets/gimpactionview.c b/app/widgets/gimpactionview.c
index b2f3312..413c8f9 100644
--- a/app/widgets/gimpactionview.c
+++ b/app/widgets/gimpactionview.c
@@ -106,8 +106,7 @@ gimp_action_view_dispose (GObject *object)
                                                 view);
         }
 
-      g_object_unref (view->manager);
-      view->manager = NULL;
+      g_clear_object (&view->manager);
     }
 
   G_OBJECT_CLASS (parent_class)->dispose (object);
@@ -118,11 +117,7 @@ gimp_action_view_finalize (GObject *object)
 {
   GimpActionView *view = GIMP_ACTION_VIEW (object);
 
-  if (view->filter)
-    {
-      g_free (view->filter);
-      view->filter = NULL;
-    }
+  g_clear_pointer (&view->filter, g_free);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/widgets/gimpbuffersourcebox.c b/app/widgets/gimpbuffersourcebox.c
index b572719..2f9f483 100644
--- a/app/widgets/gimpbuffersourcebox.c
+++ b/app/widgets/gimpbuffersourcebox.c
@@ -186,23 +186,9 @@ gimp_buffer_source_box_finalize (GObject *object)
 {
   GimpBufferSourceBox *box = GIMP_BUFFER_SOURCE_BOX (object);
 
-  if (box->priv->context)
-    {
-      g_object_unref (box->priv->context);
-      box->priv->context = NULL;
-    }
-
-  if (box->priv->source_node)
-    {
-      g_object_unref (box->priv->source_node);
-      box->priv->source_node = NULL;
-    }
-
-  if (box->priv->name)
-    {
-      g_free (box->priv->name);
-      box->priv->name = NULL;
-    }
+  g_clear_object (&box->priv->context);
+  g_clear_object (&box->priv->source_node);
+  g_clear_pointer (&box->priv->name, g_free);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/widgets/gimpcellrendererviewable.c b/app/widgets/gimpcellrendererviewable.c
index 2115f08..6ce28b9 100644
--- a/app/widgets/gimpcellrendererviewable.c
+++ b/app/widgets/gimpcellrendererviewable.c
@@ -167,11 +167,7 @@ gimp_cell_renderer_viewable_finalize (GObject *object)
 {
   GimpCellRendererViewable *cell = GIMP_CELL_RENDERER_VIEWABLE (object);
 
-  if (cell->renderer)
-    {
-      g_object_unref (cell->renderer);
-      cell->renderer = NULL;
-    }
+  g_clear_object (&cell->renderer);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/widgets/gimpcircle.c b/app/widgets/gimpcircle.c
index cb0943a..1dcb245 100644
--- a/app/widgets/gimpcircle.c
+++ b/app/widgets/gimpcircle.c
@@ -175,11 +175,7 @@ gimp_circle_dispose (GObject *object)
 {
   GimpCircle *circle = GIMP_CIRCLE (object);
 
-  if (circle->priv->surface)
-    {
-      cairo_surface_destroy (circle->priv->surface);
-      circle->priv->surface = NULL;
-    }
+  g_clear_pointer (&circle->priv->surface, cairo_surface_destroy);
 
   G_OBJECT_CLASS (parent_class)->dispose (object);
 }
@@ -206,11 +202,7 @@ gimp_circle_set_property (GObject      *object,
 
     case PROP_BACKGROUND:
       circle->priv->background = g_value_get_enum (value);
-      if (circle->priv->surface)
-        {
-          cairo_surface_destroy (circle->priv->surface);
-          circle->priv->surface = NULL;
-        }
+      g_clear_pointer (&circle->priv->surface, cairo_surface_destroy);
       gtk_widget_queue_draw (GTK_WIDGET (circle));
       break;
 
@@ -343,11 +335,7 @@ gimp_circle_size_allocate (GtkWidget     *widget,
                             allocation->width,
                             allocation->height);
 
-  if (circle->priv->surface)
-    {
-      cairo_surface_destroy (circle->priv->surface);
-      circle->priv->surface = NULL;
-    }
+  g_clear_pointer (&circle->priv->surface, cairo_surface_destroy);
 }
 
 static gboolean
diff --git a/app/widgets/gimpclipboard.c b/app/widgets/gimpclipboard.c
index 1383e78..e56afc2 100644
--- a/app/widgets/gimpclipboard.c
+++ b/app/widgets/gimpclipboard.c
@@ -844,29 +844,10 @@ gimp_clipboard_get (Gimp *gimp)
 static void
 gimp_clipboard_clear (GimpClipboard *gimp_clip)
 {
-  if (gimp_clip->image)
-    {
-      g_object_unref (gimp_clip->image);
-      gimp_clip->image = NULL;
-    }
-
-  if (gimp_clip->buffer)
-    {
-      g_object_unref (gimp_clip->buffer);
-      gimp_clip->buffer = NULL;
-    }
-
-  if (gimp_clip->svg)
-    {
-      g_free (gimp_clip->svg);
-      gimp_clip->svg = NULL;
-    }
-
-  if (gimp_clip->curve)
-    {
-      g_object_unref (gimp_clip->curve);
-      gimp_clip->curve = NULL;
-    }
+  g_clear_object (&gimp_clip->image);
+  g_clear_object (&gimp_clip->buffer);
+  g_clear_pointer (&gimp_clip->svg, g_free);
+  g_clear_object (&gimp_clip->curve);
 }
 
 static void
diff --git a/app/widgets/gimpcolordisplayeditor.c b/app/widgets/gimpcolordisplayeditor.c
index 679c052..8cb855c 100644
--- a/app/widgets/gimpcolordisplayeditor.c
+++ b/app/widgets/gimpcolordisplayeditor.c
@@ -347,23 +347,9 @@ gimp_color_display_editor_dispose (GObject *object)
       editor->selected = NULL;
     }
 
-  if (editor->stack)
-    {
-      g_object_unref (editor->stack);
-      editor->stack = NULL;
-    }
-
-  if (editor->config)
-    {
-      g_object_unref (editor->config);
-      editor->config = NULL;
-    }
-
-  if (editor->managed)
-    {
-      g_object_unref (editor->managed);
-      editor->managed = NULL;
-    }
+  g_clear_object (&editor->stack);
+  g_clear_object (&editor->config);
+  g_clear_object (&editor->managed);
 
   G_OBJECT_CLASS (parent_class)->dispose (object);
 }
diff --git a/app/widgets/gimpcolorframe.c b/app/widgets/gimpcolorframe.c
index 05cc56c..11886df 100644
--- a/app/widgets/gimpcolorframe.c
+++ b/app/widgets/gimpcolorframe.c
@@ -215,11 +215,7 @@ gimp_color_frame_finalize (GObject *object)
 {
   GimpColorFrame *frame = GIMP_COLOR_FRAME (object);
 
-  if (frame->number_layout)
-    {
-      g_object_unref (frame->number_layout);
-      frame->number_layout = NULL;
-    }
+  g_clear_object (&frame->number_layout);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -304,11 +300,7 @@ gimp_color_frame_style_set (GtkWidget *widget,
 
   GTK_WIDGET_CLASS (parent_class)->style_set (widget, prev_style);
 
-  if (frame->number_layout)
-    {
-      g_object_unref (frame->number_layout);
-      frame->number_layout = NULL;
-    }
+  g_clear_object (&frame->number_layout);
 }
 
 static gboolean
@@ -952,11 +944,7 @@ gimp_color_frame_create_transform (GimpColorFrame *frame)
 static void
 gimp_color_frame_destroy_transform (GimpColorFrame *frame)
 {
-  if (frame->transform)
-    {
-      g_object_unref (frame->transform);
-      frame->transform = NULL;
-    }
+  g_clear_object (&frame->transform);
 
   gimp_color_frame_update (frame);
 }
diff --git a/app/widgets/gimpcolormapeditor.c b/app/widgets/gimpcolormapeditor.c
index 4c56076..c1aa0f6 100644
--- a/app/widgets/gimpcolormapeditor.c
+++ b/app/widgets/gimpcolormapeditor.c
@@ -272,11 +272,7 @@ gimp_colormap_editor_finalize (GObject *object)
 {
   GimpColormapEditor *editor = GIMP_COLORMAP_EDITOR (object);
 
-  if (editor->layout)
-    {
-      g_object_unref (editor->layout);
-      editor->layout = NULL;
-    }
+  g_clear_object (&editor->layout);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/widgets/gimpcolorselectorpalette.c b/app/widgets/gimpcolorselectorpalette.c
index 3d02c62..b3c4ad6 100644
--- a/app/widgets/gimpcolorselectorpalette.c
+++ b/app/widgets/gimpcolorselectorpalette.c
@@ -127,8 +127,7 @@ gimp_color_selector_palette_set_config (GimpColorSelector *selector,
       gimp_view_renderer_set_context (GIMP_VIEW (select->view)->renderer,
                                       NULL);
 
-      g_object_unref (select->context);
-      select->context = NULL;
+      g_clear_object (&select->context);
     }
 
   if (config)
diff --git a/app/widgets/gimpcombotagentry.c b/app/widgets/gimpcombotagentry.c
index e254822..3fff361 100644
--- a/app/widgets/gimpcombotagentry.c
+++ b/app/widgets/gimpcombotagentry.c
@@ -119,11 +119,7 @@ gimp_combo_tag_entry_dispose (GObject *object)
 {
   GimpComboTagEntry *combo_entry = GIMP_COMBO_TAG_ENTRY (object);
 
-  if (combo_entry->arrow_pixbuf)
-    {
-      g_object_unref (combo_entry->arrow_pixbuf);
-      combo_entry->arrow_pixbuf = NULL;
-    }
+  g_clear_object (&combo_entry->arrow_pixbuf);
 
   if (combo_entry->normal_item_attr)
     {
@@ -233,11 +229,7 @@ gimp_combo_tag_entry_style_set (GtkWidget *widget,
 
   entry->selected_item_color = style->base[GTK_STATE_SELECTED];
 
-  if (entry->arrow_pixbuf)
-    {
-      g_object_unref (entry->arrow_pixbuf);
-      entry->arrow_pixbuf = NULL;
-    }
+  g_clear_object (&entry->arrow_pixbuf);
 }
 
 /**
diff --git a/app/widgets/gimpcontainereditor.c b/app/widgets/gimpcontainereditor.c
index f3a4b87..fabe538 100644
--- a/app/widgets/gimpcontainereditor.c
+++ b/app/widgets/gimpcontainereditor.c
@@ -303,35 +303,12 @@ gimp_container_editor_dispose (GObject *object)
 {
   GimpContainerEditor *editor = GIMP_CONTAINER_EDITOR (object);
 
-  if (editor->priv->container)
-    {
-      g_object_unref (editor->priv->container);
-      editor->priv->container = NULL;
-    }
+  g_clear_object (&editor->priv->container);
+  g_clear_object (&editor->priv->context);
+  g_clear_object (&editor->priv->menu_factory);
 
-  if (editor->priv->context)
-    {
-      g_object_unref (editor->priv->context);
-      editor->priv->context = NULL;
-    }
-
-  if (editor->priv->menu_factory)
-    {
-      g_object_unref (editor->priv->menu_factory);
-      editor->priv->menu_factory = NULL;
-    }
-
-  if (editor->priv->menu_identifier)
-    {
-      g_free (editor->priv->menu_identifier);
-      editor->priv->menu_identifier = NULL;
-    }
-
-  if (editor->priv->ui_path)
-    {
-      g_free (editor->priv->ui_path);
-      editor->priv->ui_path = NULL;
-    }
+  g_clear_pointer (&editor->priv->menu_identifier, g_free);
+  g_clear_pointer (&editor->priv->ui_path,         g_free);
 
   G_OBJECT_CLASS (parent_class)->dispose (object);
 }
diff --git a/app/widgets/gimpcontainerpopup.c b/app/widgets/gimpcontainerpopup.c
index f8571d7..fd48ede 100644
--- a/app/widgets/gimpcontainerpopup.c
+++ b/app/widgets/gimpcontainerpopup.c
@@ -97,29 +97,11 @@ gimp_container_popup_finalize (GObject *object)
 {
   GimpContainerPopup *popup = GIMP_CONTAINER_POPUP (object);
 
-  if (popup->context)
-    {
-      g_object_unref (popup->context);
-      popup->context = NULL;
-    }
-
-  if (popup->dialog_identifier)
-    {
-      g_free (popup->dialog_identifier);
-      popup->dialog_identifier = NULL;
-    }
+  g_clear_object (&popup->context);
 
-  if (popup->dialog_icon_name)
-    {
-      g_free (popup->dialog_icon_name);
-      popup->dialog_icon_name = NULL;
-    }
-
-  if (popup->dialog_tooltip)
-    {
-      g_free (popup->dialog_tooltip);
-      popup->dialog_tooltip = NULL;
-    }
+  g_clear_pointer (&popup->dialog_identifier, g_free);
+  g_clear_pointer (&popup->dialog_icon_name,  g_free);
+  g_clear_pointer (&popup->dialog_tooltip,    g_free);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/widgets/gimpcontainertreeview.c b/app/widgets/gimpcontainertreeview.c
index b679de3..12c3667 100644
--- a/app/widgets/gimpcontainertreeview.c
+++ b/app/widgets/gimpcontainertreeview.c
@@ -335,11 +335,7 @@ gimp_container_tree_view_finalize (GObject *object)
 {
   GimpContainerTreeView *tree_view = GIMP_CONTAINER_TREE_VIEW (object);
 
-  if (tree_view->model)
-    {
-      g_object_unref (tree_view->model);
-      tree_view->model = NULL;
-    }
+  g_clear_object (&tree_view->model);
 
   if (tree_view->priv->toggle_cells)
     {
diff --git a/app/widgets/gimpcontrollereditor.c b/app/widgets/gimpcontrollereditor.c
index 1a0b57c..b4e804f 100644
--- a/app/widgets/gimpcontrollereditor.c
+++ b/app/widgets/gimpcontrollereditor.c
@@ -400,15 +400,10 @@ gimp_controller_editor_finalize (GObject *object)
     {
       gimp_controller_info_set_event_snooper (editor->info, NULL, NULL);
 
-      g_object_unref (editor->info);
-      editor->info = NULL;
+      g_clear_object (&editor->info);
     }
 
-  if (editor->context)
-    {
-      g_object_unref (editor->context);
-      editor->context = NULL;
-    }
+  g_clear_object (&editor->context);
 
   if (editor->edit_dialog)
     gtk_widget_destroy (editor->edit_dialog);
diff --git a/app/widgets/gimpcontrollerinfo.c b/app/widgets/gimpcontrollerinfo.c
index c3b30c1..d799be0 100644
--- a/app/widgets/gimpcontrollerinfo.c
+++ b/app/widgets/gimpcontrollerinfo.c
@@ -170,17 +170,8 @@ gimp_controller_info_finalize (GObject *object)
 {
   GimpControllerInfo *info = GIMP_CONTROLLER_INFO (object);
 
-  if (info->controller)
-    {
-      g_object_unref (info->controller);
-      info->controller = NULL;
-    }
-
-  if (info->mapping)
-    {
-      g_hash_table_unref (info->mapping);
-      info->mapping = NULL;
-    }
+  g_clear_object (&info->controller);
+  g_clear_pointer (&info->mapping, g_hash_table_unref);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/widgets/gimpcontrollerlist.c b/app/widgets/gimpcontrollerlist.c
index dc0d5b7..404528d 100644
--- a/app/widgets/gimpcontrollerlist.c
+++ b/app/widgets/gimpcontrollerlist.c
@@ -341,11 +341,7 @@ gimp_controller_list_finalize (GObject *object)
 {
   GimpControllerList *list = GIMP_CONTROLLER_LIST (object);
 
-  if (list->gimp)
-    {
-      g_object_unref (list->gimp);
-      list->gimp = NULL;
-    }
+  g_clear_object (&list->gimp);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/widgets/gimpcontrollers.c b/app/widgets/gimpcontrollers.c
index 6b91814..d2befd7 100644
--- a/app/widgets/gimpcontrollers.c
+++ b/app/widgets/gimpcontrollers.c
@@ -300,8 +300,8 @@ gimp_controller_manager_free (GimpControllerManager *manager)
   gimp_container_remove_handler (manager->controllers,
                                  manager->event_mapped_id);
 
-  g_object_unref (manager->controllers);
-  g_object_unref (manager->ui_manager);
+  g_clear_object (&manager->controllers);
+  g_clear_object (&manager->ui_manager);
 
   g_slice_free (GimpControllerManager, manager);
 }
diff --git a/app/widgets/gimpcurveview.c b/app/widgets/gimpcurveview.c
index 08e9795..36b9d95 100644
--- a/app/widgets/gimpcurveview.c
+++ b/app/widgets/gimpcurveview.c
@@ -241,29 +241,11 @@ gimp_curve_view_finalize (GObject *object)
 {
   GimpCurveView *view = GIMP_CURVE_VIEW (object);
 
-  if (view->layout)
-    {
-      g_object_unref (view->layout);
-      view->layout = NULL;
-    }
+  g_clear_object (&view->layout);
+  g_clear_object (&view->cursor_layout);
 
-  if (view->cursor_layout)
-    {
-      g_object_unref (view->cursor_layout);
-      view->cursor_layout = NULL;
-    }
-
-  if (view->x_axis_label)
-    {
-      g_free (view->x_axis_label);
-      view->x_axis_label = NULL;
-    }
-
-  if (view->y_axis_label)
-    {
-      g_free (view->y_axis_label);
-      view->y_axis_label = NULL;
-    }
+  g_clear_pointer (&view->x_axis_label, g_free);
+  g_clear_pointer (&view->y_axis_label, g_free);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -357,17 +339,8 @@ gimp_curve_view_style_set (GtkWidget *widget,
 
   GTK_WIDGET_CLASS (parent_class)->style_set (widget, prev_style);
 
-  if (view->layout)
-    {
-      g_object_unref (view->layout);
-      view->layout = NULL;
-    }
-
-  if (view->cursor_layout)
-    {
-      g_object_unref (view->cursor_layout);
-      view->cursor_layout = NULL;
-    }
+  g_clear_object (&view->layout);
+  g_clear_object (&view->cursor_layout);
 }
 
 static void
diff --git a/app/widgets/gimpdasheditor.c b/app/widgets/gimpdasheditor.c
index 9248ae2..553f062 100644
--- a/app/widgets/gimpdasheditor.c
+++ b/app/widgets/gimpdasheditor.c
@@ -142,17 +142,8 @@ gimp_dash_editor_finalize (GObject *object)
 {
   GimpDashEditor *editor = GIMP_DASH_EDITOR (object);
 
-  if (editor->stroke_options)
-    {
-      g_object_unref (editor->stroke_options);
-      editor->stroke_options = NULL;
-    }
-
-  if (editor->segments)
-    {
-      g_free (editor->segments);
-      editor->segments = NULL;
-    }
+  g_clear_object (&editor->stroke_options);
+  g_clear_pointer (&editor->segments, g_free);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/widgets/gimpdatafactoryview.c b/app/widgets/gimpdatafactoryview.c
index 369ad03..7438d54 100644
--- a/app/widgets/gimpdatafactoryview.c
+++ b/app/widgets/gimpdatafactoryview.c
@@ -318,23 +318,10 @@ gimp_data_factory_view_dispose (GObject *object)
 {
   GimpDataFactoryView *factory_view = GIMP_DATA_FACTORY_VIEW (object);
 
-  if (factory_view->priv->tagged_container)
-    {
-      g_object_unref (factory_view->priv->tagged_container);
-      factory_view->priv->tagged_container = NULL;
-    }
+  g_clear_object (&factory_view->priv->tagged_container);
+  g_clear_object (&factory_view->priv->factory);
 
-  if (factory_view->priv->factory)
-    {
-      g_object_unref (factory_view->priv->factory);
-      factory_view->priv->factory = NULL;
-    }
-
-  if (factory_view->priv->action_group)
-    {
-      g_free (factory_view->priv->action_group);
-      factory_view->priv->action_group = NULL;
-    }
+  g_clear_pointer (&factory_view->priv->action_group, g_free);
 
   G_OBJECT_CLASS (parent_class)->dispose (object);
 }
diff --git a/app/widgets/gimpdeviceinfo.c b/app/widgets/gimpdeviceinfo.c
index 46e8d1d..2a43f8a 100644
--- a/app/widgets/gimpdeviceinfo.c
+++ b/app/widgets/gimpdeviceinfo.c
@@ -247,23 +247,10 @@ gimp_device_info_finalize (GObject *object)
 {
   GimpDeviceInfo *info = GIMP_DEVICE_INFO (object);
 
-  if (info->axes)
-    {
-      g_free (info->axes);
-      info->axes = NULL;
-    }
+  g_clear_pointer (&info->axes, g_free);
+  g_clear_pointer (&info->keys, g_free);
 
-  if (info->keys)
-    {
-      g_free (info->keys);
-      info->keys = NULL;
-    }
-
-  if (info->pressure_curve)
-    {
-      g_object_unref (info->pressure_curve);
-      info->pressure_curve = NULL;
-    }
+  g_clear_object (&info->pressure_curve);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/widgets/gimpdeviceinfoeditor.c b/app/widgets/gimpdeviceinfoeditor.c
index 8eaeb36..c56a738 100644
--- a/app/widgets/gimpdeviceinfoeditor.c
+++ b/app/widgets/gimpdeviceinfoeditor.c
@@ -503,11 +503,7 @@ gimp_device_info_editor_finalize (GObject *object)
 
   private = GIMP_DEVICE_INFO_EDITOR_GET_PRIVATE (object);
 
-  if (private->info)
-    {
-      g_object_unref (private->info);
-      private->info = NULL;
-    }
+  g_clear_object (&private->info);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/widgets/gimpdockbook.c b/app/widgets/gimpdockbook.c
index 6b19c91..3895a0f 100644
--- a/app/widgets/gimpdockbook.c
+++ b/app/widgets/gimpdockbook.c
@@ -333,15 +333,12 @@ gimp_dockbook_finalize (GObject *object)
 {
   GimpDockbook *dockbook = GIMP_DOCKBOOK (object);
 
-  if (dockbook->p->ui_manager)
-    {
-      g_object_unref (dockbook->p->ui_manager);
-      dockbook->p->ui_manager = NULL;
-    }
+  g_clear_object (&dockbook->p->ui_manager);
+
   if (dockbook->p->dock)
     {
       g_signal_handlers_disconnect_by_func (gimp_dock_get_context (dockbook->p->dock)->gimp->config,
-                                            G_CALLBACK (gimp_dockbook_config_size_changed),
+                                            gimp_dockbook_config_size_changed,
                                             dockbook);
       dockbook->p->dock = NULL;
     }
diff --git a/app/widgets/gimpdockwindow.c b/app/widgets/gimpdockwindow.c
index 0db51b9..4e191fd 100644
--- a/app/widgets/gimpdockwindow.c
+++ b/app/widgets/gimpdockwindow.c
@@ -493,23 +493,9 @@ gimp_dock_window_dispose (GObject *object)
       dock_window->p->image_flush_handler_id = 0;
     }
 
-  if (dock_window->p->ui_manager)
-    {
-      g_object_unref (dock_window->p->ui_manager);
-      dock_window->p->ui_manager = NULL;
-    }
-
-  if (dock_window->p->dialog_factory)
-    {
-      g_object_unref (dock_window->p->dialog_factory);
-      dock_window->p->dialog_factory = NULL;
-    }
-
-  if (dock_window->p->context)
-    {
-      g_object_unref (dock_window->p->context);
-      dock_window->p->context = NULL;
-    }
+  g_clear_object (&dock_window->p->ui_manager);
+  g_clear_object (&dock_window->p->dialog_factory);
+  g_clear_object (&dock_window->p->context);
 
   G_OBJECT_CLASS (parent_class)->dispose (object);
 }
@@ -519,11 +505,7 @@ gimp_dock_window_finalize (GObject *object)
 {
   GimpDockWindow *dock_window = GIMP_DOCK_WINDOW (object);
 
-  if (dock_window->p->ui_manager_name)
-    {
-      g_free (dock_window->p->ui_manager_name);
-      dock_window->p->ui_manager_name = NULL;
-    }
+  g_clear_pointer (&dock_window->p->ui_manager_name, g_free);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/widgets/gimpdynamicseditor.c b/app/widgets/gimpdynamicseditor.c
index 2e93f96..0cf04ac 100644
--- a/app/widgets/gimpdynamicseditor.c
+++ b/app/widgets/gimpdynamicseditor.c
@@ -193,11 +193,7 @@ gimp_dynamics_editor_finalize (GObject *object)
 {
   GimpDynamicsEditor *editor = GIMP_DYNAMICS_EDITOR (object);
 
-  if (editor->dynamics_model)
-    {
-      g_object_unref (editor->dynamics_model);
-      editor->dynamics_model = NULL;
-    }
+  g_clear_object (&editor->dynamics_model);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/widgets/gimpdynamicsoutputeditor.c b/app/widgets/gimpdynamicsoutputeditor.c
index 0e3a7c0..a8eb53c 100644
--- a/app/widgets/gimpdynamicsoutputeditor.c
+++ b/app/widgets/gimpdynamicsoutputeditor.c
@@ -295,11 +295,7 @@ gimp_dynamics_output_editor_finalize (GObject *object)
 {
   GimpDynamicsOutputEditorPrivate *private = GET_PRIVATE (object);
 
-  if (private->output)
-    {
-      g_object_unref (private->output);
-      private->output = NULL;
-    }
+  g_clear_object (&private->output);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/widgets/gimpeditor.c b/app/widgets/gimpeditor.c
index 9770add..fe40a1d 100644
--- a/app/widgets/gimpeditor.c
+++ b/app/widgets/gimpeditor.c
@@ -265,32 +265,19 @@ gimp_editor_dispose (GObject *object)
 {
   GimpEditor *editor = GIMP_EDITOR (object);
 
-  if (editor->priv->menu_factory)
-    {
-      g_object_unref (editor->priv->menu_factory);
-      editor->priv->menu_factory = NULL;
-    }
+  g_clear_object (&editor->priv->menu_factory);
 
-  if (editor->priv->menu_identifier)
-    {
-      g_free (editor->priv->menu_identifier);
-      editor->priv->menu_identifier = NULL;
-    }
+  g_clear_pointer (&editor->priv->menu_identifier, g_free);
 
   if (editor->priv->ui_manager)
     {
       g_signal_handlers_disconnect_by_func (editor->priv->ui_manager->gimp->config,
                                             G_CALLBACK (gimp_editor_config_size_changed),
                                             editor);
-      g_object_unref (editor->priv->ui_manager);
-      editor->priv->ui_manager = NULL;
+      g_clear_object (&editor->priv->ui_manager);
     }
 
-  if (editor->priv->ui_path)
-    {
-      g_free (editor->priv->ui_path);
-      editor->priv->ui_path = NULL;
-    }
+  g_clear_pointer (&editor->priv->ui_path, g_free);
 
   G_OBJECT_CLASS (parent_class)->dispose (object);
 }
diff --git a/app/widgets/gimpfgbgeditor.c b/app/widgets/gimpfgbgeditor.c
index d06dede..845bc45 100644
--- a/app/widgets/gimpfgbgeditor.c
+++ b/app/widgets/gimpfgbgeditor.c
@@ -178,17 +178,8 @@ gimp_fg_bg_editor_dispose (GObject *object)
   if (editor->context)
     gimp_fg_bg_editor_set_context (editor, NULL);
 
-  if (editor->default_icon)
-    {
-      g_object_unref (editor->default_icon);
-      editor->default_icon = NULL;
-    }
-
-  if (editor->swap_icon)
-    {
-      g_object_unref (editor->swap_icon);
-      editor->swap_icon = NULL;
-    }
+  g_clear_object (&editor->default_icon);
+  g_clear_object (&editor->swap_icon);
 
   G_OBJECT_CLASS (parent_class)->dispose (object);
 }
@@ -245,17 +236,8 @@ gimp_fg_bg_editor_style_set (GtkWidget *widget,
 
   GTK_WIDGET_CLASS (parent_class)->style_set (widget, prev_style);
 
-  if (editor->default_icon)
-    {
-      g_object_unref (editor->default_icon);
-      editor->default_icon = NULL;
-    }
-
-  if (editor->swap_icon)
-    {
-      g_object_unref (editor->swap_icon);
-      editor->swap_icon = NULL;
-    }
+  g_clear_object (&editor->default_icon);
+  g_clear_object (&editor->swap_icon);
 }
 
 static gboolean
@@ -614,8 +596,7 @@ gimp_fg_bg_editor_set_context (GimpFgBgEditor *editor,
           g_signal_handlers_disconnect_by_func (editor->color_config,
                                                 gimp_fg_bg_editor_destroy_transform,
                                                 editor);
-          g_object_unref (editor->color_config);
-          editor->color_config = NULL;
+          g_clear_object (&editor->color_config);
         }
 
       editor->context = context;
@@ -729,11 +710,7 @@ gimp_fg_bg_editor_create_transform (GimpFgBgEditor *editor)
 static void
 gimp_fg_bg_editor_destroy_transform (GimpFgBgEditor *editor)
 {
-  if (editor->transform)
-    {
-      g_object_unref (editor->transform);
-      editor->transform = NULL;
-    }
+  g_clear_object (&editor->transform);
 
   gtk_widget_queue_draw (GTK_WIDGET (editor));
 }
diff --git a/app/widgets/gimpfgbgview.c b/app/widgets/gimpfgbgview.c
index f893100..8f6900a 100644
--- a/app/widgets/gimpfgbgview.c
+++ b/app/widgets/gimpfgbgview.c
@@ -262,11 +262,7 @@ gimp_fg_bg_view_create_transform (GimpFgBgView *view)
 static void
 gimp_fg_bg_view_destroy_transform (GimpFgBgView *view)
 {
-  if (view->transform)
-    {
-      g_object_unref (view->transform);
-      view->transform = NULL;
-    }
+  g_clear_object (&view->transform);
 
   gtk_widget_queue_draw (GTK_WIDGET (view));
 }
@@ -298,14 +294,12 @@ gimp_fg_bg_view_set_context (GimpFgBgView *view,
           g_signal_handlers_disconnect_by_func (view->context,
                                                 gtk_widget_queue_draw,
                                                 view);
-          g_object_unref (view->context);
-          view->context = NULL;
+          g_clear_object (&view->context);
 
           g_signal_handlers_disconnect_by_func (view->color_config,
                                                 gimp_fg_bg_view_destroy_transform,
                                                 view);
-          g_object_unref (view->color_config);
-          view->color_config = NULL;
+          g_clear_object (&view->color_config);
         }
 
       view->context = context;
diff --git a/app/widgets/gimpfilleditor.c b/app/widgets/gimpfilleditor.c
index d4f22f4..98f3648 100644
--- a/app/widgets/gimpfilleditor.c
+++ b/app/widgets/gimpfilleditor.c
@@ -150,11 +150,7 @@ gimp_fill_editor_finalize (GObject *object)
 {
   GimpFillEditor *editor = GIMP_FILL_EDITOR (object);
 
-  if (editor->options)
-    {
-      g_object_unref (editor->options);
-      editor->options = NULL;
-    }
+  g_clear_object (&editor->options);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/widgets/gimpgrideditor.c b/app/widgets/gimpgrideditor.c
index 0ba5406..f5f025e 100644
--- a/app/widgets/gimpgrideditor.c
+++ b/app/widgets/gimpgrideditor.c
@@ -247,17 +247,8 @@ gimp_grid_editor_finalize (GObject *object)
 {
   GimpGridEditor *editor = GIMP_GRID_EDITOR (object);
 
-  if (editor->grid)
-    {
-      g_object_unref (editor->grid);
-      editor->grid = NULL;
-    }
-
-  if (editor->context)
-    {
-      g_object_unref (editor->context);
-      editor->context = NULL;
-    }
+  g_clear_object (&editor->grid);
+  g_clear_object (&editor->context);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/widgets/gimphistogrameditor.c b/app/widgets/gimphistogrameditor.c
index 75b6f6b..cee2380 100644
--- a/app/widgets/gimphistogrameditor.c
+++ b/app/widgets/gimphistogrameditor.c
@@ -259,17 +259,13 @@ gimp_histogram_editor_set_property (GObject      *object,
 
       if (editor->histogram)
         {
-          g_object_unref (editor->histogram);
-          editor->histogram = NULL;
-
+          g_clear_object (&editor->histogram);
           gimp_histogram_view_set_histogram (view, NULL);
         }
 
       if (editor->bg_histogram)
         {
-          g_object_unref (editor->bg_histogram);
-          editor->bg_histogram = NULL;
-
+          g_clear_object (&editor->bg_histogram);
           gimp_histogram_view_set_background (view, NULL);
         }
 
@@ -360,17 +356,13 @@ gimp_histogram_editor_set_image (GimpImageEditor *image_editor,
 
       if (editor->histogram)
         {
-          g_object_unref (editor->histogram);
-          editor->histogram = NULL;
-
+          g_clear_object (&editor->histogram);
           gimp_histogram_view_set_histogram (view, NULL);
         }
 
       if (editor->bg_histogram)
         {
-          g_object_unref (editor->bg_histogram);
-          editor->bg_histogram = NULL;
-
+          g_clear_object (&editor->bg_histogram);
           gimp_histogram_view_set_background (view, NULL);
         }
     }
@@ -409,17 +401,13 @@ gimp_histogram_editor_layer_changed (GimpImage           *image,
 
       if (editor->histogram)
         {
-          g_object_unref (editor->histogram);
-          editor->histogram = NULL;
-
+          g_clear_object (&editor->histogram);
           gimp_histogram_view_set_histogram (view, NULL);
         }
 
       if (editor->bg_histogram)
         {
-          g_object_unref (editor->bg_histogram);
-          editor->bg_histogram = NULL;
-
+          g_clear_object (&editor->bg_histogram);
           gimp_histogram_view_set_background (view, NULL);
         }
 
@@ -535,9 +523,7 @@ gimp_histogram_editor_frozen_update (GimpHistogramEditor *editor,
     }
   else if (editor->bg_histogram)
     {
-      g_object_unref (editor->bg_histogram);
-      editor->bg_histogram = NULL;
-
+      g_clear_object (&editor->bg_histogram);
       gimp_histogram_view_set_background (view, NULL);
     }
 }
diff --git a/app/widgets/gimpiconpicker.c b/app/widgets/gimpiconpicker.c
index fcb52eb..2a1e5dc 100644
--- a/app/widgets/gimpiconpicker.c
+++ b/app/widgets/gimpiconpicker.c
@@ -285,41 +285,13 @@ gimp_icon_picker_finalize (GObject *object)
 {
   GimpIconPickerPrivate *private = GET_PRIVATE (object);
 
-  if (private->icon_name)
-    {
-      g_free (private->icon_name);
-      private->icon_name = NULL;
-    }
-
-  if (private->icon_name_container)
-    {
-      g_object_unref (private->icon_name_container);
-      private->icon_name_container = NULL;
-    }
-
-  if (private->icon_name_context)
-    {
-      g_object_unref (private->icon_name_context);
-      private->icon_name_context = NULL;
-    }
+  g_clear_pointer (&private->icon_name, g_free);
 
-  if (private->icon_pixbuf)
-    {
-      g_object_unref (private->icon_pixbuf);
-      private->icon_pixbuf = NULL;
-    }
-
-  if (private->preview)
-    {
-      g_object_unref (private->preview);
-      private->preview = NULL;
-    }
-
-  if (private->null_template_object)
-    {
-      g_object_unref (private->null_template_object);
-      private->null_template_object = NULL;
-    }
+  g_clear_object (&private->icon_name_container);
+  g_clear_object (&private->icon_name_context);
+  g_clear_object (&private->icon_pixbuf);
+  g_clear_object (&private->preview);
+  g_clear_object (&private->null_template_object);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/widgets/gimplanguageentry.c b/app/widgets/gimplanguageentry.c
index 0750eff..41676d8 100644
--- a/app/widgets/gimplanguageentry.c
+++ b/app/widgets/gimplanguageentry.c
@@ -130,17 +130,9 @@ gimp_language_entry_finalize (GObject *object)
 {
   GimpLanguageEntry *entry = GIMP_LANGUAGE_ENTRY (object);
 
-  if (entry->store)
-    {
-      g_object_unref (entry->store);
-      entry->store = NULL;
-    }
+  g_clear_object (&entry->store);
 
-  if (entry->code)
-    {
-      g_free (entry->code);
-      entry->code = NULL;
-    }
+  g_clear_pointer (&entry->code, g_free);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/widgets/gimppdbdialog.c b/app/widgets/gimppdbdialog.c
index 05337f1..2e059d7 100644
--- a/app/widgets/gimppdbdialog.c
+++ b/app/widgets/gimppdbdialog.c
@@ -215,35 +215,13 @@ gimp_pdb_dialog_dispose (GObject *object)
 
   klass->dialogs = g_list_remove (klass->dialogs, object);
 
-  if (dialog->pdb)
-    {
-      g_object_unref (dialog->pdb);
-      dialog->pdb = NULL;
-    }
-
-  if (dialog->caller_context)
-    {
-      g_object_unref (dialog->caller_context);
-      dialog->caller_context = NULL;
-    }
-
-  if (dialog->context)
-    {
-      g_object_unref (dialog->context);
-      dialog->context = NULL;
-    }
+  g_clear_object (&dialog->pdb);
+  g_clear_object (&dialog->caller_context);
+  g_clear_object (&dialog->context);
 
-  if (dialog->callback_name)
-    {
-      g_free (dialog->callback_name);
-      dialog->callback_name = NULL;
-    }
+  g_clear_pointer (&dialog->callback_name, g_free);
 
-  if (dialog->menu_factory)
-    {
-      g_object_unref (dialog->menu_factory);
-      dialog->menu_factory = NULL;
-    }
+  g_clear_object (&dialog->menu_factory);
 
   G_OBJECT_CLASS (parent_class)->dispose (object);
 }
diff --git a/app/widgets/gimppickablebutton.c b/app/widgets/gimppickablebutton.c
index 7a11ea8..3e4251b 100644
--- a/app/widgets/gimppickablebutton.c
+++ b/app/widgets/gimppickablebutton.c
@@ -179,11 +179,7 @@ gimp_pickable_button_finalize (GObject *object)
 {
   GimpPickableButton *button = GIMP_PICKABLE_BUTTON (object);
 
-  if (button->private->context)
-    {
-      g_object_unref (button->private->context);
-      button->private->context = NULL;
-    }
+  g_clear_object (&button->private->context);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/widgets/gimppickablepopup.c b/app/widgets/gimppickablepopup.c
index 0a24361..de9be27 100644
--- a/app/widgets/gimppickablepopup.c
+++ b/app/widgets/gimppickablepopup.c
@@ -266,17 +266,8 @@ gimp_pickable_popup_finalize (GObject *object)
 {
   GimpPickablePopup *popup = GIMP_PICKABLE_POPUP (object);
 
-  if (popup->priv->pickable)
-    {
-      g_object_unref (popup->priv->pickable);
-      popup->priv->pickable = NULL;
-    }
-
-  if (popup->priv->context)
-    {
-      g_object_unref (popup->priv->context);
-      popup->priv->context = NULL;
-    }
+  g_clear_object (&popup->priv->pickable);
+  g_clear_object (&popup->priv->context);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/widgets/gimppluginview.c b/app/widgets/gimppluginview.c
index 6f00fac..048abc4 100644
--- a/app/widgets/gimppluginview.c
+++ b/app/widgets/gimppluginview.c
@@ -97,11 +97,7 @@ gimp_plug_in_view_finalize (GObject *object)
 {
   GimpPlugInView *view = GIMP_PLUG_IN_VIEW (object);
 
-  if (view->plug_in_hash)
-    {
-      g_hash_table_unref (view->plug_in_hash);
-      view->plug_in_hash = NULL;
-    }
+  g_clear_pointer (&view->plug_in_hash, g_hash_table_unref);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/widgets/gimpprocedureaction.c b/app/widgets/gimpprocedureaction.c
index 19195f5..da53b5f 100644
--- a/app/widgets/gimpprocedureaction.c
+++ b/app/widgets/gimpprocedureaction.c
@@ -108,11 +108,7 @@ gimp_procedure_action_finalize (GObject *object)
 {
   GimpProcedureAction *action = GIMP_PROCEDURE_ACTION (object);
 
-  if (action->procedure)
-    {
-      g_object_unref (action->procedure);
-      action->procedure = NULL;
-    }
+  g_clear_object (&action->procedure);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/widgets/gimpsettingsbox.c b/app/widgets/gimpsettingsbox.c
index 9ebba75..187dedd 100644
--- a/app/widgets/gimpsettingsbox.c
+++ b/app/widgets/gimpsettingsbox.c
@@ -375,29 +375,10 @@ gimp_settings_box_finalize (GObject *object)
 {
   GimpSettingsBoxPrivate *private = GET_PRIVATE (object);
 
-  if (private->config)
-    {
-      g_object_unref (private->config);
-      private->config = NULL;
-    }
-
-  if (private->container)
-    {
-      g_object_unref (private->container);
-      private->container = NULL;
-    }
-
-  if (private->last_file)
-    {
-      g_object_unref (private->last_file);
-      private->last_file = NULL;
-    }
-
-  if (private->default_folder)
-    {
-      g_object_unref (private->default_folder);
-      private->default_folder = NULL;
-    }
+  g_clear_object (&private->config);
+  g_clear_object (&private->container);
+  g_clear_object (&private->last_file);
+  g_clear_object (&private->default_folder);
 
   g_free (private->help_id);
   g_free (private->import_title);
diff --git a/app/widgets/gimpsettingseditor.c b/app/widgets/gimpsettingseditor.c
index d7b2e52..b27cc71 100644
--- a/app/widgets/gimpsettingseditor.c
+++ b/app/widgets/gimpsettingseditor.c
@@ -224,17 +224,8 @@ gimp_settings_editor_finalize (GObject *object)
 {
   GimpSettingsEditorPrivate *private = GET_PRIVATE (object);
 
-  if (private->config)
-    {
-      g_object_unref (private->config);
-      private->config = NULL;
-    }
-
-  if (private->container)
-    {
-      g_object_unref (private->container);
-      private->container = NULL;
-    }
+  g_clear_object (&private->config);
+  g_clear_object (&private->container);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/widgets/gimpspinscale.c b/app/widgets/gimpspinscale.c
index fd3c132..8af8c00 100644
--- a/app/widgets/gimpspinscale.c
+++ b/app/widgets/gimpspinscale.c
@@ -188,11 +188,7 @@ gimp_spin_scale_dispose (GObject *object)
 
   gimp_spin_scale_setup_mnemonic (GIMP_SPIN_SCALE (object), keyval);
 
-  if (private->layout)
-    {
-      g_object_unref (private->layout);
-      private->layout = NULL;
-    }
+  g_clear_object (&private->layout);
 
   G_OBJECT_CLASS (parent_class)->dispose (object);
 }
@@ -202,23 +198,9 @@ gimp_spin_scale_finalize (GObject *object)
 {
   GimpSpinScalePrivate *private = GET_PRIVATE (object);
 
-  if (private->label)
-    {
-      g_free (private->label);
-      private->label = NULL;
-    }
-
-  if (private->label_text)
-    {
-      g_free (private->label_text);
-      private->label_text = NULL;
-    }
-
-  if (private->label_pattern)
-    {
-      g_free (private->label_pattern);
-      private->label_pattern = NULL;
-    }
+  g_clear_pointer (&private->label,         g_free);
+  g_clear_pointer (&private->label_text,    g_free);
+  g_clear_pointer (&private->label_pattern, g_free);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -308,11 +290,7 @@ gimp_spin_scale_style_set (GtkWidget *widget,
 
   GTK_WIDGET_CLASS (parent_class)->style_set (widget, prev_style);
 
-  if (private->layout)
-    {
-      g_object_unref (private->layout);
-      private->layout = NULL;
-    }
+  g_clear_object (&private->layout);
 }
 
 static PangoAttrList *
@@ -769,11 +747,7 @@ gimp_spin_scale_screen_changed (GtkWidget *widget,
   GimpSpinScalePrivate *private = GET_PRIVATE (scale);
   GtkSettings          *settings;
 
-  if (private->layout)
-    {
-      g_object_unref (private->layout);
-      private->layout = NULL;
-    }
+  g_clear_object (&private->layout);
 
   if (old_screen)
     {
@@ -856,11 +830,7 @@ gimp_spin_scale_mnemonics_notify (GtkWindow        *window,
     {
       private->mnemonics_visible = mnemonics_visible;
 
-      if (private->layout)
-        {
-          g_object_unref (private->layout);
-          private->layout = NULL;
-        }
+      g_clear_object (&private->layout);
 
       gtk_widget_queue_draw (GTK_WIDGET (scale));
     }
@@ -1032,11 +1002,7 @@ gimp_spin_scale_set_label (GimpSpinScale *scale,
       gimp_spin_scale_setup_mnemonic (scale, previous);
     }
 
-  if (private->layout)
-    {
-      g_object_unref (private->layout);
-      private->layout = NULL;
-    }
+  g_clear_object (&private->layout);
 
   gtk_widget_queue_resize (GTK_WIDGET (scale));
 
diff --git a/app/widgets/gimptagentry.c b/app/widgets/gimptagentry.c
index e2093f9..4e4bda9 100644
--- a/app/widgets/gimptagentry.c
+++ b/app/widgets/gimptagentry.c
@@ -243,8 +243,7 @@ gimp_tag_entry_dispose (GObject *object)
       g_signal_handlers_disconnect_by_func (entry->container,
                                             gimp_tag_entry_container_changed,
                                             entry);
-      g_object_unref (entry->container);
-      entry->container = NULL;
+      g_clear_object (&entry->container);
     }
 
   if (entry->mask)
@@ -803,7 +802,7 @@ gimp_tag_entry_assign_tags (GimpTagEntry *tag_entry)
         }
     }
 
-  g_list_free_full (add_list, (GDestroyNotify) g_object_unref);
+  g_list_free_full (add_list,    (GDestroyNotify) g_object_unref);
   g_list_free_full (remove_list, (GDestroyNotify) g_object_unref);
 
   /* common tags list with changes applied. */
diff --git a/app/widgets/gimptagpopup.c b/app/widgets/gimptagpopup.c
index a1a7ecc..b7d66e1 100644
--- a/app/widgets/gimptagpopup.c
+++ b/app/widgets/gimptagpopup.c
@@ -368,23 +368,9 @@ gimp_tag_popup_dispose (GObject *object)
 
   gimp_tag_popup_remove_scroll_timeout (popup);
 
-  if (popup->combo_entry)
-    {
-      g_object_unref (popup->combo_entry);
-      popup->combo_entry = NULL;
-    }
-
-  if (popup->layout)
-    {
-      g_object_unref (popup->layout);
-      popup->layout = NULL;
-    }
-
-  if (popup->context)
-    {
-      g_object_unref (popup->context);
-      popup->context = NULL;
-    }
+  g_clear_object (&popup->combo_entry);
+  g_clear_object (&popup->layout);
+  g_clear_object (&popup->context);
 
   if (popup->tag_data)
     {
diff --git a/app/widgets/gimptemplateeditor.c b/app/widgets/gimptemplateeditor.c
index 27988cb..259399b 100644
--- a/app/widgets/gimptemplateeditor.c
+++ b/app/widgets/gimptemplateeditor.c
@@ -485,11 +485,7 @@ gimp_template_editor_finalize (GObject *object)
 {
   GimpTemplateEditorPrivate *private = GET_PRIVATE (object);
 
-  if (private->template)
-    {
-      g_object_unref (private->template);
-      private->template = NULL;
-    }
+  g_clear_object (&private->template);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/widgets/gimptexteditor.c b/app/widgets/gimptexteditor.c
index afac651..78e76b5 100644
--- a/app/widgets/gimptexteditor.c
+++ b/app/widgets/gimptexteditor.c
@@ -118,11 +118,8 @@ gimp_text_editor_finalize (GObject *object)
 {
   GimpTextEditor *editor = GIMP_TEXT_EDITOR (object);
 
-  if (editor->font_name)
-    g_free (editor->font_name);
-
-  if (editor->ui_manager)
-    g_object_unref (editor->ui_manager);
+  g_clear_pointer (&editor->font_name, g_free);
+  g_clear_object (&editor->ui_manager);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/widgets/gimptextstyleeditor.c b/app/widgets/gimptextstyleeditor.c
index 3a37547..bbe5967 100644
--- a/app/widgets/gimptextstyleeditor.c
+++ b/app/widgets/gimptextstyleeditor.c
@@ -403,29 +403,10 @@ gimp_text_style_editor_finalize (GObject *object)
 {
   GimpTextStyleEditor *editor = GIMP_TEXT_STYLE_EDITOR (object);
 
-  if (editor->context)
-    {
-      g_object_unref (editor->context);
-      editor->context = NULL;
-    }
-
-  if (editor->text)
-    {
-      g_object_unref (editor->text);
-      editor->text = NULL;
-    }
-
-  if (editor->buffer)
-    {
-      g_object_unref (editor->buffer);
-      editor->buffer = NULL;
-    }
-
-  if (editor->fonts)
-    {
-      g_object_unref (editor->fonts);
-      editor->fonts = NULL;
-    }
+  g_clear_object (&editor->context);
+  g_clear_object (&editor->text);
+  g_clear_object (&editor->buffer);
+  g_clear_object (&editor->fonts);
 
   if (editor->toggles)
     {
diff --git a/app/widgets/gimpthumbbox.c b/app/widgets/gimpthumbbox.c
index b9f401d..991acda 100644
--- a/app/widgets/gimpthumbbox.c
+++ b/app/widgets/gimpthumbbox.c
@@ -157,11 +157,7 @@ gimp_thumb_box_finalize (GObject *object)
 
   gimp_thumb_box_take_files (box, NULL);
 
-  if (box->imagefile)
-    {
-      g_object_unref (box->imagefile);
-      box->imagefile = NULL;
-    }
+  g_clear_object (&box->imagefile);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/widgets/gimptoolbox.c b/app/widgets/gimptoolbox.c
index 5f23847..4f29b53 100644
--- a/app/widgets/gimptoolbox.c
+++ b/app/widgets/gimptoolbox.c
@@ -329,11 +329,7 @@ gimp_toolbox_dispose (GObject *object)
 
   toolbox->p->in_destruction = TRUE;
 
-  if (toolbox->p->context)
-    {
-      g_object_unref (toolbox->p->context);
-      toolbox->p->context = NULL;
-    }
+  g_clear_object (&toolbox->p->context);
 
   G_OBJECT_CLASS (parent_class)->dispose (object);
 
diff --git a/app/widgets/gimptoolpreseteditor.c b/app/widgets/gimptoolpreseteditor.c
index 93ccbe2..5555327 100644
--- a/app/widgets/gimptoolpreseteditor.c
+++ b/app/widgets/gimptoolpreseteditor.c
@@ -212,11 +212,7 @@ gimp_tool_preset_editor_finalize (GObject *object)
 {
   GimpToolPresetEditor *editor = GIMP_TOOL_PRESET_EDITOR (object);
 
-  if (editor->priv->tool_preset_model)
-    {
-      g_object_unref (editor->priv->tool_preset_model);
-      editor->priv->tool_preset_model = NULL;
-    }
+  g_clear_object (&editor->priv->tool_preset_model);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/widgets/gimpundoeditor.c b/app/widgets/gimpundoeditor.c
index 4acdea0..9d8a262 100644
--- a/app/widgets/gimpundoeditor.c
+++ b/app/widgets/gimpundoeditor.c
@@ -327,15 +327,10 @@ gimp_undo_editor_clear (GimpUndoEditor *editor)
     {
       gimp_container_view_set_container (GIMP_CONTAINER_VIEW (editor->view),
                                          NULL);
-      g_object_unref (editor->container);
-      editor->container = NULL;
+      g_clear_object (&editor->container);
     }
 
-  if (editor->base_item)
-    {
-      g_object_unref (editor->base_item);
-      editor->base_item = NULL;
-    }
+  g_clear_object (&editor->base_item);
 }
 
 static void
diff --git a/app/widgets/gimpview.c b/app/widgets/gimpview.c
index 123844f..2349d27 100644
--- a/app/widgets/gimpview.c
+++ b/app/widgets/gimpview.c
@@ -197,11 +197,7 @@ gimp_view_dispose (GObject *object)
   if (view->viewable)
     gimp_view_set_viewable (view, NULL);
 
-  if (view->renderer)
-    {
-      g_object_unref (view->renderer);
-      view->renderer = NULL;
-    }
+  g_clear_object (&view->renderer);
 
   G_OBJECT_CLASS (parent_class)->dispose (object);
 }
diff --git a/app/widgets/gimpviewrenderer.c b/app/widgets/gimpviewrenderer.c
index 6043441..3679c89 100644
--- a/app/widgets/gimpviewrenderer.c
+++ b/app/widgets/gimpviewrenderer.c
@@ -206,29 +206,10 @@ gimp_view_renderer_finalize (GObject *object)
 {
   GimpViewRenderer *renderer = GIMP_VIEW_RENDERER (object);
 
-  if (renderer->priv->pattern)
-    {
-      cairo_pattern_destroy (renderer->priv->pattern);
-      renderer->priv->pattern = NULL;
-    }
-
-  if (renderer->surface)
-    {
-      cairo_surface_destroy (renderer->surface);
-      renderer->surface = NULL;
-    }
-
-  if (renderer->priv->pixbuf)
-    {
-      g_object_unref (renderer->priv->pixbuf);
-      renderer->priv->pixbuf = NULL;
-    }
-
-  if (renderer->priv->bg_icon_name)
-    {
-      g_free (renderer->priv->bg_icon_name);
-      renderer->priv->bg_icon_name = NULL;
-    }
+  g_clear_pointer (&renderer->priv->pattern, cairo_pattern_destroy);
+  g_clear_pointer (&renderer->surface, cairo_surface_destroy);
+  g_clear_object (&renderer->priv->pixbuf);
+  g_clear_pointer (&renderer->priv->bg_icon_name, g_free);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -348,17 +329,8 @@ gimp_view_renderer_set_viewable (GimpViewRenderer *renderer,
   if (viewable == renderer->viewable)
     return;
 
-  if (renderer->surface)
-    {
-      cairo_surface_destroy (renderer->surface);
-      renderer->surface = NULL;
-    }
-
-  if (renderer->priv->pixbuf)
-    {
-      g_object_unref (renderer->priv->pixbuf);
-      renderer->priv->pixbuf = NULL;
-    }
+  g_clear_pointer (&renderer->surface, cairo_surface_destroy);
+  g_clear_object (&renderer->priv->pixbuf);
 
   gimp_view_renderer_free_color_transform (renderer);
 
@@ -473,11 +445,7 @@ gimp_view_renderer_set_size_full (GimpViewRenderer *renderer,
       renderer->height       = height;
       renderer->border_width = border_width;
 
-      if (renderer->surface)
-        {
-          cairo_surface_destroy (renderer->surface);
-          renderer->surface = NULL;
-        }
+      g_clear_pointer (&renderer->surface, cairo_surface_destroy);
 
       if (renderer->viewable)
         gimp_view_renderer_invalidate (renderer);
@@ -557,11 +525,7 @@ gimp_view_renderer_set_background (GimpViewRenderer *renderer,
 
   renderer->priv->bg_icon_name = g_strdup (icon_name);
 
-  if (renderer->priv->pattern)
-    {
-      g_object_unref (renderer->priv->pattern);
-      renderer->priv->pattern = NULL;
-    }
+  g_clear_object (&renderer->priv->pattern);
 }
 
 void
@@ -938,11 +902,7 @@ gimp_view_renderer_render_temp_buf (GimpViewRenderer *renderer,
                                     GimpViewBG        inside_bg,
                                     GimpViewBG        outside_bg)
 {
-  if (renderer->priv->pixbuf)
-    {
-      g_object_unref (renderer->priv->pixbuf);
-      renderer->priv->pixbuf = NULL;
-    }
+  g_clear_object (&renderer->priv->pixbuf);
 
   if (! renderer->surface)
     renderer->surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
@@ -971,11 +931,7 @@ gimp_view_renderer_render_pixbuf (GimpViewRenderer *renderer,
   GimpColorTransform *transform;
   const Babl         *format;
 
-  if (renderer->surface)
-    {
-      cairo_surface_destroy (renderer->surface);
-      renderer->surface = NULL;
-    }
+  g_clear_pointer (&renderer->surface, cairo_surface_destroy);
 
   format = gimp_pixbuf_get_format (pixbuf);
 
@@ -1038,17 +994,8 @@ gimp_view_renderer_render_icon (GimpViewRenderer *renderer,
   g_return_if_fail (GTK_IS_WIDGET (widget));
   g_return_if_fail (icon_name != NULL);
 
-  if (renderer->priv->pixbuf)
-    {
-      g_object_unref (renderer->priv->pixbuf);
-      renderer->priv->pixbuf = NULL;
-    }
-
-  if (renderer->surface)
-    {
-      cairo_surface_destroy (renderer->surface);
-      renderer->surface = NULL;
-    }
+  g_clear_object (&renderer->priv->pixbuf);
+  g_clear_pointer (&renderer->surface, cairo_surface_destroy);
 
   pixbuf = gimp_widget_load_icon (widget, icon_name,
                                   MIN (renderer->width, renderer->height));
@@ -1133,11 +1080,7 @@ gimp_view_renderer_free_color_transform (GimpViewRenderer *renderer)
 {
   g_return_if_fail (GIMP_IS_VIEW_RENDERER (renderer));
 
-  if (renderer->priv->profile_transform)
-    {
-      g_object_unref (renderer->priv->profile_transform);
-      renderer->priv->profile_transform = NULL;
-    }
+  g_clear_object (&renderer->priv->profile_transform);
 
   gimp_view_renderer_invalidate (renderer);
 }


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