BUG: gh-2969: Backport memory leak fix 80b3a34.