Fix crash on export with "overwrite existing files"

This should fix #2010. A required object was not retained long enough.