From 43a0a07e9ed427a26161b352810827df3934e2c7 Mon Sep 17 00:00:00 2001 From: Chris Feger Date: Mon, 7 Nov 2022 14:32:42 -0700 Subject: [PATCH] Don't use double the memory for async SWF assembly --- .../BytecodeEditor/source/BytecodeEditor.cpp | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/Native/BytecodeEditor/source/BytecodeEditor.cpp b/Native/BytecodeEditor/source/BytecodeEditor.cpp index 8e229ae..f0094e5 100644 --- a/Native/BytecodeEditor/source/BytecodeEditor.cpp +++ b/Native/BytecodeEditor/source/BytecodeEditor.cpp @@ -142,13 +142,7 @@ FREObject BytecodeEditor::assembleAsync( ABC::ABCWriter(Assembler::assemble(strings, includeDebugInstructions).toABC()) .data()); - currentSWF->replaceABCData(data.data(), data.size()); - - std::vector finalData(currentSWF->getFullSize()); - - currentSWF->writeTo(finalData.data()); - - SUCCEED_ASYNC(finalData); + SUCCEED_ASYNC(data); } catch (std::exception& e) { @@ -269,15 +263,9 @@ FREObject BytecodeEditor::finishAssembleAsync() std::vector data = std::move(ABC::ABCWriter(partialAssembly->toABC()).data()); - currentSWF->replaceABCData(data.data(), data.size()); - - std::vector finalData(currentSWF->getFullSize()); - - currentSWF->writeTo(finalData.data()); - partialAssembly = nullptr; - SUCCEED_ASYNC(finalData); + SUCCEED_ASYNC(data); } catch (std::exception& e) { @@ -352,9 +340,17 @@ FREObject BytecodeEditor::taskResult() { const auto& data = std::get<3>(m_taskResult); + if (!currentSWF) + { + FAIL("Current SWF not set when trying to access result data. Did you accidentally " + "clean up first?"); + } + + currentSWF->replaceABCData(data.data(), data.size()); + FREObject lengthObj; - DO_OR_FAIL( - "Failed to create length object", FRENewObjectFromUint32(data.size(), &lengthObj)); + DO_OR_FAIL("Failed to create length object", + FRENewObjectFromUint32(currentSWF->getFullSize(), &lengthObj)); FREObject bytearrayObj; DO_OR_FAIL("Failed to create returned bytearray", @@ -366,7 +362,8 @@ FREObject BytecodeEditor::taskResult() FREByteArray ba; DO_OR_FAIL("Failed to acquire bytearray", FREAcquireByteArray(bytearrayObj, &ba)); - std::copy(data.begin(), data.end(), ba.bytes); + currentSWF->writeTo(ba.bytes); + DO_OR_FAIL("Failed to release bytearray", FREReleaseByteArray(bytearrayObj)); m_taskResult = std::monostate{};