2015-05-29

[Technology] Memory leaks, vala, and copying delegates

Some of the Gee data structures, like a TreeSet, can get a comparator, a GLib.CompareDataFunc comparator.

If I define a compliant static function and hand it over to the TreeSet's constructor, I get a warning that copying delegates is not supported. I can't really change how TreeSet handles it, and I couldn't find how I was supposed to pass it elsewhere, so I passed it over like this (not exact code):

You see, I needed different comparators for different subclasses, but I couldn't just hand over static functions. However, somehow, passing a function instantiated in this manner to new TreeSet () resulted in an increase to the reference count for pie! And, in fact, I had 10 different collections that made use of 3 different comparators like this. And I went through 10,000s of pies! I think you can start to see my problem.

The functions were causing reference cycles so none of my pies were being freed when I was finished with them, and I quickly ran out of memory. :D Hooray!

Right now, I'm doing what I'm not supposed to, and passing static functions (copying delegates) to the new TreeSet constructor (they're passed from the subclass to the superclass through base (), so that might be a part of the problem) and things are working alright.

I'm grateful for Malloc.mallinfo ().uordblks, and for GLib.Object.ref_count for helping me see into problem today.