fdo#84086 Fix assorted use-after-free bugs

Change-Id: Iec004fffdb0afbe27bd69f379db90f6d904a8a65
Reviewed-on: https://gerrit.libreoffice.org/11553
Reviewed-by: Noel Grandin <noelgrandin@gmail.com>
Tested-by: Noel Grandin <noelgrandin@gmail.com>