Thread safety analysis: fix use after free bug reported by Evgeniy Stepanov.