title: Is passing a string back to the calling code necessary in FFI modules to prevent memory leaks?
body: <p>I have</p>
  
  <p><em>lib.rs</em></p>
  
  <pre class="lang-rust prettyprint-override"><code>#[no_mangle]
  pub extern "C" fn test() -&gt; *const c_char {
      CString::from("Hello").unwrap().into_raw()
  }
  </code></pre>
  
  <p>In Ruby, I call it as</p>
  
  <p><em>main.rb</em></p>
  
  <pre class="lang-rb prettyprint-override"><code>require 'ffi'
  
  module Rust
     extend FFI::Library
  
     # [redacted loading the library]
  
     attach_function :test, [], :string
  end
  
  some_string = Rust::test()
  </code></pre>
  
  <p>Do I have to pass the string back to Rust to be freed? Won't garbage collection pick this <code>some_string</code> up and free it?</p>
  
  <p>I'm using <a href="http://jakegoulding.com/rust-ffi-omnibus/string_return/" rel="nofollow noreferrer"><em>The Rust FFI Omnibus</em></a> as a reference. I ran <code>valgrind ruby main.rb</code> and it didn't seem to find any leaks.</p>