title: sp_session_create segfaults when called from Rust FFI
body: <p>Since Spotify is unlikely to provide a successor to libspotify anytime soon, I'm trying to wrap it into a thin Rust library that I can use for other projects. I'm using Bindings generated from the header file by rust-bindgen. Since they are quite long (~1100 lines) and not that easy to shrink down to only the important bits, I created a <a href="http://pastebin.com/ggXsRKAr" rel="nofollow">pastebin</a> containing the code.</p>
  
  <p>Calling <a href="https://developer.spotify.com/docs/libspotify/12.1.51/api_8h.html#a181d0940997cb8b69869449cd826cf88" rel="nofollow">sp_build_id</a> works just fine, invoking <a href="https://developer.spotify.com/docs/libspotify/12.1.51/group__session.html#gaf2891f2daced4ff6da84219d6376b3aa" rel="nofollow">sp_session_create</a> on the other hand, causes a segfault at address <code>0x000001C0</code>. Searching for libspotify and that memory address yielded <a href="https://stackoverflow.com/questions/17438339/c-marshall-strings-to-utf8-char">this post</a>. It suggests that something is wrong with the string marshalling, I just cannot figure out what it could be or if it indeed is the string marshalling.</p>
  
  <p>This is how I call the method:</p>
  
  <pre><code>fn main() {
      let app_key = include_bytes!(env!("SP_APP_KEY_PATH"));
      let cache_path = ::std::ffi::CString::new(env!("SP_CACHE_PATH")).unwrap();
  
      let config = sp_session_config {
          api_version: 12,
          cache_location: cache_path.as_ptr(),
          settings_location: cache_path.as_ptr(),
          application_key: &amp;app_key[..] as *const _ as *const ::std::os::raw::c_void,
          application_key_size: app_key.len() as u64,
          ..Default::default()
      };
  
      let mut ptr = ::std::ptr::null_mut();
      match unsafe { sp_session_create(&amp;config, &amp;mut ptr) } {
          Enum_sp_error::SP_ERROR_OK =&gt; println!("Session creation succeeded!"),
          err =&gt; println!("Session creation failed! {:?}", err)
      }
  }
  </code></pre>
  
  <p><code>SP_CACHE_PATH</code> points to a folder in my PC in which libspotify can store its assets and <code>SP_APP_KEY_PATH</code> points to the binary file with the application key. This all works fine (I have checked the generated code after the macros).</p>
  
  <p><code>sp_session_create</code> looks like this:</p>
  
  <pre><code>#[link(name = "libspotify")]
  extern "system" {
      pub fn sp_session_create(config: *const sp_session_config, sess: *mut *mut sp_session) -&gt; sp_error;
  }
  </code></pre>
  
  <p>The <code>system</code>-callconv expands to <code>stdcall</code> on 32-bit and <code>cdecl</code> on 64-bit, so it should just work for libspotify.</p>
  
  <p>I'm running Windows 10, Rust nightly 1.10 32-bit MSVC and I'm using libspotify 12.1.51. I currently do not have a Linux machine at my disposal, so it's kinda hard to test the code on other operating systems. I'd be glad if somebody else could try to test out the code and see if it works for them or not. I'd also gladly appreciate any ideas on how I might improve the FFI code.</p>