Fix use-after-free in replication