Strings used as object keys get freed by the garbage collector #5

Closed
opened 2026-07-11 10:27:10 +00:00 by manabug · 1 comment
Owner

When a string is used as an object key, but is not stored anywhere else, the garbage collector frees it, while it is still in use as the object key.

This happens because the object key type is a plain string type instead of a String value, which means it cannot be marked during GC.

Reproducible code:

obj := {}
obj["a" + "b"] = 1
print(obj)
When a string is used as an object key, but is not stored anywhere else, the garbage collector frees it, while it is still in use as the object key. This happens because the object key type is a plain `string` type instead of a `String` value, which means it cannot be marked during GC. Reproducible code: ``` obj := {} obj["a" + "b"] = 1 print(obj) ```
manabug self-assigned this 2026-07-11 10:27:10 +00:00
Author
Owner

I fixed the bug by tracking strings used as keys in a separate map in
the Object struct. I know this is not a very performant or memory
efficient solution.

I considered doing string interning, but that seems too annoying to
implement for now. This is something I can consider in the future.

I fixed the bug by tracking strings used as keys in a separate map in the Object struct. I know this is not a very performant or memory efficient solution. I considered doing string interning, but that seems too annoying to implement for now. This is something I can consider in the future.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
manabug/chroma#5
No description provided.