After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 597785 - g_type_class_add_private code snippet is a bad example
g_type_class_add_private code snippet is a bad example
Status: RESOLVED FIXED
Product: glib
Classification: Platform
Component: gobject
2.25.x
Other All
: Normal normal
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2009-10-08 09:38 UTC by Christian Dywan
Modified: 2012-03-01 21:54 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Use "priv" member to cache the private pointer (1.01 KB, patch)
2009-10-08 09:53 UTC, Christian Dywan
none Details | Review
Emphasize use of priv in member and use g_return_val_if_fail (1.08 KB, patch)
2010-09-01 13:47 UTC, Christian Dywan
needs-work Details | Review
Emphasize use of priv in member and use g_return_val_if_fail.v2 (1.30 KB, patch)
2011-03-26 14:50 UTC, Javier Jardón (IRC: jjardon)
committed Details | Review

Description Christian Dywan 2009-10-08 09:38:26 UTC
The function g_type_class_add_private explains by example how a private field can be added to an object. I think it should use a 'priv' variable in the object structure since that is a lot faster and many people don't realize this.
Comment 1 Christian Dywan 2009-10-08 09:53:44 UTC
Created attachment 145025 [details] [review]
Use "priv" member to cache the private pointer

I adjusted the example to mention the overhead of obtaining the private data, to use it in the example accessor and turned the function into a public function to demonstrate return_val_if_fail.
Comment 2 Christian Dywan 2009-10-08 09:57:35 UTC
In case it's not clear why I used return_val_if_fail there: since there is a function before the return statement, the private must be declared beforehand, and I think it is less common to write a function at all only to lookup a private field.
Comment 3 Javier Jardón (IRC: jjardon) 2010-08-02 23:40:36 UTC
This is mostly fixed now, but the return_val_if_fail part is missing.

Christian, could you update your patch?
Comment 4 Christian Dywan 2010-09-01 13:47:55 UTC
Created attachment 169257 [details] [review]
Emphasize use of priv in member and use g_return_val_if_fail
Comment 5 Javier Jardón (IRC: jjardon) 2010-09-01 15:40:28 UTC
Review of attachment 169257 [details] [review]:

looks good to me
Comment 6 Stefan Sauer (gstreamer, gtkdoc dev) 2011-03-26 12:42:40 UTC
Review of attachment 169257 [details] [review]:

::: gobject/gtype.c
@@ +4421,3 @@
  *
+ *   g_return_val_if_fail (MY_IS_OBJECT (my_object), 0);
+ *

this would be too late, as you dereferenced my_object already. Yet another place where C99 is handy.
Comment 7 Javier Jardón (IRC: jjardon) 2011-03-26 14:50:19 UTC
Created attachment 184292 [details] [review]
Emphasize use of priv in member and use g_return_val_if_fail.v2

Updated patch
Comment 8 Javier Jardón (IRC: jjardon) 2012-03-01 21:54:04 UTC
Comment on attachment 184292 [details] [review]
Emphasize use of priv in member and use g_return_val_if_fail.v2

commit cb0566eaf3b245bf2eaa8c9e912cf240d8868193
Comment 9 Javier Jardón (IRC: jjardon) 2012-03-01 21:54:12 UTC
This problem has been fixed in the development version. The fix will be available in the next major software release. Thank you for your bug report.