title: Adding a TensorFlow Op that uses malloc() and has state
body: <p>I wrote a TensorFlow Op that wraps a library written in C that calls <code>malloc()</code> internally to allocate buffers and hold state. I initialize the library in the constructor, which is where the <code>malloc()</code> calls take place, and then in <code>Compute()</code> I use it with a mutex guard. The destructor makes the appropriate calls to <code>free()</code>. </p>
  
  <p>In my model each thread creates a copy of the Op, all in the same graph, and runs it within a shared session. The code very often crashes with pointer-related problems, such as "incorrect checksum for freed object - object was probably modified after being freed". The unit tests I wrote for the Op, with a single thread, work fine.</p>
  
  <p>I suspect the graph optimizer may be re-using a single instance of the Op, or something like that. Is there a way to tell it that the Op is stateful and that it should be left as-is? Are there other possible reasons for the crash?</p>
  
  <p>I'm running TensorFlow 1.0.0 and compiling the Op with clang 7.0, under OSX 10.10. I compile the C files first into a shared object, and then compile the C++ files together with the shared object.</p>