title: TF Lite C API crashes on second iteration
body: <p>I am trying to use the C API of TF Lite to execute the inference cyclically, e.g. I set up the interpreter and feed him inputs every second or so to get predictions.</p>
  
  <p>For this purpose, I have built <code>libtensorflowlite_c.so</code> via bazel. As documented <a href="https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/c/c_api.h" rel="nofollow noreferrer">here</a>, I try to do the inference like this, but inside a for loop to simulate the cyclic execution:</p>
  
  <pre><code>#include "tensorflow/lite/c/c_api.h"
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  
  int main (int argc, char* argv[]) {
  
     for(int i = 0; i &lt; 3; i++)
      {
          printf("Iteration: %d\n", i);
  
          float input[49] = { 0.0 };
          TfLiteModel* model = TfLiteModelCreateFromFile("model.tflite");
          TfLiteInterpreterOptions* options = TfLiteInterpreterOptionsCreate();
          TfLiteInterpreterOptionsSetNumThreads(options, 2);
          TfLiteInterpreter* interpreter = TfLiteInterpreterCreate(model, options);
          TfLiteInterpreterAllocateTensors(interpreter);
  
          TfLiteTensor* input_tensor = TfLiteInterpreterGetInputTensor(interpreter, 0);
          TfLiteTensorCopyFromBuffer(input_tensor, input, 49 * sizeof(float));
  
          TfLiteInterpreterInvoke(interpreter);
  
          const TfLiteTensor* output_tensor = TfLiteInterpreterGetOutputTensor(interpreter, 14);
  
          float output[49];
          TfLiteTensorCopyToBuffer(output_tensor, output, 49 * sizeof(float));
  
          printf("Output: \n\n");
          for (int j = 0; j &lt; 49; j++) {
              printf("%d: %f\n", j, output[j]);
          }
  
          TfLiteInterpreterDelete(interpreter);
          TfLiteInterpreterOptionsDelete(options);
          TfLiteModelDelete(model);
      }
      return 0;
  }
  </code></pre>
  
  <p>The first iteration runs fine and returns something. But on the second iteration, I get a SegFault when calling <code>TfLiteTensorCopyToBuffer(output_tensor, output, 49 * sizeof(float));</code>. Reason for this is that the previous function <code>TfLiteInterpreterGetOutputTensor</code> returns a nullpointer.</p>
  
  <p>I expected to run this multiple times without any problems, as I destroy all old instances of variables at the end of the for-loop and thus start a fresh interpreter everytime. Obviously, this is not the case.</p>
  
  <p>Can somebody provide any guidance on this? Also, I know that I probably do not have to create an interpreter on every iteration, but I wanted to make sure that everything is created new when I start over again. </p>
  
  <p>EDIT:</p>
  
  <p>I tried rewriting the code to exclude unnecessary parts from the actual loop:</p>
  
  <pre><code>#include "tensorflow/lite/c/c_api.h"
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  
  int main (int argc, char* argv[]) {
  
      float input[49] = {0.0};
      float output[49] = {[0 ... 48] = 2.5};
  
      TfLiteModel* model = TfLiteModelCreateFromFile("VariationalAutoencoder_440.tflite");
      TfLiteInterpreterOptions* options = TfLiteInterpreterOptionsCreate();
      TfLiteInterpreterOptionsSetNumThreads(options, 2);
      TfLiteInterpreter* interpreter = TfLiteInterpreterCreate(model, options);
      TfLiteInterpreterAllocateTensors(interpreter);
      TfLiteTensor* input_tensor = TfLiteInterpreterGetInputTensor(interpreter, 0);
      const TfLiteTensor* output_tensor = TfLiteInterpreterGetOutputTensor(interpreter, 14);
  
      for(int i = 0; i &lt; 3; i++)
      {
          printf("\nIteration: %d\n", i);
          TfLiteTensorCopyFromBuffer(input_tensor, input, 49 * sizeof(float));
          TfLiteInterpreterInvoke(interpreter);
          TfLiteTensorCopyToBuffer(output_tensor, output, 49 * sizeof(float));
  
          printf("Output: \n");
          for (int j = 0; j &lt; 49; j++)
          {
              printf("%02d: %f\n", j, output[j]);
          }
      }
  
      TfLiteInterpreterDelete(interpreter);
      TfLiteInterpreterOptionsDelete(options);
      TfLiteModelDelete(model);
  
      return 0;
  }
  </code></pre>