Re: [Vala] Implicit array size for function allocating an array



On 16/12/2014 10:52, Nicolas CARRIER wrote:
Hello,

I'm trying to bind a C function which has the following prototype:

int sos_addresses_to_string(const struct sos_address *addresses,
        char ***string_addresses, int nb);

It's job is to convert an array of addresses, into an array of their
string representation. So both the addresses array (in input) and the
string_addresses array will have the same size which is nb. So in
output, string_addresses will point to an allocated array of allocated
char buffers.

I tried to bind it this way:

namespace Sos {
    public struct Address {
        ...
        public static int array_to_string([CCode (array_length_pos =
3)] Address addresses[], [CCode (array_length = false] out string[]
string_addresses);
    }
}

and to use it with the following minimal code example:

public static int main(string[] args)
{
    string[] strings;
    Sos.Address[] addrs;
    // the following call just populates the initial addrs array
    Sos.Server.get_server_addresses("mambo", out addrs);
    Sos.Address.array_to_string(addrs, out strings);
    stdout.printf(@"str_addr: $(strings[0])\n");
    return Posix.EXIT_SUCCESS;
}

This approach works, but valgrind complains that the pointed string,
in the returned array, isn't freed. In fact, in the generated code,
_vala_array_free is called with an array length of -1, so only the
array is freed, not the pointed addresses.

I tried to add array_length_cexpr = "addresses.length" but it didn't
do anything, unless I remove 'array_length = false', but in this case,
an extra parameter is added and the code doesn't compile any-more.


What would be the best solution to this issue in your opinion ?
After the array_to_string call, do strings.length = addrs.length;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]