Fix coverity issues: unused return value and use after free

This fixes the final issue in #979