Re: [Vala] using Variant: memory leaks
- From: Luca Dionisi <luca dionisi gmail com>
- To: vala-list <vala-list gnome org>
- Subject: Re: [Vala] using Variant: memory leaks
- Date: Sat, 17 Dec 2011 00:05:21 +0100
On Fri, Dec 16, 2011 at 3:23 PM, Luca Dionisi <luca dionisi gmail com> wrote:
Hi folks
I am using Vala 0.12
This simple function leaks memory
void leaks()
{
Variant v = "hello";
string s = (string)v;
}
I am doing other investigations with Variant and memory leaks.
This time, always with valac 0.12, I mess with int arrays.
This function leaks:
void leaks()
{
int[] y = new int[] {1,2,3};
Variant v = y;
int[] x = (int[])v;
}
Valgrind reveals it:
==21222== LEAK SUMMARY:
==21222== definitely lost: 20 bytes in 1 blocks
This method uses a VariantIter in the stack to iterate the
container-type variant (g_variant_iter_next_value);
from each Variant of the container the int is retrieved
(g_variant_get_int32) and placed in a new array (which gets
resized/doubled when needed with g_renew);
the Variant itself is un-referenced, as required by the VariantIter.
Then the method duplicates the array of int costructed in order to get
an allocated buffer of the exact size, with g_memdup; the problem is
that the old buffer has not been freed (with g_free).
I tried to use g_variant_get, but this time this leads to another issue.
The assertion at the end of this function fails:
void wrongsize()
{
int[] y = new int[] {1,2,3};
Variant v = y;
int[] ai;
assert(v.get_type_string() == "ai");
v.get("ai", out ai);
assert(ai.length == 3);
}
The generated code doesn't pass the variable ai_length1 to the method
g_variant_get.
The only way I found to make it correctly work is to manually use the
VariantIter.
This function just works and does not leak:
void manually()
{
int[] y = new int[] {1,2,3};
Variant v = y;
ArrayList<int> li = new ArrayList<int>();
VariantIter iter = new VariantIter(v);
Variant? v0 = iter.next_value();
while (v0 != null)
{
int i = (int)v0;
li.add(i);
v0 = iter.next_value();
}
int[] ai = new int[li.size];
for (int i = 0; i < li.size; i++) ai[i] = li[i];
assert(ai.length == 3);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]