Why does Lua have both `__gc` and `__close`?

Viewed 141

The Lua manual states that the __close metamethod is used when to-be-closed variables go out of scope, and should release the variable's resources. But it also says that when a value is garbage-collected, the __gc metamethod is used to release resources.

In this question one of the comments says that if you have __close (or both __close and __gc) you have to be careful to avoid freeing the memory twice.

I'm still a little confused, though. So long as you're careful about how you write it, why can't there just be a single __gc function that gets used in both cases?

0 Answers
Related