Avoid infinite loops while freeing a cyclic hierarchy