serizba / cppflow

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

std::move causes segmentation fault in TF_DeleteSession () from /usr/local/lib/libtensorflow.so.2 #254

Open k-maheshkumar opened 7 months ago

k-maheshkumar commented 7 months ago

Moving cppflow::model causes segmentation fault on desctruction similar to #132, #154. Btw, this will happen to all default constructor implementations.

#include <cppflow/model.h>
#include <iostream>

// to run: ./example2 /path/to/model

class ModelInference
{
public:
    explicit ModelInference(cppflow::model&& _model) : model{std::move(_model)} {}
private:
    cppflow::model model;
};

int main(int, char* argv[])
{
    const std::string modelPath{argv[1]};
    ModelInference modelInference{cppflow::model{modelPath}};
    std::cout << "all ok exiting" << std::endl;
    return 0;
}

Please find the example to reproduce the problem at cppflow.zip

Gdb backtrace:

[New Thread 0x7ffea5ffb640 (LWP 16486)]
[Thread 0x7ffea5ffb640 (LWP 16486) exited]
2023-11-28 13:42:00.067943: I tensorflow/cc/saved_model/loader.cc:215] Running initialization op on SavedModel bundle at path: ../model/
2023-11-28 13:42:00.072693: I tensorflow/cc/saved_model/loader.cc:314] SavedModel load for tags { serve }; Status: success: OK. Took 339877 microseconds.
all ok exiting

Thread 1 "example2" received signal SIGSEGV, Segmentation fault.
0x00007fffced60a7d in TF_DeleteSession () from /usr/local/lib/libtensorflow.so.2
(gdb) bt
#0  0x00007fffced60a7d in TF_DeleteSession () from /usr/local/lib/libtensorflow.so.2
#1  0x000055555555b2b7 in cppflow::model::model(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cppflow::model::TYPE)::{lambda(TF_Session*)#1}::operator()(TF_Session*) const ()
#2  0x000055555555ecac in std::_Sp_counted_deleter<TF_Session*, cppflow::model::model(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cppflow::model::TYPE)::{lambda(TF_Session*)#1}, std::allocator<void>, (__gnu_cxx::_Lock_policy)2>::_M_dispose() ()
#3  0x000055555555b083 in std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release() ()
#4  0x000055555555bd79 in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count() ()
#5  0x000055555555b25e in std::__shared_ptr<TF_Session, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr() ()
#6  0x000055555555b27e in std::shared_ptr<TF_Session>::~shared_ptr() ()
#7  0x000055555555bb4e in cppflow::model::~model() ()
#8  0x000055555555bbc2 in ModelInference::~ModelInference() ()
#9  0x000055555555add7 in main ()
(gdb) bt
#0  0x00007fffced60a7d in TF_DeleteSession () from /usr/local/lib/libtensorflow.so.2
#1  0x000055555555b2b7 in cppflow::model::model(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cppflow::model::TYPE)::{lambda(TF_Session*)#1}::operator()(TF_Session*) const ()
#2  0x000055555555ecac in std::_Sp_counted_deleter<TF_Session*, cppflow::model::model(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cppflow::model::TYPE)::{lambda(TF_Session*)#1}, std::allocator<void>, (__gnu_cxx::_Lock_policy)2>::_M_dispose() ()
#3  0x000055555555b083 in std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release() ()
#4  0x000055555555bd79 in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count() ()
#5  0x000055555555b25e in std::__shared_ptr<TF_Session, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr() ()
#6  0x000055555555b27e in std::shared_ptr<TF_Session>::~shared_ptr() ()
#7  0x000055555555bb4e in cppflow::model::~model() ()
#8  0x000055555555bbc2 in ModelInference::~ModelInference() ()
#9  0x000055555555add7 in main ()
adayton1 commented 1 month ago

Just remove the call to std::move. You already have an rvalue reference, so the move constructor will get called without an additional call to std::move.