I am making an interpreter in C, and I'm having a problem with my reference counting.
Each value
(which is the interpreter's representation... of a value) is allocated with refcount
0. Once it gets added to the stack, it increments the refcount
.
The only way to get a value
off the stack is to pop it off it, but that leads to problems. My popping function returns the value
that is popped, but if the refcount
is 0 and I destroy the value I can no longer return it.
I get that I should probably put the refcount
check somewhere else, but that just seems ugly as there are a lot of places that use the popping function.
What can I do to workaround this issue? Is implementing a real GC algorithm necessary in this case?
I use my own data base system which also uses a kind of refcount.
When an object is stored into a data base, then its refcount is incremented. When I get an object from a data base, its refcount remains unchanged. It is decremented only if the object is deleted by any way (usually the deletion of a data base containing it or its replacement by another object in a data base containing it). The object is really destroyed only when its refcount is equal to zero AND its deletion is required.