Fix regressions after commit d99f7bb7857

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