TFPWriterPNG leaks memory
Original Reporter info from Mantis: Michalis @michaliskambi
-
Reporter name: Michalis Kamburelis
Original Reporter info from Mantis: Michalis @michaliskambi
- Reporter name: Michalis Kamburelis
Description:
Simplest example of FPImgCanv on http://wiki.freepascal.org/fcl-image#Basic_Canvas_Setup shows that TFPWriterPNG may leak memory when writing. Compiling fpwritepng.pp with HeapTrc:
$ fpc -l -gl -gh png_write_leak.pas
Free Pascal Compiler version 2.7.1 [2012/01/02] for i386
...
$ ./png_write_leak
Heap dump by heaptrc unit
55 memory blocks allocated : 370051/370136
53 memory blocks freed : 369907/369992
2 unfreed memory blocks : 144
True heap size : 425984
True free heap : 425680
Should be : 425712
Call trace for block $B7778130 size 128
$080A6575 TFPWRITERPNG__DETERMINEHEADER, line 414 of fpwritepng.pp
...
Attaching a simple patch that fixes the issue. When FPalette is created (because we want to write Indexed PNG image, but original image is not indexed, i.e. TheImage.UsePalette is false) --- it must be later freed. This is tracked by OwnsPalette property.
BTW, TFPWriterPNG.Indexed should probably be false by default, as this is a saner default in my opinion (and will work in 100% cases, while palette is limited). TFPCustomImage.UsePalette default was changed to false for similar reasons some time ago (see "Why is FPImage dog slow in reading jpeg's?" thread on fpc-pascal, http://free-pascal-general.1045716.n5.nabble.com/Why-is-FPImage-dog-slow-in-reading-jpeg-s-td4439450.html ). It seems natural that TFPWriterPNG.Indexed should be false by default too. See also issue #17621 (closed) , that would not happen if Indexed was false by default. This is a one-line change in TFPWriterPNG.create.
Mantis conversion info:
- Mantis ID: 21835
- OS: Debian GNU/Linux
- OS Build: (testing)
- Platform: i386
- Fixed in version: 3.0.0
- Fixed in revision: 21003 (#b5b0388f)
- Target version: 2.6.1