Merge pull request #1415 from oicchris/gtsam_issue_1336

Fix (issue #1336) dangling pointer access violation.
release/4.3a0
Frank Dellaert 2023-01-26 23:00:36 -08:00 committed by GitHub
commit a41b4e02e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 5 deletions

View File

@ -19,6 +19,13 @@
#pragma once
// The MSVC compiler workaround for the unsupported variable length array
// utilizes the std::unique_ptr<> custom deleter.
// See Expression<T>::valueAndJacobianMap() below.
#ifdef _MSC_VER
#include <memory>
#endif
#include <gtsam/nonlinear/internal/ExpressionNode.h>
namespace gtsam {
@ -202,7 +209,10 @@ T Expression<T>::valueAndJacobianMap(const Values& values,
// allocated on Visual Studio. For more information see the issue below
// https://bitbucket.org/gtborg/gtsam/issue/178/vlas-unsupported-in-visual-studio
#ifdef _MSC_VER
auto traceStorage = static_cast<internal::ExecutionTraceStorage*>(_aligned_malloc(size, internal::TraceAlignment));
std::unique_ptr<void, void(*)(void*)> traceStorageDeleter(
_aligned_malloc(size, internal::TraceAlignment),
[](void *ptr){ _aligned_free(ptr); });
auto traceStorage = static_cast<internal::ExecutionTraceStorage*>(traceStorageDeleter.get());
#else
internal::ExecutionTraceStorage traceStorage[size];
#endif
@ -211,10 +221,6 @@ T Expression<T>::valueAndJacobianMap(const Values& values,
T value(this->traceExecution(values, trace, traceStorage));
trace.startReverseAD1(jacobians);
#ifdef _MSC_VER
_aligned_free(traceStorage);
#endif
return value;
}