Fix proposed by ryanofsky in https://github.com/bitcoin/bitcoin/pull/12647#discussion_r174875670
This cleans the wallet_hd.py with asan.
Partial backport of core PR10740 (commit 59b87a2)
https://github.com/bitcoin/bitcoin/pull/10740/commits/59b87a27efea819e433c727756bf5fac57b33dd6