Free BerkeleyEnvironment instances when not in use
Summary:
Instead of adding BerkeleyEnvironment objects permanently to the g_dbenvs map, use reference counted shared pointers and remove map entries when the last BerkeleyEnvironment reference goes out of scope.
Backport of core PR11911 and PR15322:
https://github.com/bitcoin/bitcoin/pull/11911/files
https://github.com/bitcoin/bitcoin/pull/15322/files
This should fix ASAN and TSAN.
Depends on D4888.
Test Plan:
ninja all check ./test/functional/test_runner.py wallet_* ABC_BUILD_NAME=build-asan ./contrib/teamcity/build-configurations.sh ABC_BUILD_NAME=build-tsan ./contrib/teamcity/build-configurations.sh
Reviewers: #bitcoin_abc, deadalnix, jasonbcox
Reviewed By: #bitcoin_abc, deadalnix, jasonbcox
Subscribers: jasonbcox
Differential Revision: https://reviews.bitcoinabc.org/D4891