KGRKJGETMRETU895U-589TY5MIGM5JGB5SDFESFREWTGR54TY
Server : Apache/2.2.17 (Unix) mod_ssl/2.2.17 OpenSSL/0.9.8e-fips-rhel5 DAV/2 PHP/5.2.17
System : Linux localhost 2.6.18-419.el5 #1 SMP Fri Feb 24 22:47:42 UTC 2017 x86_64
User : nobody ( 99)
PHP Version : 5.2.17
Disable Function : NONE
Directory :  /proc/22697/root/usr/share/gtk-doc/html/gobject/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //proc/22697/root/usr/share/gtk-doc/html/gobject/howto-gobject-destruction.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Object Destruction</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.69.1">
<link rel="start" href="index.html" title="GObject Reference Manual">
<link rel="up" href="howto-gobject.html" title="How To define and implement a new GObject?">
<link rel="prev" href="howto-gobject-construction.html" title="Object Construction">
<link rel="next" href="howto-gobject-methods.html" title="Object methods">
<meta name="generator" content="GTK-Doc V1.6 (XML mode)">
<link rel="stylesheet" href="style.css" type="text/css">
<link rel="preface" href="pr01.html" title="Introduction">
<link rel="part" href="pt01.html" title="Part&#160;I.&#160;Concepts">
<link rel="chapter" href="ch01.html" title="Background">
<link rel="chapter" href="ch02.html" title="The Glib Dynamic Type System">
<link rel="chapter" href="chapter-gobject.html" title="The GObject base class">
<link rel="chapter" href="chapter-signal.html" title="The GObject messaging system">
<link rel="reference" href="rn01.html" title="API Reference">
<link rel="reference" href="rn02.html" title="Tools Reference">
<link rel="part" href="pt02.html" title="Part&#160;IV.&#160;Tutorial">
<link rel="chapter" href="howto-gobject.html" title="How To define and implement a new GObject?">
<link rel="chapter" href="howto-interface.html" title="How To define and implement Interfaces?">
<link rel="chapter" href="howto-signals.html" title="Howto create and use signals">
<link rel="part" href="pt03.html" title="Part&#160;V.&#160;Related Tools">
<link rel="chapter" href="tools-gob.html" title="GObject builder">
<link rel="chapter" href="tools-ginspector.html" title="Graphical inspection of Gobjects">
<link rel="chapter" href="tools-refdb.html" title="Debugging reference count problems">
<link rel="chapter" href="tools-gtkdoc.html" title="Writing API docs">
<link rel="index" href="ix01.html" title="Index">
<link rel="index" href="ix02.html" title="Index of deprecated symbols">
<link rel="index" href="ix03.html" title="Index of new symbols in 2.2">
<link rel="index" href="ix04.html" title="Index of new symbols in 2.4">
<link rel="index" href="ix05.html" title="Index of new symbols in 2.6">
<link rel="index" href="ix06.html" title="Index of new symbols in 2.8">
<link rel="index" href="ix07.html" title="Index of new symbols in 2.10">
<link rel="index" href="ix08.html" title="Index of new symbols in 2.12">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle">
<td><a accesskey="p" href="howto-gobject-construction.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
<td><a accesskey="u" href="howto-gobject.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
<th width="100%" align="center">GObject Reference Manual</th>
<td><a accesskey="n" href="howto-gobject-methods.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
</tr></table>
<div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="howto-gobject-destruction"></a>Object Destruction</h2></div></div></div>
<p>
        Again, it is often difficult to figure out which mechanism to use to hook into the object's
        destruction process: when the last <code class="function"><a href="gobject-The-Base-Object-Type.html#g-object-unref">g_object_unref</a></code> function call is made,
        a lot of things happen as described in <a href="gobject-memory.html#gobject-destruction-table" title="Table&#160;2.&#160;g_object_unref">Table&#160;2, &#8220;g_object_unref&#8221;</a>.
      </p>
<p>
        The destruction process of your object must be split is two different phases: you must override
        both the dispose and the finalize class methods.
</p>
<pre class="programlisting">
struct _MamanBarPrivate {
  gboolean dispose_has_run;
};

static GObjectClass parent_class = NULL;

static void
bar_dispose (GObject *obj)
{
  MamanBar *self = (MamanBar *)obj;

  if (self-&gt;private-&gt;dispose_has_run) {
   /* If dispose did already run, return. */
    return;
  }
  /* Make sure dispose does not run twice. */
  object-&gt;private-&gt;dispose_has_run = TRUE;

  /* 
   * In dispose, you are supposed to free all types referenced from this
   * object which might themselves hold a reference to self. Generally,
   * the most simple solution is to unref all members on which you own a 
   * reference.
   */

   /* Chain up to the parent class */
   G_OBJECT_CLASS (parent_class)-&gt;dispose (obj);
}

static void
bar_finalize (GObject *obj)
{
  MamanBar *self = (MamanBar *)obj;

  /*
   * Here, complete object destruction.
   * You might not need to do much...
   */
  g_free (self-&gt;private);

   /* Chain up to the parent class */
   G_OBJECT_CLASS (parent_class)-&gt;finalize (obj);
}

static void
bar_class_init (BarClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class-&gt;dispose = bar_dispose;
  gobject_class-&gt;finalize = bar_finalize;
}

static void
maman_bar_init (GTypeInstance   *instance,
                gpointer         g_class)
{
  MamanBar *self = (MamanBar *)instance;
  self-&gt;private = g_new0 (MamanBarPrivate, 1);
  self-&gt;private-&gt;dispose_has_run = FALSE;

  parent_class = g_type_class_peek_parent (klass);
}
</pre>
<p>
      </p>
<p>
        Add similar code to your GObject, make sure the code still builds and runs: dispose and finalize must be called
        during the last unref.
        It is possible that object methods might be invoked after dispose is run and before finalize runs. GObject
        does not consider this to be a program error: you must gracefully detect this and neither crash nor warn
        the user. To do this, you need something like the following code at the start of each object method, to make
        sure the object's data is still valid before manipulating it:
</p>
<pre class="programlisting">
if (self-&gt;private-&gt;dispose_has_run) {
  /* Dispose has run. Data is not valid anymore. */
  return;
}
</pre>
<p>
      </p>
</div>
</body>
</html>

Anon7 - 2021