Fix memory leak cause by race condition when creating public keys.