Re-gen location sql, fix memory leak