Eliminate several memory leaks.

All leaks found with valgrind while running the test suite.
This commit also clears the Cairo cache to improve SNR of Valgrind
output.
This commit is contained in:
whitequark 2016-08-01 03:52:12 +00:00
parent a2a50927e9
commit 4f49a8a9d4
6 changed files with 24 additions and 5 deletions

View File

@ -637,6 +637,7 @@ void Group::DrawFilledPaths(Canvas *canvas) {
SPolygon sp = {}; SPolygon sp = {};
sbls.MakePwlInto(&sp); sbls.MakePwlInto(&sp);
canvas->DrawPolygon(sp, hcf); canvas->DrawPolygon(sp, hcf);
sp.Clear();
} }
} }

View File

@ -144,6 +144,11 @@ bool Canvas::Fill::Equals(const Fill &other) const {
pattern == other.pattern); pattern == other.pattern);
} }
void Canvas::Clear() {
strokes.Clear();
fills.Clear();
}
Canvas::hStroke Canvas::GetStroke(const Stroke &stroke) { Canvas::hStroke Canvas::GetStroke(const Stroke &stroke) {
for(const Stroke &s : strokes) { for(const Stroke &s : strokes) {
if(s.Equals(stroke)) return s.h; if(s.Equals(stroke)) return s.h;

View File

@ -98,6 +98,7 @@ public:
StipplePattern stipplePattern; StipplePattern stipplePattern;
double stippleScale; double stippleScale;
void Clear() { *this = {}; }
bool Equals(const Stroke &other) const; bool Equals(const Stroke &other) const;
}; };
@ -114,13 +115,15 @@ public:
RgbaColor color; RgbaColor color;
FillPattern pattern; FillPattern pattern;
void Clear() { *this = {}; }
bool Equals(const Fill &other) const; bool Equals(const Fill &other) const;
}; };
IdList<Stroke, hStroke> strokes; IdList<Stroke, hStroke> strokes;
IdList<Fill, hFill> fills; IdList<Fill, hFill> fills;
Canvas() : strokes(), fills() {}; Canvas() : strokes(), fills() {}
virtual void Clear();
hStroke GetStroke(const Stroke &stroke); hStroke GetStroke(const Stroke &stroke);
hFill GetFill(const Fill &fill); hFill GetFill(const Fill &fill);
@ -226,7 +229,7 @@ public:
BBox bbox; BBox bbox;
SurfaceRenderer() : camera(), lighting(), chordTolerance(), mesh(), bbox() {} SurfaceRenderer() : camera(), lighting(), chordTolerance(), mesh(), bbox() {}
virtual void Clear(); void Clear() override;
// Canvas interface. // Canvas interface.
const Camera &GetCamera() const override { return camera; } const Camera &GetCamera() const override { return camera; }

View File

@ -352,6 +352,8 @@ void SurfaceRenderer::OutputInPaintOrder() {
if(tr.meta.color.IsEmpty()) continue; if(tr.meta.color.IsEmpty()) continue;
OutputTriangle(tr); OutputTriangle(tr);
} }
mp.Clear();
} }
for(auto eit : edges) { for(auto eit : edges) {
@ -382,6 +384,8 @@ void SurfaceRenderer::OutputInPaintOrder() {
} }
void SurfaceRenderer::Clear() { void SurfaceRenderer::Clear() {
Canvas::Clear();
for(auto &eit : edges) { for(auto &eit : edges) {
SEdgeList &el = eit.second; SEdgeList &el = eit.second;
el.l.Clear(); el.l.Clear();

View File

@ -2,7 +2,8 @@
include_directories( include_directories(
${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}) ${CMAKE_CURRENT_BINARY_DIR}
${CAIRO_INCLUDE_DIRS})
set(testsuite_SOURCES set(testsuite_SOURCES
harness.cpp harness.cpp

View File

@ -5,6 +5,7 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include "harness.h" #include "harness.h"
#include <regex> #include <regex>
#include <cairo.h>
#if defined(WIN32) #if defined(WIN32)
#include <windows.h> #include <windows.h>
#else #else
@ -310,11 +311,15 @@ int main(int argc, char **argv) {
if(failTally > 0) { if(failTally > 0) {
fprintf(stderr, "Failure! %u checks failed\n", fprintf(stderr, "Failure! %u checks failed\n",
(unsigned)failTally); (unsigned)failTally);
return 1;
} else { } else {
fprintf(stderr, "Success! %u test cases (%u skipped), %u checks, %.3fs\n", fprintf(stderr, "Success! %u test cases (%u skipped), %u checks, %.3fs\n",
(unsigned)ranTally, (unsigned)skippedTally, (unsigned)ranTally, (unsigned)skippedTally,
(unsigned)checkTally, testTime.count()); (unsigned)checkTally, testTime.count());
return 0;
} }
// At last, try to reset all caches we or our dependencies have, to make SNR
// of memory checking tools like valgrind higher.
cairo_debug_reset_static_data();
return (failTally > 0);
} }