dialog: prevent double free in case of errors

(cherry picked from commit eef59c1d7b47ecc35438c01e7dcbdfeea903115a)