serizba / cppflow

Run TensorFlow models in C++ without installation and without Bazel
https://serizba.github.io/cppflow/
MIT License
779 stars 177 forks source link

TFE_DeleteContext stuck on exit when loading from dll #190

Closed mwindowshz closed 2 years ago

mwindowshz commented 2 years ago

on exit my app is stuck on inline

context::~context() {
        TFE_DeleteContext(this->tfe_context);
    }

I have a dll that loads cppflow model, , runs and on exit, the code jumps to this contex destructor, but It does not close and stays stuck on TFE_DeleteContext() why is this. do I need to call some release function?

mwindowshz commented 2 years ago

The issue is that Contex class creates one global contex, and it is freed on the end. but when loading a the cppflow in a dll, My class usese cppflow, and I delete my class object this does not free the global contex. The hint for solution came from here: https://github.com/serizba/cppflow/blob/e389df06dea6dd2247276f7320cc227a3dfbe64b/include/cppflow/context.h#L43-L49

Problem with multi threaded, Our solution was to create contex and save it, then release it manualy on exit, and removing the ~() destructor call to TFE_DeleteContext(this->tfe_context);

our class creates TFE_Context*_contextInference = nullptr; in constructor:

// see comments cppflow/context.h linse 44 and 88
MyClass:MyClass()
{
    TFE_ContextOptions* tfe_opts = nullptr;
    cppflow::get_global_context() = cppflow::context(tfe_opts);
    _contextInference = cppflow::context::get_context();
}
MyClass~MyClass()
{
   TFE_DeleteContext(_contextInference);
  _contextInference = nullptr;
}

Thanks to @yochananscharf