Fix "using value after free" issue