From ef2575c16e42a9f0a5d2d23d24d8a30413ba3876 Mon Sep 17 00:00:00 2001 From: Allan Leal Date: Wed, 7 Jun 2023 18:31:22 +0200 Subject: [PATCH] Fixed dangling reference when using Eigen matrix views repeatedly. --- Optima/EchelonizerW.cpp | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/Optima/EchelonizerW.cpp b/Optima/EchelonizerW.cpp index 04bc8430..019d1c38 100644 --- a/Optima/EchelonizerW.cpp +++ b/Optima/EchelonizerW.cpp @@ -21,6 +21,7 @@ #include #include #include +#include namespace Optima { @@ -90,19 +91,7 @@ struct EchelonizerW::Impl const auto nb = echelonizer.numBasicVariables(); const auto nn = echelonizer.numNonBasicVariables(); - //============================================================================================================= - // WARNING: - //============================================================================================================= - // For some reason, the commented line below does not play correctly with - // eigen commit 7b35638ddb99a0298c5d3450de506a8e8e0203d3 in Windows with Release mode. - // This problem does not exist in Linux and macOS, and also in Windows with Debug mode. - // The pytest tests fail with error message like this: - // Windows fatal exception: access violation - // Current thread 0x00002544 (most recent call first): - // File "C:\Users\allan\codes\optima\tests\testing\utils\matrices.py", line 156 in createMatrixViewRWQ - // The solution was to avoid creating the topRows reference from the returned reference in echelonizer.R(). - //============================================================================================================= - // const auto Rb = echelonizer.R().topRows(nb); // WARNING: This + // const auto Rb = echelonizer.R().topRows(nb); // WARNING: This creates dangling references. const auto R = echelonizer.R(); const auto Rb = R.topRows(nb); @@ -155,7 +144,8 @@ struct EchelonizerW::Impl const auto nb = echelonizer.numBasicVariables(); const auto nn = echelonizer.numNonBasicVariables(); - const auto Rb = echelonizer.R().topRows(nb); + const auto R = echelonizer.R(); + const auto Rb = R.topRows(nb); const auto Sbn = S.topLeftCorner(nb, nn); const auto Sbp = S.topRightCorner(nb, np); const auto jbn = echelonizer.Q();