Using a map after delete? (delete vs. clear_map)

Hi!

(First a heads up: I am pretty new to Odin and manual memory management in general!)

Just noticed that that I can still “use” a map after calling delete on it.
For example, this will still output all the key/values in the map:

new_map : map[int]int
new_map[1] = 2
new_map[2] = 3
delete(new_map)
for k,v in new_map {
	fmt.printf("Can still see this: {} is {}\n", k, v)
}

I guess this just means that it’s now unsafe to use the map, but the memory it pointed to is still around and can still be looked at?

(I noticed I had to use clear_map to actually “reset” the map.)

I guess my intuition was that calling delete on a Map would not allow me to use it afterwards, but this is not the case?

This is a use-after free bug. Don’t do this.

The memory might still be committed and not zeroed out by the default allocator, but do not use it since there is no guarantee now for anything.

1 Like

Yeah, my question was probably more “is the expected behaviour: that I can use it after delete (and that it does not get cleared when calling delete)?”, I understand that I should not use it after calling delete. But now I understand how it works, thanks!

yes

odin doesn’t have a borrow checker like rust, because it doesn’t have any ownership semantics

for a bug like this, you can use something like a ‘tracking allocator’ or asan.

-sanitize:address would have likely told you off here (at runtime)

@Laytan It sure did, thanks! That’s great!