fix memory error in key copy routine