Skip to content

Commit

Permalink
Merge pull request #1415 from oicchris/gtsam_issue_1336
Browse files Browse the repository at this point in the history
Fix (issue #1336) dangling pointer access violation.
  • Loading branch information
dellaert authored Jan 27, 2023
2 parents 12d4cfa + 2f5430d commit a41b4e0
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions gtsam/nonlinear/Expression-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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;
}

Expand Down

0 comments on commit a41b4e0

Please # to comment.