Posts Tagged ‘gtk+’

Articles

Writing small GUI applications on Linux

In Gnome shell,gtk+,Programming on November 14, 2020 by oli4444 Tagged: , ,

Last years I had little time for programming. Most of that time was spent maintaining Bluefish and Jailkit. Last month I wanted to write some small utilities.

First utility I wrote was a gnome-shell extension to track how the time on the computer was spent. The extension polls the current active application and it’s window title every 10 seconds and appends that to a logfile. Although I have some experience with Javascript in websites, I found it very hard. Hardest was to find examples and documentation. A simple thing like how to append a single line to a logfile took me a lot of time.

Next utility I needed was a tool to parse the window activity logfile and show useful statistics. Because of the bad experience with Javascript I choose Python. I didn’t use Glade for years, but I decided to use it for the GUI. I was very surprised with the state of Python Gtk bindings and the combination of Python and Glade. It was excellent to see how quick one can write an application with the combination of python and glade. The documentation on Python and Gtk is good (https://python-gtk-3-tutorial.readthedocs.io/en/latest/) which helps. It surprises me that Glade is mentioned only in chapter 21 of that tutorial. I would have made it chapter 2, and I would have uses it everywhere else in the tutorial.

Because the combination worked very well for me, I also used it for my next utility: saving laptop batteries. Most of my family members keep their laptops always connected to the power at home (which currently is most of the time). I have a few Sonoff switches around with self-written firmware that I can switch on/off with a JSON call. The laptops now automatically switch off their power when their batteries are >95% charged, and switch on the power when their batteries reach < 10%. Then I improved the utility to read the status of the smart meter in house, so now the laptops prefer to charge when I have excess power from my solar panels.

I used pydbus to read the battery state from the system dbus. Paho MQTT was used to read the status of my Smart meter (I have a Raspberry Pi connected to it’s serial port that puts the values on an MQTT bus). Normal python libraries like requests for the JSON call and configparser for config file loading/saving. And again the combination of Glade with Gobject introspection for Gtk and Libnotify and I got everything working in < 250 lines of code. Very happy with the result 🙂

Statuses

Debugging a reference count bug

In Bluefish,gtk+,open source,Programming,Ubuntu on February 5, 2012 by oli4444 Tagged:

Last days I have been debugging some weird reports. They all show the same characteristics:

  • the users are on Ubuntu 11.10
  • they use bluefish compiled against gtk 3.2 (so not the bluefish package that is provided by Ubuntu, but a newer one)
  • in the Bluefish run the sort function of a GtkTreeModelSort is called after the GtkTreeModelSort should have been finalized and free’ed.

First I used gobject-list.c from http://people.gnome.org/~mortenw/gobject-list.c to see all refs and unrefs on all GtkTreeModelSort objects in Bluefish (luckily there is only 1 used in Bluefish).This showed that there was indeed a GtkTreeModelSort with lots of references left after it should have been finalized. I tried the same thing on Fedora 16 (also gtk-3.2), but it can only be reproduced on Ubuntu 11.10.I tried to get backtraces with gobject-list (which uses libunwind for that) but those backtraces turned out to be useless.

Luckily I received some help on IRC #gtk+ from Company and alex. The first idea was to use systemtap, but since there is no useful kernel for systemtap available for Ubuntu I had to use something more low tech suggested by Company:  I set a breakpoint on gtk_tree_model_sort_new to retrieve the pointer of the GtkTreeModelSort. Once I got that pointer I could set a breakpoint on g_object_ref and g_object_unref with a condition on this pointer. Then I created an automatic backtrace on each breakpoint:

break g_object_ref if object == 0x123123123
commands
bt
c
end

I configured gdb to log everrything to a file, and did a bluefish run. This resulted in a 2.1 Mb logfile with backtraces. This log also showed there were more refs than unrefs.

In this logfile there were a lot of similar backtraces, with an identical function doing a ref and an unref. I wrote a short python script to parse the backtraces and skip all ‘valid pairs’

After this step I had only 15 backtraces left. And from these backtraces the leaking references were easily identified.

Because I was unsure if this is a Ubuntu specific bug or a generic gtk bug the resulting bugreport can be found both at https://bugzilla.gnome.org/show_bug.cgi?id=669376 and at https://bugs.launchpad.net/bugs/926889

Now I am wondering if this approach would work for any reference count leaking problem. I guess the most difficult issue is to find the value of the pointer that is leaking if you have many objects of the same type.. Any suggestions how to do this?