GNOME Bugzilla – Bug 675332
Crash in Gtk.TextView().get_pango_context().get_font_description().get_family()
Last modified: 2012-07-20 03:38:14 UTC
I am writing a text editor in python 3.2.1 with gobject-introspection. When I try to get the default font family from a text view, the text view crashes. Here is what happens: >>> Gtk.TextView().get_pango_context().get_font_description().get_family() Traceback (most recent call last):
+ Trace 230173
return info.invoke(*args, **kwargs)
>>> Gtk.TextView().get_pango_context().get_font_description().get_family() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python3.2/site-packages/gi/types.py", line 43, in function return info.invoke(*args, **kwargs) UnicodeDecodeError: 'utf8' codec can't decode byte 0xa0 in position 0: invalid start byte >>> *** glibc detected *** python3: corrupted double-linked list: 0x001e6c68 *** ======= Backtrace: ========= /lib/libc.so.6[0x486a7f12] /lib/libc.so.6[0x486a925d] /lib/libglib-2.0.so.0[0x488c90cc] /lib/libglib-2.0.so.0(g_free+0x37)[0x488c93c7] /usr/lib/libpango-1.0.so.0(pango_font_description_free+0x50)[0x4905a6e0] /lib/libgobject-2.0.so.0[0x48a1b9e2] /lib/libgobject-2.0.so.0(g_boxed_free+0x114)[0x489f39a4] /usr/lib/python3.2/site-packages/gi/_gi.so(+0xd326)[0x269326] /usr/lib/libpython3.2mu.so.1.0[0x4107943e] /usr/lib/libpython3.2mu.so.1.0[0x41075f9b] /usr/lib/libpython3.2mu.so.1.0[0x41054406] /usr/lib/libpython3.2mu.so.1.0[0x410eb6a9] /usr/lib/libpython3.2mu.so.1.0[0x410eb6b9] /usr/lib/libpython3.2mu.so.1.0[0x410643f9] /usr/lib/libpython3.2mu.so.1.0(PyDict_SetItem+0x7a)[0x410660fa] /usr/lib/libpython3.2mu.so.1.0(PyDict_SetItemString+0x4c)[0x41067b7c] /usr/lib/libpython3.2mu.so.1.0(PyImport_Cleanup+0x165)[0x410d5a75] /usr/lib/libpython3.2mu.so.1.0(Py_Finalize+0x12a)[0x410e1e7a] /usr/lib/libpython3.2mu.so.1.0(Py_Main+0x488)[0x410f55b8] python3(main+0x144)[0x80487b4] /lib/libc.so.6(__libc_start_main+0xf3)[0x4864a6b3] python3[0x8048841] ======= Memory map: ======== 00179000-00184000 r-xp 00000000 fd:01 164426 /usr/lib/libpangocairo-1.0.so.0.2904.0 00184000-00185000 r--p 0000a000 fd:01 164426 /usr/lib/libpangocairo-1.0.so.0.2904.0 00185000-00186000 rw-p 0000b000 fd:01 164426 /usr/lib/libpangocairo-1.0.so.0.2904.0 00186000-001a8000 r-xp 00000000 fd:01 164432 /usr/lib/libgdk_pixbuf-2.0.so.0.2400.1 001a8000-001a9000 r--p 00021000 fd:01 164432 /usr/lib/libgdk_pixbuf-2.0.so.0.2400.1 001a9000-001aa000 rw-p 00022000 fd:01 164432 /usr/lib/libgdk_pixbuf-2.0.so.0.2400.1 001aa000-001d6000 r-xp 00000000 fd:01 164425 /usr/lib/libpangoft2-1.0.so.0.2904.0 001d6000-001d7000 r--p 0002b000 fd:01 164425 /usr/lib/libpangoft2-1.0.so.0.2904.0 001d7000-001d8000 rw-p 0002c000 fd:01 164425 /usr/lib/libpangoft2-1.0.so.0.2904.0 001d8000-001d9000 r-xp 00000000 fd:01 156960 /usr/lib/gdk-pixbuf-2.0/2.10.0/loaders/libpixbufloader-svg.so 001d9000-001da000 r--p 00001000 fd:01 156960 /usr/lib/gdk-pixbuf-2.0/2.10.0/loaders/libpixbufloader-svg.so 001da000-001db000 rw-p 00002000 fd:01 156960 /usr/lib/gdk-pixbuf-2.0/2.10.0/loaders/libpixbufloader-svg.so 001db000-001e0000 r-xp 00000000 fd:01 39048 /usr/lib/gtk-3.0/modules/libcanberra-gtk3-module.so 001e0000-001e1000 r--p 00004000 fd:01 39048 /usr/lib/gtk-3.0/modules/libcanberra-gtk3-module.so 001e1000-001e2000 rw-p 00005000 fd:01 39048 /usr/lib/gtk-3.0/modules/libcanberra-gtk3-module.so 001e2000-00201000 r-xp 00000000 fd:01 921401 /usr/lib/python3.2/site-packages/gi/_gobject/_gobject.so 00201000-00202000 r--p 0001e000 fd:01 921401 /usr/lib/python3.2/site-packages/gi/_gobject/_gobject.so 00202000-00204000 rw-p 0001f000 fd:01 921401 /usr/lib/python3.2/site-packages/gi/_gobject/_gobject.so 00204000-00237000 r-xp 00000000 fd:01 163959 /usr/lib/libfontconfig.so.1.4.4 00237000-00238000 rw-p 00033000 fd:01 163959 /usr/lib/libfontconfig.so.1.4.4 0024e000-00254000 r-xp 00000000 fd:01 663593 /usr/lib/python3.2/lib-dynload/_struct.cpython-32mu.so 00254000-00256000 rw-p 00006000 fd:01 663593 /usr/lib/python3.2/lib-dynload/_struct.cpython-32mu.so 0025c000-00279000 r-xp 00000000 fd:01 921391 /usr/lib/python3.2/site-packages/gi/_gi.so 00279000-0027a000 r--p 0001c000 fd:01 921391 /usr/lib/python3.2/site-packages/gi/_gi.so 0027a000-0027c000 rw-p 0001d000 fd:01 921391 /usr/lib/python3.2/site-packages/gi/_gi.so 0027c000-002a3000 r-xp 00000000 fd:01 141510 /usr/lib/libpng12.so.0.49.0 002a3000-002a4000 r--p 00026000 fd:01 141510 /usr/lib/libpng12.so.0.49.0 002a4000-002a5000 rw-p 00027000 fd:01 141510 /usr/lib/libpng12.so.0.49.0 002a5000-002cb000 r-xp 00000000 fd:01 1943 /lib/libexpat.so.1.6.0 002cb000-002cc000 ---p 00026000 fd:01 1943 /lib/libexpat.so.1.6.0 002cc000-002ce000 r--p 00026000 fd:01 1943 /lib/libexpat.so.1.6.0 002ce000-002cf000 rw-p 00028000 fd:01 1943 /lib/libexpat.so.1.6.0 002cf000-002f9000 r-xp 00000000 fd:01 156616 /usr/lib/gio/modules/libgvfsdbus.so 002f9000-002fa000 r--p 00029000 fd:01 156616 /usr/lib/gio/modules/libgvfsdbus.so 002fa000-002fb000 rw-p 0002a000 fd:01 156616 /usr/lib/gio/modules/libgvfsdbus.so 002fd000-002ff000 r-xp 00000000 fd:01 663595 /usr/lib/python3.2/lib-dynload/atexit.cpython-32mu.so 002ff000-00300000 rw-p 00001000 fd:01 663595 /usr/lib/python3.2/lib-dynload/atexit.cpython-32mu.so 00300000-0032b000 r-xp 00000000 fd:01 146209 /usr/lib/libbluray.so.0.0.0 0032b000-0032c000 r--p 0002b000 fd:01 146209 /usr/lib/libbluray.so.0.0.0 0032c000-0032d000 rw-p 0002c000 fd:01 146209 /usr/lib/libbluray.so.0.0.0 00365000-00369000 r-xp 00000000 fd:01 663609 /usr/lib/python3.2/lib-dynload/readline.cpython-32mu.so 00369000-0036b000 rw-p 00003000 fd:01 663609 /usr/lib/python3.2/lib-dynload/readline.cpython-32mu.so 0036b000-0041f000 r-xp 00000000 fd:01 146517 /usr/lib/libcairo.so.2.11000.2 0041f000-00420000 r--p 000b4000 fd:01 146517 /usr/lib/libcairo.so.2.11000.2 00420000-00421000 rw-p 000b5000 fd:01 146517 /usr/lib/libcairo.so.2.11000.2 00421000-00423000 rw-p 00000000 00:00 0 004dc000-004dd000 r-xp 00000000 00:00 0 [vdso] 00552000-0055d000 r-xp 00000000 fd:01 1214 /lib/libnss_files-2.14.90.so 0055d000-0055e000 r--p 0000a000 fd:01 1214 /lib/libnss_files-2.14.90.so 0055e000-0055f000 rw-p 0000b000 fd:01 1214 /lib/libnss_files-2.14.90.so 007ec000-007ee000 r-xp 00000000 fd:01 44518 /usr/lib/gtk-3.0/modules/libpk-gtk-module.so 007ee000-007ef000 r--p 00002000 fd:01 44518 /usr/lib/gtk-3.0/modules/libpk-gtk-module.so 007ef000-007f0000 rw-p 00003000 fd:01 44518 /usr/lib/gtk-3.0/modules/libpk-gtk-module.so 0091a000-0091d000 r-xp 00000000 fd:01 163891 /usr/lib/libpyglib-gi-2.0-python3.so.0.0.0 0091d000-0091e000 r--p 00002000 fd:01 163891 /usr/lib/libpyglib-gi-2.0-python3.so.0.0.0 0091e000-0091f000 rw-p 00003000 fd:01 163891 /usr/lib/libpyglib-gi-2.0-python3.so.0.0.0 009ae000-009bd000 r-xp 00000000 fd:01 921396 /usr/lib/python3.2/site-packages/gi/_glib/_glib.so 009bd000-009be000 r--p 0000e000 fd:01 921396 /usr/lib/python3.2/site-packages/gi/_glib/_glib.so 009be000-009c0000 rw-p 0000f000 fd:01 921396 /usr/lib/python3.2/site-packages/gi/_glib/_glib.so 00b22000-00b24000 r-xp 00000000 fd:01 663566 /usr/lib/python3.2/lib-dynload/_bisect.cpython-32mu.soAborted (core dumped) Seems like a memory overflow problem.
This returns an empty string here with Python 3.2: >>> Gtk.TextView().get_pango_context().get_font_description().get_family() >>> With Python 2.7 I get some garbage: >>> Gtk.TextView().get_pango_context().get_font_description().get_family() '`\xce%\x01' The garbage changes with every invocation, so this indeed does look like bad memory handling. I cannot say yet whether that is a bug in Pango or in PyGObject. The other properties of the FontDescription seem to work with 3.2: >>> Gtk.TextView().get_pango_context().get_font_description().to_string() 'Normal 11' >>> Gtk.TextView().get_pango_context().get_font_description().to_filename() 'normal_11' >>> Gtk.TextView().get_pango_context().get_font_description().get_variant() <enum PANGO_VARIANT_NORMAL of type PangoVariant> >>> But with 2.7 I get garbage again with to_string() and to_filename(). What seems to help is to explicitly import the Pango module: from gi.repository import Pango Can you please add that before or after your import of Gtk and compare the result? Thanks!
I tried running the original command after I imported Pango, both before importing Gtk and after importing Gtk. There was no change. In addition, I tried running to_string and to_filename, and both also gave me the same error as before. However, get_variant worked as expected. I would like to note that I tested this on up-to-date versions of OpenSUSE 12.1, Ubuntu 12.04, and Fedora 16. They all gave the same results.
I tested that on Ubuntu 12.04 as well, so I wonder why it's so different for me. The "prints garbage" path seems sufficiently close to what you are seeing, but I wonder why it still does not work for you after importing Pango.
Could it be architecture? I'm running a 32-bit Ubuntu 12.04 through Virtualbox 4.1.14 on a Windows host (still need Win for development too). OpenSUSE 12.1 was also run through Virtualbox, but it is 64-bit, and Fedora (32-bit) was run bare metal on an (albeit, older) desktop machine. All three still had the same errors that I had observed earlier, despite when I imported Pango. Here is the test case plus the output from Ubuntu: >>> from gi.repository import Pango >>> from gi.repository import Gtk >>> Gtk.TextView().get_pango_context().get_font_description().to_string() Traceback (most recent call last):
+ Trace 230183
>>> Gtk.TextView().get_pango_context().get_font_description().to_string() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python3/dist-packages/gi/types.py", line 43, in function return info.invoke(*args, **kwargs) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb9 in position 1: invalid start byte >>> Gtk.TextView().get_pango_context().get_font_description().get_family() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python3/dist-packages/gi/types.py", line 43, in function return info.invoke(*args, **kwargs) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb4 in position 1: invalid start byte
*** Bug 679654 has been marked as a duplicate of this bug. ***
Confirmed by a duplicate, and the garbage looks sufficiently close to the crash.
For a working text editor (styles, coloring, undo/redo, ...) in python with gobject, see http://gramps.svn.sourceforge.net/viewvc/gramps/branches/geps/gep-029-gtk3/src/gui/widgets/styledtexteditor.py?view=log Still need to check if spell check can be made to work. Would be interesting if you check if it works in python 3 ;-) The workaround for this bug in python2.7 is to use a class method and obtain font families once. patchset: http://gramps.svn.sourceforge.net/viewvc/gramps/branches/geps/gep-029-gtk3/src/gui/widgets/styledtexteditor.py?r1=19859&r2=19964
Working with a recent git-master stack and Benny's work-around using the Quartz Gdk backend and Pango's CoreText backend, I found that the crash was from the family objects getting freed prematurely: Hardware watchpoint 2: *$1.family_name Old value = 76 'L' New value = 7 '\a' 0x91c1ea06 in tiny_free_list_add_ptr () (gdb) bt 24
+ Trace 230540
pango_context_list_families takes a PangoFamily*** and points it at a dynamic array of pointers to the family. The comments say that the array should be freed, but pygi is going a step beyond that and unreffing the pointers in the array, causing the actual family data to be freed as well. The next attempt to access the family data naturally crashes. To prove the concept I modified pango_core_text_font_map_list_families() to g_object_ref() the family* as it added them to the list, and that prevents the unwanted freeing. That's obviously not an acceptable solution as it would cause leaks in C programs using pango. I imagine that the real fix will need to be either an override or an annotation change so that python understands that it owns the container but not the contents.
Indeed this is the same root problem as in bug 678607 -- the TextView object is freed after calling get_pango_context(), as there is no further reference to it. If you do tv = Gtk.TextView() tv.get_pango_context().get_font_description().get_family() things work properly. *** This bug has been marked as a duplicate of bug 678607 ***