GNOME Bugzilla – Bug 653143
Closure passed to async method loses reference to local vars
Last modified: 2018-05-22 14:04:24 UTC
Created attachment 190404 [details] closure passed to async method In this code: var f = etc; async_func ( () => { fn (f);} ); the lambda may not be able to access f correctly, sometimes resulting in a segmentation fault. In the attachment, the expected output is hello hello hello bye But I got: hello hello (null) bye The bug is due to incorrect access to x in the following: var x = "hello"; f2 (() => {stdout.printf ("%s\n", x);});
The problem is that x goes out of scope immediately after calling f2. Workarounds: 1) Use owned delegate parameters. 2) Call f2 (() => {stdout.printf ("%s\n", x);}, (s,r) => { f2.end (r); stdout.printf("end %s\n", x); }); I think it could be possible for vala to keep the data alive until the async method finishes by generating an implicit async callback, like we do already for explicit async callback.
-- GitLab Migration Automatic Message -- This bug has been migrated to GNOME's GitLab instance and has been closed from further activity. You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/vala/issues/208.