Fix a very subtle memory corruption bug