From 0448b75918019c07bd62bc602f5a0b7a48820895 Mon Sep 17 00:00:00 2001 From: Ian Howell Date: Thu, 17 Jun 2021 08:07:23 -0700 Subject: [PATCH] Fix error when destructing a static std::unique_ptr When creating a static version of a reference to a `cppflow::model`, there becomes an error when destructing, due to the `context::get_status()` method using a `thread_local` status. While this is done normally for performance gains and is fine in most places. However, when the program is exiting, it first destructs `thread_local` objects and then `static` ones. This leads to `local_tf_status` containing a `nullptr` instead of a pointer to a `TF_Status` object. Therefore, `TF_DeleteSession(sess, status)` fails because it access `*status`. The simplest fix is to not re-use this status object in the destructor of the session and instead make a new, non-`thread_local` status instead. --- include/cppflow/model.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/include/cppflow/model.h b/include/cppflow/model.h index acc897c..f52e7f9 100644 --- a/include/cppflow/model.h +++ b/include/cppflow/model.h @@ -52,8 +52,10 @@ namespace cppflow { std::unique_ptr meta_graph = {TF_NewBuffer(), TF_DeleteBuffer}; auto session_deleter = [](TF_Session* sess) { - TF_DeleteSession(sess, context::get_status()); - status_check(context::get_status()); + auto status = TF_NewStatus(); + TF_DeleteSession(sess, status); + status_check(status); + TF_DeleteStatus(status); }; int tag_len = 1;