Autoclose v0.2 - use-after-free fix

Added DOC_VALID checks