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

Memory leak in creating string tensor. #199

Closed ns-wxin closed 2 years ago

ns-wxin commented 2 years ago

Hi, I have small test using the cppflow::tensor constructor from a string and got ASAN complaint on memory leak. It's complaining about TF_TString_ResizeUninitialized() method that adjusted the TF_TString size by re-allocating some space for (I believe) a large string. The code snippet looks like the following:

 int main(int argc, char **argv) {
      string modelPath = argv[1];
      // std::vector<std::string> v{ "dog", "Puppies are nice.", "I enjoy taking long walks along the beach
  with my dog." };
      // auto input = cppflow::fill(v, "string");
      string sent1 {"I enjoy taking long walks\n along the beach with my dog."};
      string sent2 {"Puppies are nice."};
      string sent3 {"I enjoy taking long walks along the beach with my do."};

      // auto sentence = cppflow::tensor(text);
      auto sentence2 = cppflow::tensor(sent1);
      auto sentence3 = cppflow::tensor(sent3);
      cout << "loading model: " <<  modelPath << endl;
      cppflow::model model(modelPath);

      return 0;
  }

Error was shown as below:

Direct leak of 64 byte(s) in 1 object(s) allocated from:
    #0 0x49a73d in malloc (/home/wxin/Code/shared/FingerprintMlCpp/build/src/test_string+0x49a73d)
    #1 0x4d0ed0 in TF_TString_ResizeUninitialized(TF_TString*, unsigned long) /home/wxin/Code/shared/TensorflowC/include/tensorflow/core/platform/ctstring_internal.h:272:23
    #2 0x4cc830 in TF_TString_Copy(TF_TString*, char const*, unsigned long) /home/wxin/Code/shared/TensorflowC/include/tensorflow/core/platform/ctstring_internal.h:395:17
    #3 0x4d6263 in cppflow::tensor::tensor<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) /home/wxin/Code/shared/cppflow/include/cppflow/tensor.h:161:9
    #4 0x4d08a9 in main /home/wxin/Code/shared/FingerprintMlCpp/src/test_string.cpp:217:22
    #5 0x7f172f2a0082 in __libc_start_main /build/glibc-SzIz7B/glibc-2.31/csu/../csu/libc-start.c:308:16
ns-wxin commented 2 years ago

Is the author of the cppflow able to look into the leak issue @serizba ?

serizba commented 2 years ago

Hi @ns-wxin

Can you check my comment in #200?