aggsol
aggsol

Reputation: 2510

What is an async scope for a delegate in Vala?

I am trying the async examples from the GNOME project site. I get the follwoing warning which I don't under stand on how to fix.

async.vala:8.2-8.17: warning: delegates with scope="async" must be owned

Code

async double do_calc_in_bg(double val) throws ThreadError {
    SourceFunc callback = do_calc_in_bg.callback;
    double[] output = new double[1];

    // Hold reference to closure to keep it from being freed whilst
    // thread is active.
    // WARNING HERE
    ThreadFunc<bool> run = () => {
        // Perform a dummy slow calculation.
        // (Insert real-life time-consuming algorithm here.)
        double result = 0;
        for (int a = 0; a<100000000; a++)
            result += val * a;

        output[0] = result;
        Idle.add((owned) callback);
        return true;
    };
    new Thread<bool>("thread-example", run);

    yield;
    return output[0];
}

void main(string[] args) {
    var loop = new MainLoop();
    do_calc_in_bg.begin(0.001, (obj, res) => {
            try {
                double result = do_calc_in_bg.end(res);
                stderr.printf(@"Result: $result\n");
            } catch (ThreadError e) {
                string msg = e.message;
                stderr.printf(@"Thread error: $msg\n");
            }
            loop.quit();
        });
    loop.run();
}

The warning is pointing at the run variable inside the async function. Who or what needs to be owned? The reference to the closure?

Upvotes: 3

Views: 222

Answers (1)

Jens M&#252;hlenhoff
Jens M&#252;hlenhoff

Reputation: 14883

The delegate needs to have a well defined owner all the time. The error message is a bit misleading.

To fix it you have to explicitly transfer the ownership from the delegate to the thread constructor:

new Thread<bool>("thread-example", (owned) run);

Instead of

new Thread<bool>("thread-example", run);

See also: https://wiki.gnome.org/Projects/Vala/Tutorial#Ownership

PS: The generated C code is fine in both cases. (at least with valac 0.46.6)

Upvotes: 3

Related Questions