Fix segfault with STM; fixes #8035. Patch from errge.