[Kde-bindings] [PATCH] Deferred objects and QGraphicsEffects

Paolo Capriotti p.capriotti at gmail.com
Wed Mar 17 00:24:56 UTC 2010


Hi all,
the attached patch solves two issues with QGraphicsEffects.
Background: a QGraphicEffect instance can be associated to a
QGraphicsItem by calling QGraphicsItem::setGraphicsEffect. When you do
that, QGraphicsItem takes ownership of the effect instance, and will
delete it in its destructor. The issues are:
1) When an item is deleted (say by the GC), the associated effect is
deleted as well, and Binding::deleted should take care of cleaning up
its smokeruby_object and unmap the pointer. Unfortunately, it may
happen that the effect has also been scheduled for finalization, and
it seems that in this case, ruby changes the type of the object (in
RBasic::flag) to T_DEFERRED, and invalidates the pointer. This implies
that we cannot use the VALUE stored in the pointer map to clean up.
What's currently happening is that the smokeruby_object doesn't get
set as deallocated (ptr = 0) as it should, and is then accessed when
the GC finally decides to call its finalizer => boom!
My proposed solution (in the patch) is to store both the
smokeruby_object _and_ the ruby VALUE in the map. So even if the VALUE
is invalidated, you can still access the smokeruby_object directly and
clean up. This fixes any instance of the crash I've seen.
2) The second issue is more straightforward: QGraphicsItem can
reference a QGraphicsEffect instance, so the latter needs to be marked
(in the mark phase of the GC) whenever the former is.

Please note that the first issue is not related only to
QGraphicsEffects. I think that in theory you could get a crash in any
situation when an object is deferred by ruby, deleted by Qt, and
finally finalized by qtruby, although I've never actually seen such a
crash except for the case with QGraphicsEffect.
To reproduce the crash, you can use the following code:

require 'Qt4'

loop do
  item = Qt::GraphicsItem.new
  effect = Qt::GraphicsEffect.new
  item.set_graphics_effect(effect)
end

Paolo
-------------- next part --------------
A non-text attachment was scrubbed...
Name: qtruby-effects.patch
Type: text/x-patch
Size: 4301 bytes
Desc: not available
URL: <http://mail.kde.org/pipermail/kde-bindings/attachments/20100317/77a1dc1b/attachment.patch>


More information about the Kde-bindings mailing list