Fix segfault on document close