title: Does dereferencing a GArray unref it?
body: <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdbool.h&gt;
  #include &lt;stdint.h&gt;
  #include &lt;glib.h&gt;
  
  void transform_pointer_contents(GArray* const arr) {
      GArray* const tmp = g_array_new(true, true, sizeof(uint8_t));
      const uint8_t example_num = 5;
      g_array_append_val(tmp, example_num);
  
      *arr = *tmp
  
      // Why does this cause a double-free error?
      g_array_unref(tmp);
  }
  
  int main() {
      GArray* const arr = g_array_new(true, true, sizeof(uint8_t));
      transform_pointer_contents(arr);
      printf(&quot;Arr length is %d/n&quot;, arr-&gt;len);
  
      g_array_unref(arr);
  }
  </code></pre>
  <h1>Questions</h1>
  <ul>
  <li>Why does the unref at the end of <code>transform_pointer_contents</code> cause a double-free?</li>
  <li>I suspect that the dereferencing of <code>tmp</code> also unrefs it somehow, if that is true, how does it work? I think that a similar reference-counted type in C++ would work by overwriting the dereference operator, but surely C doesn't work that way?</li>
  <li>Do I have a guarantee that <code>tmp</code> is properly freed without the unref?</li>
  </ul>