Fix crash on close #255

This commit fixes #255 and the probable duplicate #490.