From 6d15ab00ede3955c43853b7034c4cb9b6ac5e327 Mon Sep 17 00:00:00 2001 From: gaaclarke <30870216+gaaclarke@users.noreply.github.com> Date: Wed, 20 Sep 2023 17:22:05 -0700 Subject: [PATCH] [Impeller] removed global mutable variable for tessellation allocation function pointers (flutter/engine#46127) issue: https://github.com/flutter/flutter/issues/125749 This removes a global mutable variable, instead giving each tesellator its own struct. I suspect the root cause of the linked issue is memory corruption but this is good cleanup anyways. If for some reason libTess2 is mutating that global variable at cleanup, that could be the source of this problem and this would fix the issue. [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../src/flutter/impeller/tessellator/tessellator.cc | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/engine/src/flutter/impeller/tessellator/tessellator.cc b/engine/src/flutter/impeller/tessellator/tessellator.cc index 4e2c3c535f..f6b27215cc 100644 --- a/engine/src/flutter/impeller/tessellator/tessellator.cc +++ b/engine/src/flutter/impeller/tessellator/tessellator.cc @@ -21,7 +21,7 @@ static void HeapFree(void* userData, void* ptr) { } // Note: these units are "number of entities" for bucket size and not in KB. -static TESSalloc alloc = { +static const TESSalloc kAlloc = { HeapAlloc, HeapRealloc, HeapFree, 0, /* =userData */ 16, /* =meshEdgeBucketSize */ 16, /* =meshVertexBucketSize */ @@ -31,8 +31,14 @@ static TESSalloc alloc = { 0 /* =extraVertices */ }; -Tessellator::Tessellator() - : c_tessellator_(::tessNewTess(&alloc), &DestroyTessellator) {} +Tessellator::Tessellator() : c_tessellator_(nullptr, &DestroyTessellator) { + TESSalloc alloc = kAlloc; + { + // libTess2 copies the TESSalloc despite the non-const argument. + CTessellator tessellator(::tessNewTess(&alloc), &DestroyTessellator); + c_tessellator_ = std::move(tessellator); + } +} Tessellator::~Tessellator() = default;