fix memleak through ThreadLocals (reviewed by Alexander Shatalin)