Fix access of potentially freed memory.

Found with python compiled with --with-pydebug.