[Vala] HashTable get_keys() and get_values() methods. Bug or my fault?



I don't know if I'm misunderstanding the semantics of the GLib.HashTable get_keys() and get_values() methods, so I'm asking here before posting it as bug.

I noticed that these methods work only the first time they're called, returning the desired List. While doing so they seem to touch something in memory, as after this the HashTable is somehow corrupted (aren't they read-only methods? or are they supposed to modify the HashTable?). Calling the methods for the second time result is not the expected one, and with the third call the program crashes.

I report a sample code that shows the problem (I repeat I may be missing something in the methods' semantics)

using GLib;

public class HashTest : GLib.Object {
    private HashTable<string,string> ht;

    public void run () throws DBus.Error, GLib.Error {
        for (int i=0; i<5; i++){
            List<string> v = ht.get_values();
            foreach (string s in v)
                message("%d: %s",i,s);
        }
    }
   
    public HashTest (){
        ht = new HashTable<string,string>(str_hash, str_equal);
        ht.insert("one","1");
        ht.insert("two","2");
        ht.insert("three","3");
    }

    static int main (string[] args) {
        var test = new HashTest ();
        test.run ();
        return 0;
    }
}

If I lookup every single item there is no problem (but of course in a more complex program I can't do this). I know that the for_each method is better to do these things (and it works properly): this is only sample code that shows the problem... I need to use get_keys() and get_values() in other contexts in my program.

Problem shows up in 0.5.1 and 0.5.2 too. I don't know in previous versions.

Daniele Benucci

--
Grabel's Law:
2 is not equal to 3 -- not even for large values of 2.


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