From ca6781fc8f8a5abcbd3402125559838a91867cc6 Mon Sep 17 00:00:00 2001 From: Philip Hall Date: Wed, 8 Jan 2025 14:10:22 +0000 Subject: [PATCH] MLBEDSW-10229: Fix unit test memory leak - Valgrind detects memory leaks in the unit tests from the function-static operator store. This commit allows the operator store to be cleaned up before a test exits. Signed-off-by: Philip Hall Change-Id: I2fdc0cf3bb1ddd390443a0c6588fb1a1d7b42702 --- ethosu/regor/test/util.cpp | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/ethosu/regor/test/util.cpp b/ethosu/regor/test/util.cpp index 6842308c..617f495c 100644 --- a/ethosu/regor/test/util.cpp +++ b/ethosu/regor/test/util.cpp @@ -1,5 +1,5 @@ // -// SPDX-FileCopyrightText: Copyright 2024 Arm Limited and/or its affiliates +// SPDX-FileCopyrightText: Copyright 2024-2025 Arm Limited and/or its affiliates // // SPDX-License-Identifier: Apache-2.0 // @@ -21,6 +21,9 @@ #include "common/data_type.hpp" #include "common/ini_reader.hpp" +#include +#include + using namespace regor; // Helpers for Architecture @@ -182,14 +185,38 @@ std::shared_ptr CreateSchedulerTensor(const std::string &name, return schedTensor; } +static struct temporary_op_scope_t +{ + std::vector> _ops; + std::mutex _lock; + + void add_op(const std::shared_ptr &op) + { + std::lock_guard lock(_lock); + _ops.push_back(op); + } + + ~temporary_op_scope_t() + { + try + { + for ( const auto &op : _ops ) + op->Disconnect(); + } + catch ( std::bad_weak_ptr & ) + { + } + } +} s_ops; + + // Create a SchedulerOperation with unary input std::unique_ptr CreateSchedulerOperation(OpType opType, TensorUsage ifmUsage, std::shared_ptr &ifm, TensorUsage ofmUsage, std::shared_ptr &ofm) { // use static vector to keep operation reference alive - static std::vector> ops; auto op = CreateOperation(opType, ifmUsage, ifm->srcTensor, ofmUsage, ofm->srcTensor); - ops.push_back(op); + s_ops.add_op(op); auto schedOp = std::make_unique(opType); schedOp->_srcKey = static_cast(op.get()); -- GitLab