api: fix memory error in multi-thread environment