backport memory leak fix fixes