From c24e5365b898e157668860ddd6f617333270b09b Mon Sep 17 00:00:00 2001 From: Calcitem Date: Tue, 26 Dec 2023 01:42:50 +0800 Subject: [PATCH] perfect: Refactor deinitializeIfNeeded for Performance and Safety This commit adjusts the deinitializeIfNeeded method in MalomSolutionAccess to enhance performance and ensure safety. The check for a null pointer (pp == nullptr) is moved to the beginning of the method. This change prevents unnecessary cleanup calls and potential double deletion issues, improving both the performance and robustness of the code. By returning early when pp is null, we avoid redundant cleanup operations and align with the intended idempotent nature of the function. Reference: https://github.com/ggevay/malom/pull/3#discussion_r1349746480 Change-Id: I6b77af22f99ad500217f1a70145144b33c1f20fb --- src/perfect/perfect_api.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/perfect/perfect_api.cpp b/src/perfect/perfect_api.cpp index ae3054d32..e78957212 100644 --- a/src/perfect/perfect_api.cpp +++ b/src/perfect/perfect_api.cpp @@ -167,12 +167,12 @@ void MalomSolutionAccess::initializeIfNeeded() void MalomSolutionAccess::deinitializeIfNeeded() { - Rules::cleanup(); - if (pp == nullptr) { return; } + Rules::cleanup(); + delete pp; pp = nullptr;