owned pointers and array_length = false
Submitted by Allison (desrt)
Link to original bug (#612664)
Description
This pair of problems is apparent through the binding for Gtk.TreePath:
public class TreePath {
...
[CCode (array_length = false)]
public unowned int[] get_indices ();
consider this sample code:
weak int[] wrap (Gtk.TreePath path) { var p = path.get_indices (); p.length = path.get_depth (); return p; }
broken.vala:4.3-4.11: error: Local variable with strong reference used as return value and method return type has not been declared to transfer ownership return p; ^^^^^^^^^ Compilation failed: 1 error(s), 0 warning(s)
That's not what I'd expect. I said 'var' and the return value was unowned, so why is it trying to make a copy of it? That's the first bug.
Then I wondered how it knows the length to make the copy. I look at the source for the 'var p = path.get_indices ();' line:
p = (_tmp1_ = (_tmp0_ = gtk_tree_path_get_indices (path), (_tmp0_ == NULL) ? ((gpointer) _tmp0_) : _vala_array_dup1 (_tmp0_, -1)), p_length1 = -1, p_size = p_length1, _tmp1_);
the most frightening part here:
_vala_array_dup1 (tmp0, -1)
when you consider:
static gint* _vala_array_dup1 (gint* self, int length) { return g_memdup (self, length * sizeof (gint)); }
That's really bad. That's the second bug.
Two fixes for two bugs, I guess:
-
maybe: make 'var' follow the unowned/weak
-
definitely: give a compile error when attempting to duplicate an array that we know that we don't know the length of.