Can't use array parameters in async method or function after yield
If I pass an string array as a parameter to an async method or function, I can use it before the first yield; but after it, it is not possible to use it because it has been freed.
Example code:
using GLib;
using Gtk;
async void asyncmethod(string[] a_string_list) {
foreach (var item in a_string_list) {
print(item); // this is printed fine
}
print("\n");
GLib.Timeout.add(1000, () => {
asyncmethod.callback ();
return false;
}, GLib.Priority.DEFAULT);
yield;
foreach (var item in a_string_list) {
print(item); // this results in a core dump or in garbage
}
print("\nDone\n");
}
void prepare_all() {
string [] string_list = {};
string_list += "Hello";
string_list += "Goodbye";
asyncmethod.begin(string_list);
}
int main(string[] argv) {
Gtk.init(ref argv);
prepare_all();
Gtk.main();
return 0;
}
The main function calls prepare_all
, which creates an string array, populates it and calls to asyncmethod
passing it as a parameter. The first half of the method can access the array, but as soon as the yield is executed, the array is freed, so when the execution continues after the yield, the array is not available and it results in a core dump.
Edited by Michael Gratton