Nicolas píše v St 09. 12. 2009 v 18:24 +0100:
> [...]
> > Could you post the code that segfaults? That's probably a bug in Vala.
> 

Thank you. That's not a Vala bug. There is a problem that you don't
supply Map's constructor with custom hash and equality methods. In that
case, Map will only compare references, which are different each time
you call it. That means the values you get are always null, which leads
to segfault because you aren't checking for nulls. See my attached
code. :)
using Gee;

uint hash_func (void* num) {
	return str_hash (((double?)num).to_string ());
}

bool eql_func (void* num1, void* num2) {
	return *((double*)num1) == *((double*)num2);
}


static int main (string[] args) {

    double? first = 0.0;
    double? second = 0.0;
   	stdout.printf ("Direct hash for first: %u\n", direct_hash ((void*) first));
    stdout.printf ("Hash for first: %u\n", hash_func ((void*) first));
    stdout.printf ("Direct hash for second: %u\n", direct_hash ((void*) second));
    stdout.printf ("Hash for second: %u\n", hash_func ((void*) second));
    stdout.printf ("Reference equal: %s\n", direct_equal ((void*) first, (void*) second) ? "yes" : "no");
    stdout.printf ("Equal: %s\n", eql_func ((void*) first, (void*) second) ? "yes" : "no");

	var map = new HashMap<double?, double?> (hash_func, eql_func, eql_func);

    map.set (0.0, 0.0);
   	var val = map[0.0];
	if (val == null) {
		stdout.printf ("Value is null!\n");
	}
	else {
		stdout.printf ("Value: %f\n", val);
	}
    return 0;
}

Attachment: signature.asc
Description: Toto je digitálně podepsaná část zprávy

_______________________________________________
Vala-list mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/vala-list

Reply via email to