Commit Graph

189 Commits

Author SHA1 Message Date
Dan Winship
efecfe0fac gobject: simplify object-in-construction handling
Rather than keeping a global list of objects that are being
constructed, use qdata on the object itself like we do with several
other properties now.

https://bugzilla.gnome.org/show_bug.cgi?id=661576
2013-10-22 11:01:15 -04:00
Dan Winship
0d62eb467f gobject: forbid finalization-during-construction
If a constructor() implementation created an object but then unreffed
it rather than returning it, that object would get left on the
construction_objects list, which would cause problems later when that
memory location got reused by another object.

"Fix" this by making it fail intentionally, and add a test for it (and
for the normal, working singleton case).

https://bugzilla.gnome.org/show_bug.cgi?id=661576
2013-10-22 11:01:15 -04:00
Nick Schermer
4b334ef8f1 gobject: Handle ref_count==0 in notify_by_pspec
Just like g_object_notify, check for a zero ref_count in
g_object_notify_by_pspec and leave if it is 0.

This allows using functions in ->finalize() that possibly also
notify a property change on the object.  Previously,
this resulted in an error from g_object_ref.

https://bugzilla.gnome.org/show_bug.cgi?id=705570
2013-08-14 18:11:05 +01:00
Ryan Lortie
c1e32a5c59 GObject: turn add-property-after-init to a warning
We have turned up enough cases of this being done (including GTK API
allowing apps to do this to GtkSettings well after it has been
instantiated) that it is clear that we cannot really break this feature
while claiming to be backwards compatible.

For that reason, it becomes a warning rather than a critical (ie: it is
still well-defined behaviour, but you are discouraged from doing it).

The intention is to keep this feature for at least the next while.
A given GObjectClass will be able to avoid using GParamSpec pool for as
long as you don't install properties after init.  If you do that, you
will get a warning and we will devolve to using GParamSpecPool.

https://bugzilla.gnome.org/show_bug.cgi?id=698614
2013-06-10 11:18:06 -04:00
Ryan Lortie
a8a9afe17c GObject: prevent installing properties after init
GObject has previously allowed installing properties after class_init
has finished running.  This means that you could install some of your
own properties on G_TYPE_OBJECT, for example, although they wouldn't
have worked properly.

A previous patch asserted that this was not true and we had to revert it
because it broke the shell.  Instead of reverting, we should have used a
critical, so do that now.

Complaints go to this bug:

https://bugzilla.gnome.org/show_bug.cgi?id=698614
2013-05-29 09:25:25 -04:00
Matthias Clasen
2349635ebe Trivial documentation typos 2013-05-29 08:37:57 -04:00
Dan Winship
4b94c0831e Use 'dumb quotes' rather than `really dumb quotes'
Back in the far-off twentieth century, it was normal on unix
workstations for U+0060 GRAVE ACCENT to be drawn as "‛" and for U+0027
APOSTROPHE to be drawn as "’". This led to the convention of using
them as poor-man's ‛smart quotes’ in ASCII-only text.

However, "'" is now universally drawn as a vertical line, and "`" at a
45-degree angle, making them an `odd couple' when used together.

Unfortunately, there are lots of very old strings in glib, and also
lots of new strings in which people have kept up the old tradition,
perhaps entirely unaware that it used to not look stupid.

Fix this by just using 'dumb quotes' everywhere.

https://bugzilla.gnome.org/show_bug.cgi?id=700746
2013-05-21 11:23:22 -03:00
Dan Winship
c0e0c6a420 gobject: rename an unused parameter to make AIX happy 2013-05-02 13:58:25 -04:00
Ryan Lortie
7d61da0c07 g_object_new: check for NULL from _constructor()
There is some code in the wild (like in gnome-session) that does this
from its custom _constructor() implementation:

{
  GObject *obj;

  obj = ((chain up));

  if (!object_is_viable (obj))
    {
      g_object_unref (obj);
      return NULL;
    }
  else
    return obj;
}

This has never been a valid use of GObject and this code has always
caused memory to be leaked[1] by growing the construction_objects list.
The ability to legitimately return NULL from a constructor was exactly
the reason that we created GInitable, in fact.

That doesn't change the fact that the g_object_new() rewrite will crash
in this case, so instead of doing that, let's emit a critical and avoid
the crash.  This will allow people to upgrade their GLib without also
upgrading their gnome-session.  Meanwhile, people can fix their broken
code.

[1] not in the strictest sense of the word, because it's still reachable
2013-04-26 11:34:27 -04:00
Ryan Lortie
bfa8bef7b9 GObject: substantially rework g_object_new()
Make a number of improvements to g_object_new():

 - instead of looking up the GParamSpec for the named property once in
   g_object_new() (in order to collect) and then again in g_object_newv
   (when actually setting the property), g_object_new_internal() is a
   new function that takes the GParamSpec on the interface to avoid the
   second lookup

 - in the case that ->constructor() is not set, we need not waste time
   creating an array of GObjectConstructParam to pass in.  Just directly
   iterate the list of parameters, calling set_property() on each.

 - instead of playing with linked lists to keep track of the construct
   properties, realise that the number of construct properties that we
   will set is exactly equal to the length of the construct_properties
   list on GObjectClass and the only thing that may change is where the
   value comes from (in the case that it was passed in)

   This assumption was already implicit in the existing code and can be
   seen from the sizing of the array used to hold the construct
   properties, but it wasn't taken advantage of to make things simpler.

 - instead of allocating and filling a separate array of the
   non-construct properties just re-iterate the passed-in list and set
   all properties that were not marked G_PARAM_CONSTRUCT (since the ones
   that were construct params were already used during construction)

 - use the new g_param_spec_get_default_value() API instead of
   allocating and setting the GValue for each construct property that
   wasn't passed from the user

Because we are now iterating the linked list of properties in-order we
need to append to that list during class initialising instead of
prepending.

These changes show a very small improvement on the simple-construction
performance testcase (probably just noise) and they improve the
complex-construction case by ~30%.

Thanks to Alex Larsson for reviews and fixes.

https://bugzilla.gnome.org/show_bug.cgi?id=698056
2013-04-23 14:39:09 -04:00
Ryan Lortie
e8438f98e2 Revert "GObject: prevent installing properties after init"
This reverts commit ddb0ce1421.

Colin's smoke testing has found issues in at least gjs and
gnome-settings-daemon.  We'll need to see if we can address those.
2013-04-22 18:32:49 -04:00
Ryan Lortie
ddb0ce1421 GObject: prevent installing properties after init
GObject has previously allowed installing properties after class_init
has finished running.  This means that you could install some of your
own properties on G_TYPE_OBJECT, for example, although they wouldn't
have worked properly.

Prevent this from happening.  Require that all properties are installed by
the time class_init has finished.

Complaints go to this bug:

https://bugzilla.gnome.org/show_bug.cgi?id=698614
2013-04-22 17:40:51 -04:00
Sébastien Wilmet
e569079414 Doc: clarify a bit g_signal_connect_object()
"the object" can be a bit confusing for a beginner, he can think it is
the @instance.

https://bugzilla.gnome.org/show_bug.cgi?id=695887
2013-03-15 09:07:29 +01:00
Kjell Ahlstedt
269f890474 GObject: Fix code snippet in description of floating reference.
https://bugzilla.gnome.org/show_bug.cgi?id=688180
2012-11-21 18:09:46 -05:00
Ryan Lortie
5272bc4d67 Revert "[gobject] set all properties before constructed()"
This reverts commit 028d4a03f2.

I thought that we would be able to get away with this incompatible
change but it appears to impact far too much existing code.  The only
thing we can do is revert.

https://bugzilla.gnome.org/show_bug.cgi?id=688596
2012-11-19 10:30:47 -05:00
Ryan Lortie
028d4a03f2 [gobject] set all properties before constructed()
Move the constructed() call to happen after all of the properties are
set (not just the construct properties).

This is an incompatible change but we are making it under the belief
that it should be safe.  If this change impacts you in a negative way
please comment on the bug.

https://bugzilla.gnome.org/show_bug.cgi?id=685733
2012-10-08 11:40:00 -04:00
Ryan Lortie
8fd75705f4 fix g_signal_connect_object() documentation
g_signal_connect_object() now works properly, so we can remove the note
in the docs about it being broken.

https://bugzilla.gnome.org/show_bug.cgi?id=118536
2012-10-08 11:22:04 -04:00
Matthias Clasen
1254ca716b Add an atomic compare-and-exchange operation for object data
This is useful when using object data in thread-safe libraries.

https://bugzilla.gnome.org/show_bug.cgi?id=682849
2012-09-02 15:09:13 -04:00
Philip Withnall
f642209ef4 gobject: Document notify signal deduplication with freeze/thaw
Expand the documentation for g_object_[freeze|thaw]_notify() to explain that
it deduplicates “notify” signals emitted by frozen objects, so that at most
one signal is emitted per property.

https://bugzilla.gnome.org/show_bug.cgi?id=676937
2012-05-28 15:47:07 +01:00
Xavier Claessens
00285b7517 Add g_clear_pointer()
Also reimplement g_clear_object() using g_clear_pointer()

https://bugzilla.gnome.org/show_bug.cgi?id=674634
2012-04-27 09:42:29 +02:00
Dan Winship
ab59739e11 gobject: Use a destructor rather than g_atexit() for refcount debugging
https://bugzilla.gnome.org/show_bug.cgi?id=669671
2012-02-15 09:54:38 -05:00
Matthias Clasen
99c166501a Fix parameter name mismatches in GWeakRef api 2012-01-03 10:48:50 -05:00
Ryan Lortie
46c2f570da GWeakRef: add a weak GObject reference believed to be thread-safe
This patch is a joint work with Simon McVittie.

https://bugzilla.gnome.org/show_bug.cgi?id=548954
2012-01-02 12:23:15 -05:00
Simon McVittie
28c87a5594 g_object_weak_ref, g_object_add_weak_pointer: document non-thread-safety
Transparent access to a weak pointer from the thread performing the
weak -> strong conversion is incompatible with thread-safety: that
thread will have to do something special. This is GNOME#548954.

https://bugzilla.gnome.org/show_bug.cgi?id=548954
2012-01-02 12:23:11 -05:00
Ryan Lortie
557da16507 GObject: do checks on interface property install
Add some checks to g_object_interface_install_property() similar to
those in g_object_class_install_property().

https://bugzilla.gnome.org/show_bug.cgi?id=666616
2011-12-20 19:45:57 -05:00
Ryan Lortie
b237187109 GObject: require READ or WRITE on property install
g_object_class_install_property() currently lets you install properties
that are neither readable nor writable.  Add a check to prevent that.

https://bugzilla.gnome.org/show_bug.cgi?id=666616
2011-12-20 19:43:21 -05:00
Ryan Lortie
4e793c2eef GObject: allow G_PARAM_CONSTRUCT on any override
We were previously preventing implementations of an interface from
specifying G_PARAM_CONSTRUCT for a property of that interface if the
interface didn't specify it itself (or was readonly).

This is something that should only interest the implementation, so we
remove this restriction.

This allows 6 new possible override scenarios:

 - writable                 -> writable/construct
 - writable                 -> readwrite/construct
 - readwrite                -> readwrite/construct
 - writable/construct-only  -> writable/construct
 - writable/construct-only  -> readwrite/construct
 - readwrite/construct-only -> readwrite/construct

and we update the testcase to reflect this.

https://bugzilla.gnome.org/show_bug.cgi?id=666616
2011-12-20 19:40:44 -05:00
Ryan Lortie
d8d78688a8 GObject: change the order of property checks
Change the order of the checks so that we hear about the 'biggest'
problem first.  Also, stop reporting problems after we report the first
one for a particular property.

Add some comments.

https://bugzilla.gnome.org/show_bug.cgi?id=666616
2011-12-20 19:18:25 -05:00
Ryan Lortie
5fb7a8e127 GObject: fix property override type checks
The property override typecheck was meant to enforce the type on the
overriding property being exactly equal to the type on the interface
property.  Instead, g_type_is_a() was incorrectly used.

We could try to enforce equality, but if a property is read-only then it
should be possible for the implementation to type the property with any
subtype of the type specified on the interface (because returning a more
specific type will still satisfy the interface).  Likewise, if the
property is write-only then it should be possible for the implementation
to type the property with any supertype.

We implement the check this way.

https://bugzilla.gnome.org/show_bug.cgi?id=666616
2011-12-20 19:18:25 -05:00
Ryan Lortie
958f2bac7a GObject: fixup reversed logic in last commit
Accidentally dropped a !.
2011-12-20 15:29:16 -05:00
Ryan Lortie
3af050f6fc gobject: Clean up logic in property checks
Simplify some of the logic in this function.

  1) Simplify flag checks as per Colin's suggestions in
     https://bugzilla.gnome.org/show_bug.cgi?id=605667

  2) Don't repeatedly recheck if class_pspec is NULL.
2011-12-20 15:12:44 -05:00
Ryan Lortie
af24dbc12a gobject: loosen property override flag restrictions
GObject enforces the following restrictions on property overrides:

  - must only add abilities: if the parent class supports
    readability/writability then the subclass must also support them.
    Subclasses are free to add readability/writability.

  - must not add additional restrictions: if the parent class doesn't
    have construct/construct-only restrictions then the subclass must
    not add them.  Subclasses are free to remove restrictions.

The problem with the previous implementation is that the check against
adding construct/construct-only restrictions was being done even if the
property was not previously writable.  As an example:

  "readable" and "writable only on construct"

was considered as being more restrictive than

  "read only".

This patch tweaks the check to allow the addition of
construct/construct-only restrictions for properties that were
previously read-only and are now being made writable.

https://bugzilla.gnome.org/show_bug.cgi?id=666615
2011-12-20 14:58:38 -05:00
Matthias Clasen
e50d8a11b2 Cosmetic doc change 2011-12-09 08:01:12 -05:00
Nicola Fontana
f24d8247b3 Do not use static GTypeInfo and GInterfaceInfo
Either g_type_register_static_simple (used by G_DEFINE_TYPE_EXTENDED)
and G_IMPLEMENT_INTERFACE use automatic variables for GTypeInfo and
GInterfaceInfo structs, while tutorials and source code often use
static variables. This commit consistently adopts the former method.

https://bugzilla.gnome.org/show_bug.cgi?id=600161
2011-11-29 22:03:25 -05:00
Matthias Clasen
2da24259ab Fix !debug builds
As pointed out by Chun-wei Fan in bug 664455, parts of the merged
gobjectnotifyqueue.c code ended up inside an #ifdef G_ENABLE_DEBUG
section.
2011-11-22 19:25:03 -05:00
Ryan Lortie
877c0ad5b8 [notify] remove some rather bogus 'inline' use 2011-11-16 17:50:13 +00:00
Ryan Lortie
ac0ddcf23f [notify] dispatch 'notify' directly if not frozen
Avoid the notify queue in the case that we're not already frozen.
2011-11-16 17:50:13 +00:00
Ryan Lortie
39458748dd [notify] add 'conditional' to _notify_queue_freeze
When the 'conditional' parameter is TRUE, the queue will only be frozen
(ie: have its freeze count increase by one) if it is already frozen.

This will allow us to avoid a freeze-notify-thaw in the case that we
just want to notify on a single property.

Another approach may have been to add an is_frozen() type call and avoid
even increasing the freeze count at all in this case.  Unfortunately,
I'm not totally sure what is the exact expected semantics of
simultaneous notifications in multiple threads and this may interact
badly with someone freezing or thawing in between our check and
emission.
2011-11-16 17:50:13 +00:00
Ryan Lortie
393d4c28b4 [notify] Remove GObjectNotifyContext indirection
This silly abstraction is causing more trouble than it's worth -- just
use the relevant bits directly.
2011-11-16 17:50:13 +00:00
Ryan Lortie
8215fc5f25 [notify] lift some logic out of _notify_queue_add
Lift the check-if-READABLE and redirect-target logic from out of
g_object_notify_queue_add() into its own function, get_notify_pspec().

Use that function at the site of our two calls to
g_object_notify_queue_add().
2011-11-16 17:50:12 +00:00
Ryan Lortie
760037ec46 [notify] remove an obviously false comment 2011-11-16 17:50:12 +00:00
Ryan Lortie
45d80cf9bd [notify] lift some code outside of critical region 2011-11-16 17:50:12 +00:00
Ryan Lortie
1d98f93194 [notify] drop some unused code 2011-11-16 17:50:12 +00:00
Ryan Lortie
128862eafe [notify] merge gobjectnotifyqueue.c into gobject.c
This was done as a separate file before, and #include brought it into
gobject.c.  That's a bit mad, so stop doing that.

Unfortunately, the insanity steps up a level: gobjectnotifyqueue.c is
installed in the public include dir, so we can't just get rid of it
entirely.
2011-11-16 17:50:12 +00:00
Ryan Lortie
688059cb94 [notify] remove some useless forward declarations
These cause trouble in the next commit.
2011-11-16 17:50:12 +00:00
Marc-André Lureau
fb95c20c96 Add G_SIGNAL_DEPRECATED
Similar to G_PARAM_DEPRECATED. It will warn only for users of the
signals, so a signal can still be emited without warning, for
compatibility reasons.

Apparently, there is no way user flags could have been used before,
so that shouldn't break anyone.

https://bugzilla.gnome.org/show_bug.cgi?id=663581
2011-11-07 23:12:45 +01:00
Javier Jardón
108480b7ba gobject: Use G_VALUE_INIT 2011-10-18 17:12:33 +01:00
Ryan Lortie
9829d04be8 GObject docs: resolve broken links
Some links were broken due to typos, because functionality was removed
in GLib 2.0 or for various other reasons.  Fix up as many of them as is
reasonable.
2011-09-05 18:46:59 -04:00
Matthias Clasen
1b28408b8b Spelling fixes
Spelling fixes in comments and docs, provided by
Kjartan Maraas in bug 657336.
2011-08-29 14:49:32 -04:00
Pavel Holejsovsky
d4c5e3b156 Add (allow-none) annotation for GValue setters.
Assorted g_value_set_xxx() and g_value_take_xxx() functions actually
allow NULL in source parameter.
2011-08-17 12:55:12 +02:00