Bug #3979
closedWPdfImage leaks memory through libharu
0%
Description
Constructing a WPdfImage using the constructor that does not accept a HPDF_Doc results in a memory leak when the WPdfImage is destructed. This leak is apparently a HPDF_GState that libharu fails to release, even though the WPdfImage destructor calls HPDF_Free. The problem seems to be that this code path leaves a document page with two GStates on the GState stack, but HPDF_Free appears to free at most one GState.
The memory leak can be observed with valgrind on the stand-alone charts example. Testing was done with Wt 3.3.4 on Debian 7.8 (wheezy), using libharu from github RELEASE_2_3_0RC2-61-g22e741e. Here is a lightly edited loss record from the charts example:
600 bytes in 3 blocks are definitely lost in loss record 600 of 680
at: malloc (vg_replace_malloc.c:263)
by: HPDF_GetMem (hpdf_mmgr.c:204)
by: HPDF_GState_New (hpdf_gstate.c:34)
by: HPDF_Page_New (hpdf_pages.c:337)
by: HPDF_AddPage (hpdf_doc.c:877)
by: Wt::WPdfImage::WPdfImage(Wt::WLength const&, Wt::WLength const&, Wt::WObject*) (WPdfImage.C:63)
by: Wt::ServerSideFontMetrics::ServerSideFontMetrics() (ServerSideFontMetrics.C:25)
by: Wt::WCanvasPaintDevice::fontMetrics() (WCanvasPaintDevice.C:626)
by: Wt::WMeasurePaintDevice::fontMetrics() (WMeasurePaintDevice.C:149)
by: Wt::WMeasurePaintDevice::drawText(Wt::WRectF const&, Wt::WFlags<Wt::AlignmentFlag>, Wt::TextFlag, Wt::WString const&) (WMeasurePaintDevice.C:97)
by: Wt::WPainter::drawText(Wt::WRectF const&, Wt::WFlags<Wt::AlignmentFlag>, Wt::WString const&) (WPainter.C:500)
by: Wt::Chart::WAxis::renderLabel(Wt::WPainter&, Wt::WString const&, Wt::WPointF const&, Wt::WFlags<Wt::AlignmentFlag>, double, int) const (WAxis.C:1347)
by: Wt::Chart::WAxis::render(Wt::WPainter&, Wt::WFlags<Wt::Chart::AxisProperty>, Wt::WPointF const&, Wt::WPointF const&, double, double, double, Wt::WFlags<Wt::AlignmentFlag>) const (WAxis.C:1293)
by: Wt::Chart::WCartesianChart::renderAxis(Wt::WPainter&, Wt::Chart::WAxis const&, Wt::WFlags<Wt::Chart::AxisProperty>) const (WCartesianChart.C:1968)
by: Wt::Chart::WCartesianChart::renderAxes(Wt::WPainter&, Wt::WFlags<Wt::Chart::AxisProperty>) const (WCartesianChart.C:1974)
by: Wt::Chart::WCartesianChart::initLayout(Wt::WRectF const&, Wt::WPaintDevice*) const (WCartesianChart.C:1444)
by: Wt::Chart::WCartesianChart::render(Wt::WPainter&, Wt::WRectF const&) const (WCartesianChart.C:1396)
by: Wt::Chart::WCartesianChart::paint(Wt::WPainter&, Wt::WRectF const&) const (WCartesianChart.C:1381)
by: Wt::Chart::WCartesianChart::paintEvent(Wt::WPaintDevice*) (WCartesianChart.C:1388)
by: Wt::WPaintedWidget::createDomElement(Wt::WApplication*) (WPaintedWidget.C:335)
While the leak in WPdfImage does not appear to be new, it may be encountered more often via the new setAutoLayoutEnabled feature in WCartesianChart.
The attached patch has been (lightly) tested as a workaround. Perhaps this should really be considered a bug in libharu?
Files
Updated by Bruce Toll about 9 years ago
It looks like this issue is still present with github Wt 3.3.5-rc1-2-gee51f12. While it may be caused by an upstream bug, it may be expedient to work around it in Wt.
Updated by Bruce Toll about 9 years ago
- File valgrind_graphics.supp valgrind_graphics.supp added
As a follow-up, I just attached the valgrind suppression file that I use when testing this issue. It makes it easier to spot the leak, and I probably should have included it with the original report. The full command line for testing is something like:
valgrind ---leak-check=full ---num-callers=20 ---suppressions=valgrind_graphics.supp ---log-file=valgrind_%p.log [charts.wt ...]
Updated by Bruce Toll about 9 years ago
Since there are a number of spurious one-time memory leaks reported by valgrind, I should have mentioned that this one appears to grow with use. Is it possible that the issue is specific to my environment? Let me know if there is additional information I can supply.
Updated by Koen Deforche about 9 years ago
- Status changed from New to InProgress
- Assignee set to Benoit Daccache
- Target version set to 3.3.5
Bruce, I agree with you that we should work around it in Wt, if it looks like we can.
Updated by Bruce Toll about 9 years ago
Thanks, Koen. I plan to file an upstream bug report and/or patch for libharu at some point. I'm not sure the prospects for inclusion, though.
Updated by Ulf Johnsson over 8 years ago
Any news on this?
I think I am seeing the same thing...
I have a WPaintedWidget, and in the paintEvent() I call WPaintDevice::fontMetrics() and WPaintDevice::measureText().
I use Wt3.3.6 with libHaru.
BR, Ulf.
Updated by Wim Dumon over 8 years ago
- Status changed from InProgress to Resolved
- Target version changed from 3.3.5 to 3.3.7
I've added Bruce's fix to WPdfImage, since it will probably not hurt when haru fixes this error too.
Updated by Roel Standaert over 7 years ago
- Status changed from Resolved to Closed