GNOME Bugzilla – Bug 597785
g_type_class_add_private code snippet is a bad example
Last modified: 2012-03-01 21:54:12 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.
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.
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.
This is mostly fixed now, but the return_val_if_fail part is missing. Christian, could you update your patch?
Created attachment 169257 [details] [review] Emphasize use of priv in member and use g_return_val_if_fail
Review of attachment 169257 [details] [review]: looks good to me
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.
Created attachment 184292 [details] [review] Emphasize use of priv in member and use g_return_val_if_fail.v2 Updated patch
Comment on attachment 184292 [details] [review] Emphasize use of priv in member and use g_return_val_if_fail.v2 commit cb0566eaf3b245bf2eaa8c9e912cf240d8868193
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.