Fix crash in GetLegendGraphic with scale parameter and plug memory leak