refs.c: free duplicate entries in the ref array instead of leaking them