Fix memory leak in sync legacy db