Question

I am creating a UI using GTK3 (from gi.repository) and python3. When I add default icons to the UI and then run the program it crashes with the fellowing error:

segmentation fault (core dumped) python main.py

I am adding the icon with the set_icon_list method of Gtk.Window:

self.c_win.set_icon_list(icon_list)

If I comment this line, the program runs as intended. I get the icon list with the function below:

def load_icon():
    req = pkg_resources.Requirement.parse("pympress")

   # If pkg_resources fails, load from directory
   try:
       icon_names = pkg_resources.resource_listdir(req, "share/pixmaps")
    except pkg_resources.DistributionNotFound:
       icon_names = os.listdir("share/pixmaps")
    icons = []
    for icon_name in icon_names:
       if os.path.splitext(icon_name)[1].lower() != ".png":
           continue

        # If pkg_resources fails, load from directory
        try:
            icon_fn = pkg_resources.resource_filename(req, "share/pixmaps/{}".format(icon_name))
        except pkg_resources.DistributionNotFound:
            icon_fn = "share/pixmaps/{}".format(icon_name)
        try:
            icon_pixbuf = Pixbuf()
            icon_pixbuf.new_from_file(icon_fn)
            icons.append(icon_pixbuf)
        except Exception as e:
            print(e)
    return icons

It returns a list of Pixbuf which is the intended input of set_icon_list.

The full code is available on github: https://github.com/Jenselme/pympress Any idea what the problem is ?

Was it helpful?

Solution

Although it shouldn't crash, part of the problem might be due to the way new_from_file() is being used. new_from_file() is a constructor which returns a new pixbuf which you should store in a variable. It does not load the contents of the file into an existing pixbuf. So the "icons" list actually contains a bunch of empty (or rather 1x1) pixbufs.

# Creates a new 1x1 pixbuf.
icon_pixbuf = Pixbuf()

# Creates a new pixbuf from the file the value of which is lost
# because there is no assignment.
icon_pixbuf.new_from_file(icon_fn)

# Stores the first 1x1 pixbuf in the list.
icons.append(icon_pixbuf)

What you really want is:

icon_pixbuf = Pixbuf.new_from_file(icon_fn)
icons.append(icon_pixbuf)

In any case, it should not segfault. Please log it as a bug with a minimal code example which causes the crash: https://bugzilla.gnome.org/enter_bug.cgi?product=pygobject

Also note the version of gi and GTK+ being used:

import gi
from gi.repository import Gtk
print(gi.version_info)
print(Gtk.MINOR_VERSION)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top