Fix regressions after commit fdde3339619

The commit fixed few memory leaks, but also caused several crashes
due to those changes, thus this fixes the introduces issues.