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 661469 - An implemented method that returns a boxed this boxed is managed wrongly
An implemented method that returns a boxed this boxed is managed wrongly
Status: RESOLVED OBSOLETE
Product: pygobject
Classification: Bindings
Component: introspection
3.0.x
Other Linux
: Normal normal
: ---
Assigned To: Nobody's working on this now (help wanted and appreciated)
Python bindings maintainers
: 661470 661471 (view as bug list)
Depends on:
Blocks:
 
 
Reported: 2011-10-11 15:20 UTC by Ignacio Casal Quinteiro (nacho)
Modified: 2018-01-10 20:12 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description Ignacio Casal Quinteiro (nacho) 2011-10-11 15:20:39 UTC
See: http://git.gnome.org/browse/gedit/commit/?id=9f6ea03608f6afc216bec243249d7fb170ff0b6d

If I print the iter before returning the iter is correct, if I print it from gtksourceview right after this iter is get, the iter is invalid. I guess this is a problem in pygobject managing boxed types.
Comment 1 André Klapper 2011-10-11 18:48:16 UTC
*** Bug 661471 has been marked as a duplicate of this bug. ***
Comment 2 André Klapper 2011-10-11 18:48:20 UTC
*** Bug 661470 has been marked as a duplicate of this bug. ***
Comment 3 johnp 2011-10-12 15:31:28 UTC
Are iters actually boxed?  I thought they were structs.
Comment 4 Ignacio Casal Quinteiro (nacho) 2011-10-12 15:32:28 UTC
seems so:

from the docs:
  GBoxed
   +----GtkTextIter
Comment 5 Ignacio Casal Quinteiro (nacho) 2011-10-12 15:32:57 UTC
Also see http://git.gnome.org/browse/gtk+/tree/gtk/gtktextiter.c#n469
Comment 6 johnp 2011-10-14 21:48:08 UTC
Iters are containers with arbitrary gpointers.  There is no way to support them generically and we can't put a dependency on Gtk.  Someone needs to think deeply about what an iter is and how it can be consistently supported in scripting languages.  Until then setting iters are never going to work.
Comment 7 Paolo Borelli 2011-10-16 17:33:23 UTC
forgive my ignorance, but I do not understand why iters are different from any other boxed types: in the commit nacho linked in comment #1 the iter is correct before returning from the callee and is corrupted when inspected from the caller.

I would have expected that pygobject figures out it is a boxed type and uses the gboxed copy/free methods to handle it, without having to care what is inside...
Comment 8 johnp 2011-10-17 18:19:17 UTC
The interface is passing the iter in as caller-allocated.  So an iter is passed in that needs to filled in (not assigned).  The problem is that the iter is just a struct of pointers which have no context in GI.  We can not set them because they can't be memory managed.  PyGTK got around this by leaking python objects.  It would set the pointer to the python object and Py_INCREF it with no knowledge of when the iter goes away (most iters are are statically created as a local variable in the calling code).  That isn't to say they can't be supported but someone has to think about the best way to support them in all cases without putting a dependency on Gtk.

One quick thought would be to create a pluggable iter interface in PyGObject which keeps track of the current outstanding iters and the data encapsulated in them.  It would then periodically unref those pieces of data and change the timestamp on the owning model to invalidate the iters.  Since iters are supposed to be short lived within a single scope (they aren't supposed to be stored and reference in a later code block) this should work fine.  It is just a complicated patch that needs someone to work out all of the details.
Comment 9 Paolo Borelli 2011-10-19 14:40:43 UTC
(In reply to comment #8)
> The interface is passing the iter in as caller-allocated.  So an iter is passed
> in that needs to filled in (not assigned).  The problem is that the iter is
> just a struct of pointers which have no context in GI.  We can not set them
> because they can't be memory managed.

This is the part I do not understand: in this case python does not need to fill the structure itself, python calls a gtk api and the struct is filled by gtk in C
Comment 10 johnp 2011-10-19 16:09:23 UTC
(In reply to comment #9)
> (In reply to comment #8)
> > The interface is passing the iter in as caller-allocated.  So an iter is passed
> > in that needs to filled in (not assigned).  The problem is that the iter is
> > just a struct of pointers which have no context in GI.  We can not set them
> > because they can't be memory managed.
> 
> This is the part I do not understand: in this case python does not need to fill
> the structure itself, python calls a gtk api and the struct is filled by gtk in
> C

Yep that should work fine but inevitably people want to do things like iter = (1,1).  That is where we run into issues.  Theoretically it should work provided we build a framework around iters which explicitly manages their lifecycle by invalidating the model when the wrapped python object is destroyed.
Comment 11 johnp 2011-10-19 16:14:44 UTC
BTW we can't assign pointer fields though as there is little context on what to store in that field.  For instance if you try to assign an integer does that get converted to a pointer?  What about a PyGObject?  Do we get the wrapped pointer oe assign the python pointer for the wrapper itself?

For caller allocates we would have to have a iter_assign method for this to work.
Comment 12 Sebastian Pölsterl 2012-04-23 18:56:59 UTC
Could someone please provide a minimal test case? Mabye this will help reviving this issue.
Comment 13 Tobias Mueller 2012-09-13 16:51:16 UTC
hm. I am reopening as I can't see any open non developer question. And I'm setting to NEW as this has been confirmed.
Comment 14 Simon Feltman 2012-09-13 17:57:57 UTC
Linked this to a couple tickets:
 https://bugzilla.gnome.org/show_bug.cgi?id=668356
This was a patch that went in to work with gpointers as if they are pointers to python objects which will leak as John describes. 

I recently opened a rant ticket about this here:
 https://bugzilla.gnome.org/show_bug.cgi?id=683599
I think Johns comments are right on. The only thing I think gpointers should ever be interpreted as in pygobject are ints, this at least removes reference leaks and potential crash dangers. It is then left up to the application developer to interpret the int however they like. For instance, using it as an integer key into a pool of properly ref counted python object in the higher level structure that manages the iters.
Comment 15 Simon Feltman 2013-04-11 01:58:46 UTC
Can someone provide a way to reproduce this? Minimal code snippet is preferred but just instructions for how to notice this in the gedit plugin would be helpful.
Comment 16 Simon Feltman 2014-09-27 22:31:32 UTC
I believe the problem is that GtkTextIter requires using gtk_text_iter_assign() for copying memory. PyGI uses memcopy to overwrite caller allocated out argument memory with the boxed object returned from the Python function (this has always seemed a bit sketchy to me in general).

Possible fixes:

1) Annotate the text iter as an input argument and formalize pass-by-ref semantics for boxed arguments to vfuncs in PyGI. This basically means removing the special casing with pass-by-ref from bug 734465 and using it for any vfunc/callback boxed input arg. And also adding the copy-if-stored hack from bug 736175.

2) Add something like g_boxed_assign() and corresponding callback registration to GLib which would allow generic support for boxed assignment to existing memory. This might also allow us to remove special casing of GValue argument marshaling because the fundamental is GBoxed and could use g_value_copy() as the assign function.
Comment 17 GNOME Infrastructure Team 2018-01-10 20:12:46 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to GNOME's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/pygobject/issues/21.