diff --git a/src/sdl2/audio.rs b/src/sdl2/audio.rs index 60f668d2..0551e186 100644 --- a/src/sdl2/audio.rs +++ b/src/sdl2/audio.rs @@ -652,7 +652,10 @@ impl AudioDevice { Some(device) => Some(CString::new(device).unwrap()), None => None }; - let device_ptr = device.map_or(ptr::null(), |s| s.as_ptr()); + // Warning: map_or consumes its argument; `device.map_or()` would therefore consume the + // CString and drop it, making device_ptr a dangling pointer! To avoid that we downgrade + // device to an Option<&_> first. + let device_ptr = device.as_ref().map_or(ptr::null(), |s| s.as_ptr()); let iscapture_flag = if capture { 1 } else { 0 }; let device_id = sys::SDL_OpenAudioDevice(