Fixed issue where a bitmap was not disposed, causing a memory leak if there were more than 256 colors on the image and it was not forcefully quantized.

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2455 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2013-02-01 20:35:05 +00:00
parent 5a1c8ffefb
commit 7ba6a57b4a
2 changed files with 34 additions and 18 deletions

View file

@ -259,21 +259,22 @@ namespace GreenshotPlugin.Core {
// check for color reduction, forced or automatically, only when the DisableReduceColors is false // check for color reduction, forced or automatically, only when the DisableReduceColors is false
if (!outputSettings.DisableReduceColors && (conf.OutputFileAutoReduceColors || outputSettings.ReduceColors)) { if (!outputSettings.DisableReduceColors && (conf.OutputFileAutoReduceColors || outputSettings.ReduceColors)) {
WuQuantizer quantizer = new WuQuantizer((Bitmap)imageToSave); using (WuQuantizer quantizer = new WuQuantizer((Bitmap)imageToSave)) {
int colorCount = quantizer.GetColorCount(); int colorCount = quantizer.GetColorCount();
LOG.InfoFormat("Image with format {0} has {1} colors", imageToSave.PixelFormat, colorCount); LOG.InfoFormat("Image with format {0} has {1} colors", imageToSave.PixelFormat, colorCount);
if (outputSettings.ReduceColors || colorCount < 256) { if (outputSettings.ReduceColors || colorCount < 256) {
try { try {
LOG.Info("Reducing colors on bitmap to 256."); LOG.Info("Reducing colors on bitmap to 256.");
tmpImage = quantizer.GetQuantizedImage(256); tmpImage = quantizer.GetQuantizedImage(256);
if (disposeImage) { if (disposeImage) {
imageToSave.Dispose(); imageToSave.Dispose();
}
imageToSave = tmpImage;
// Make sure the "new" image is disposed
disposeImage = true;
} catch (Exception e) {
LOG.Warn("Error occurred while Quantizing the image, ignoring and using original. Error: ", e);
} }
imageToSave = tmpImage;
// Make sure the "new" image is disposed
disposeImage = true;
} catch (Exception e) {
LOG.Warn("Error occurred while Quantizing the image, ignoring and using original. Error: ", e);
} }
} }
} }

View file

@ -69,7 +69,7 @@ namespace GreenshotPlugin.Core {
public Int32 Volume { get; set; } public Int32 Volume { get; set; }
} }
public class WuQuantizer { public class WuQuantizer : IDisposable {
private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(WuQuantizer)); private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(WuQuantizer));
private const Int32 MAXCOLOR = 512; private const Int32 MAXCOLOR = 512;
@ -100,6 +100,13 @@ namespace GreenshotPlugin.Core {
private Bitmap sourceBitmap; private Bitmap sourceBitmap;
private Bitmap resultBitmap; private Bitmap resultBitmap;
public void Dispose() {
if (resultBitmap != null) {
resultBitmap.Dispose();
resultBitmap = null;
}
}
/// <summary> /// <summary>
/// See <see cref="IColorQuantizer.Prepare"/> for more details. /// See <see cref="IColorQuantizer.Prepare"/> for more details.
/// </summary> /// </summary>
@ -222,7 +229,11 @@ namespace GreenshotPlugin.Core {
} }
} }
resultBitmap.Palette = imagePalette; resultBitmap.Palette = imagePalette;
return resultBitmap;
// Make sure the bitmap is not disposed, as we return it.
Bitmap tmpBitmap = resultBitmap;
resultBitmap = null;
return tmpBitmap;
} }
/// <summary> /// <summary>
@ -361,7 +372,11 @@ namespace GreenshotPlugin.Core {
imagePalette.Entries[paletteIndex] = Color.FromArgb(255, reds[paletteIndex], greens[paletteIndex], blues[paletteIndex]); imagePalette.Entries[paletteIndex] = Color.FromArgb(255, reds[paletteIndex], greens[paletteIndex], blues[paletteIndex]);
} }
resultBitmap.Palette = imagePalette; resultBitmap.Palette = imagePalette;
return resultBitmap;
// Make sure the bitmap is not disposed, as we return it.
Bitmap tmpBitmap = resultBitmap;
resultBitmap = null;
return tmpBitmap;
} }
/// <summary> /// <summary>
@ -632,5 +647,5 @@ namespace GreenshotPlugin.Core {
} }
} }
} }
} }
} }