resize on unowned arrays should not be allowed
Vala code:
void func(int[] arr) {
arr.resize(2000);
stdout.printf("Length after resize: %d\n", arr.length);
}
void main() {
int[] arr = new int[1];
func(arr);
stdout.printf("Length in main: %d\n", arr.length);
}
Generated C code:
void
func (gint* arr,
gint arr_length1)
{
gint _tmp0_;
FILE* _tmp1_;
_tmp0_ = 2000;
arr = g_renew (gint, arr, 2000);
(_tmp0_ > arr_length1) ? memset (arr + arr_length1, 0, sizeof (gint) * (_tmp0_ - arr_length1)) : NULL;
arr_length1 = _tmp0_;
_tmp1_ = stdout;
fprintf (_tmp1_, "Length after resize: %d\n", arr_length1);
}
void
_vala_main (void)
{
gint* arr = NULL;
gint* _tmp0_;
gint arr_length1;
gint _arr_size_;
FILE* _tmp1_;
_tmp0_ = g_new0 (gint, 1);
arr = _tmp0_;
arr_length1 = 1;
_arr_size_ = arr_length1;
func (arr, (gint) arr_length1);
_tmp1_ = stdout;
fprintf (_tmp1_, "Length in main: %d\n", arr_length1);
arr = (g_free (arr), NULL);
}
Output:
Length after resize: 2000
Length in main: 1
Valgrind also reports a memory leak.
Since resize
ultimately calls realloc
, the allocation is moved and since an unowned reference doesn't include the pointer to the owned reference variable, it's impossible to support reallocations for this case.