code: handle NULL buffer and boxing gracefully

We do a series of cleanups on the state object and we might have lost
our connection to the object graph and or the buffer itself. This handles
those situations gracefully (while still asserting correctness in devel
builds).

Fixes #1297
2 jobs for master in 41 minutes and 24 seconds (queued for 2 seconds)
Status Job ID Name Coverage
  Test
passed #896666
flatpak
flatpak

00:36:39

 
  Deploy
passed #896667
nightly

00:04:45