fix multithread bug -- allocate memory move to main loop