Clear weakrefs in garbage found by the GC (GH-16495) (#16499)

Fix a bug due to the interaction of weakrefs and the cyclic garbage
collector. We must clear any weakrefs in garbage in order to prevent
their callbacks from executing and causing a crash.
(cherry picked from commit bcda460baf25062ab68622b3f043f52b9db4d21d)

Co-authored-by: Neil Schemenauer <nas-github@arctrix.com>
2 files changed