Merge "Fix heap use after free." into pi-dev