Fixed memory bug in thread.