diff --git a/gtsam/nonlinear/Expression-inl.h b/gtsam/nonlinear/Expression-inl.h index b2ef6f055..620e6afca 100644 --- a/gtsam/nonlinear/Expression-inl.h +++ b/gtsam/nonlinear/Expression-inl.h @@ -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::valueAndJacobianMap() below. +#ifdef _MSC_VER +#include +#endif + #include #include @@ -208,7 +215,10 @@ T Expression::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(_aligned_malloc(size, internal::TraceAlignment)); + std::unique_ptr traceStorageDeleter( + _aligned_malloc(size, internal::TraceAlignment), + [](void *ptr){ _aligned_free(ptr); }); + auto traceStorage = static_cast(traceStorageDeleter.get()); #else internal::ExecutionTraceStorage traceStorage[size]; #endif @@ -217,10 +227,6 @@ T Expression::valueAndJacobianMap(const Values& values, T value(this->traceExecution(values, trace, traceStorage)); trace.startReverseAD1(jacobians); -#ifdef _MSC_VER - _aligned_free(traceStorage); -#endif - return value; }