CAddrMan::Clear() exists to reset the internal state of CAddrMan. It’s currently used in two places:
- on startup, if deserializing peers.dat fails, Clear()is called to reset to an empty addrman
- in tests, Clear()is called to reset the addrman for more tests
In both cases, we can simply destruct the CAddrMan and construct a new, empty addrman. That approach is safer - it’s possible that Clear() could ‘reset’ the addrman to a state that’s not equivalent to a freshly constructed addrman (one actual example of this is that Clear() does not clear the m_tried_collisions set). On the other hand, if we destruct and then construct a fresh addrman, we’re guaranteed that the new object is empty.
This wasn’t possible when addrman was initially implemented, since it was a global, and so it would only be destructed on shutdown. However, addrman is now owned by node.context, so we have control over its destruction/construction.