How to create array or list of object instances

Hi all,

as I was trying to nail down the bug why my gtk4_list_clocks only ever update one of the clocks and not all of them, I’ve traced down the problem to be in GLib.SList.prepend().
Changing from GLib.SList to Gee.ArrayList didn’t solve the problem.
In the c code example a list of all clock widgets were created by appending them to a singly linked list. I’ve tried to mimic that but as it turned out the list size is always 1 and only one object ever get appended.

void start_ticking () {
        /* if no clock is ticking yet, start */
        if (ticking_clock_id == 0) {
            ticking_clock_id = GLib.Timeout.add_seconds (1, tick);
        ticking_clocks.add (instance); // Bug
        // Although instance pointer is different according to the number of instantiated objects!!!
        print ("Clock instance %p\n", instance); 
        // always 1 !!!
        print ("Number of ticking clocks %d\n", ticking_clocks.size);

Could somebody please help to point out the problem?
Thanks in advance.

My vala knowledge is very limited, but I think this is the problem:

    construct {
        ticking_clocks = new Gee.ArrayList<Clock> ();

That is, the static ticking_clocks member is recreated every time a Clock instance is constructed, so all previously added instances are lost.

Does changing that to static construct fix it?

@fmuellner Thank you very much, Indeed the list should be static across all instances.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.