Re: [Vala] Closure inside a closure and variable access



On Tue, Jun 07, 2011 at 12:46:09PM -0700, Joseph Montanez wrote:
This is probably a no brainer but thought it was worth posting.

        state.on_score = () => {
            // Reset the ball
            ball.reset_location (engine);
            ball.pause ();

            var c = new Circle();
            c.x = engine.width / 2;
            c.y = engine.height / 2;
            c.radius = 34.00;
            engine.sprites.add (c);

            engine.add_timer(() => {
                ball.unpause ();
                engine.sprites.remove (c); //<-- CRASH
            }, 3000);
        };


In Vala is it correct to assume that anything defined inside an
anonymous function is not accessible anywhere else, even inside
another anonymous function that's inside itself? For now I ended up
moving the object "c" outside of them both and access was fine. I also
tried tossing around owned and unowned but same results. Is there
maybe a flag I can use to catch this sort of issue?

If it crashes it's because the delegate parameter of add_timer is not owned,
so you shouldn't use a closure.

-- 
http://www.debian.org - The Universal Operating System



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]