After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 638029 - Freeing the list from HashTable.get_values() causes corrupted data in the HashTable
Freeing the list from HashTable.get_values() causes corrupted data in the Has...
Status: RESOLVED DUPLICATE of bug 615830
Product: vala
Classification: Core
Component: general
0.10.x
Other Linux
: Normal normal
: ---
Assigned To: Vala maintainers
Vala maintainers
Depends on: 615830
Blocks:
 
 
Reported: 2010-12-26 08:37 UTC by Alexander Krivács Schrøder
Modified: 2010-12-27 16:25 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description Alexander Krivács Schrøder 2010-12-26 08:37:36 UTC
When using a HashTable, a nice and convenient way to get to all its values is by using its get_values() function. This function returns a List. The documentation for this function call specifies:

===
The content of the list is owned by the hash table and should not be modified or freed. Use g_list_free() when done using the list.
===

However, when the list goes out of scope in Vala, it tries to free the contents of the list as well. This causes the data in the HashTable to be corrupt. Example code demonstrating the problem:

int main(string[] args)
{
	HashTable<string, string> table = new HashTable<string, string>(str_hash, str_equal);
	table.insert("abc", "def");
	table.insert("ghi", "jkl");
	table.insert("mno", "pqr");
	
	{
		List<string> list = table.get_values();
	}
	
	stdout.printf("%s\n", table.lookup("ghi"));
	
	return 0;
}

If you comment out the scope in which the get_values() call is made, the output is as you would expect:

$ ./hash_list 
jkl
$ 

However, if you leave it in, this is the output I get from a couple of sample runs:

$ ./hash_list 
�_p	
$ ./hash_list 
�?�
$ ./hash_list 
�
$ ./hash_list 
ȯO
$ ./hash_list 
ȿ�	
$ 

In my larger application, where I discovered this bug, more sinister things happen, resulting in segmentation faults or glibc corruption errors. I haven't tested, but I'm pretty certain a similar problem would occur with the get_keys() call.

You should have a way of telling your compiler to free the list but not its contents, I assume a [CCode] attribute on the get_values() specification could help with this, although not knowing how the vala compiler works, that's just a guess.
Comment 1 Luca Bruno 2010-12-27 11:08:29 UTC
(In reply to comment #0)
> You should have a way of telling your compiler to free the list but not its
> contents, I assume a [CCode] attribute on the get_values() specification could
> help with this, although not knowing how the vala compiler works, that's just a
> guess.

That should be already that way due to the unowned keyword, it may be a regression.
Comment 2 Luca Bruno 2010-12-27 11:14:23 UTC
The problem is that you're assigning from List<unowned string> to List<string>. Marking the bug as duplicate of 615830.

*** This bug has been marked as a duplicate of bug 615830 ***
Comment 3 Alexander Krivács Schrøder 2010-12-27 12:10:31 UTC
No, I am not. The signature of the get_values() method is "List<V> get_values()", not "List<unowned V> get_values()". See for yourself here: http://valadoc.org/glib-2.0/GLib.HashTable.html

Either your documentation is wrong, or your vapi files are wrong. Regardless, something is wrong somewhere.
Comment 4 Luca Bruno 2010-12-27 16:25:08 UTC
I'm referring to vala master /  0.11.x.