diff --git a/fyrox-graphics/src/framebuffer.rs b/fyrox-graphics/src/framebuffer.rs index ee8dc3a93..686d23d36 100644 --- a/fyrox-graphics/src/framebuffer.rs +++ b/fyrox-graphics/src/framebuffer.rs @@ -328,16 +328,11 @@ impl dyn GpuFrameBufferTrait { where T: Pod, { - let mut bytes = self.read_pixels(read_target)?; - let typed = unsafe { - Vec::::from_raw_parts( - bytes.as_mut_ptr() as *mut T, - bytes.len() / size_of::(), - bytes.capacity() / size_of::(), - ) - }; - std::mem::forget(bytes); - Some(typed) + let bytes = self.read_pixels(read_target)?; + // Use bytemuck for a safe, correctly-aligned conversion. This copies into a new + // Vec with proper alignment, avoiding the UB from reinterpreting a Vec + // allocation as Vec (which would deallocate with the wrong layout). + Some(bytemuck::cast_slice::(&bytes).to_vec()) } }