Could you please open a separate MR to preserve topology id for resolved topology? To fix that I think we should sacrifice a bit of overhead, and maybe use CloneFrom(), followed by Clear(). I don't think there is another way to preserve this id.
I'll take a look at refcounting problem you found.