MemoryOutputStream's construction should not dupe array
I'd like to use MemoryOutputStream with fixed-sized buffer. However, this currently fails:
int main() {
uint8 outbuf[1] = {0};
{
var mem_stream = new MemoryOutputStream(outbuf, null, null);
var ostr = new DataOutputStream(mem_stream);
ostr.put_byte('H');
}
assert(outbuf[0] == 'H');
return 0;
}
Reading source code (or running with Valgrind) reveals that Vala silently makes a copy of array:
_tmp1_ = _vala_array_dup1 (outbuf, 1);
_tmp1__length1 = 1;
_tmp2_ = (GMemoryOutputStream*) g_memory_output_stream_new (_tmp1_, _tmp1__length1, NULL, NULL);
I'm guessing this is caused by owned
annotation of array in MemoryOutputStream
's constructor, which seems like a mistake to me. Removing owned
annotation by hand indeed makes provided example work as intended.
P.S.: this is also the case for MemoryInputStream
, but there it "only" results in silent memory leak instead of major logic error.
Using Vala 0.54.5.