Skip to content

Commit

Permalink
Fixed dangling reference when using Eigen matrix views repeatedly.
Browse files Browse the repository at this point in the history
  • Loading branch information
allanleal committed Jun 7, 2023
1 parent 02010be commit ef2575c
Showing 1 changed file with 4 additions and 14 deletions.
18 changes: 4 additions & 14 deletions Optima/EchelonizerW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <Optima/Exception.hpp>
#include <Optima/EchelonizerExtended.hpp>
#include <Optima/Utils.hpp>
#include <Optima/Macros.hpp>

namespace Optima {

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit ef2575c

Please sign in to comment.