Fixed bugs in instance id_map reference counting.