Fix a rare crash in case multiple document instances are destroyed at ui_reload