-
Hey I'm pretty new to ash and while I was trying to create multiple swapchain to support multiple window I realize that sometime I get a pub(super)
struct Swapchain
{
vulkan_context: Weak<VulkanContext>,
raw: vk::SwapchainKHR,
frames: Vec<Frame>,
}
impl Swapchain
{
pub(super)
fn new(vulkan_context: &Rc<VulkanContext>, sizes: &[f32; 2]) -> Self
{
win_render_log!(Verbose, "Creating swapchain...");
let surface_format = choose_surface_format(
vulkan_context,
vk::Format::B8G8R8A8_SRGB,
vk::ColorSpaceKHR::SRGB_NONLINEAR,
);
let present_mode = choose_present_mode(
vulkan_context,
vk::PresentModeKHR::MAILBOX,
);
let extent = choose_extent(
vulkan_context,
sizes
);
let image_count = choose_image_count(
vulkan_context,
3,
);
let unique_queue_family_indices =
vulkan_context.queue_family_indices
.get_unique_values()
.iter()
.map(|&index| index as u32)
.collect::<Vec<u32>>();
let sharing_mode = {
if unique_queue_family_indices.len() == 1 {
vk::SharingMode::EXCLUSIVE
} else {
vk::SharingMode::CONCURRENT
}
};
let create_info = vk::SwapchainCreateInfoKHR::builder()
.surface(vulkan_context.surface.handle)
.min_image_count(image_count)
.image_format(surface_format.format)
.image_color_space(surface_format.color_space)
.image_extent(extent)
.image_array_layers(1)
.image_usage(vk::ImageUsageFlags::COLOR_ATTACHMENT)
.image_sharing_mode(sharing_mode)
.queue_family_indices(&unique_queue_family_indices)
.pre_transform(vk::SurfaceTransformFlagsKHR::IDENTITY)
.present_mode(present_mode)
.composite_alpha(vk::CompositeAlphaFlagsKHR::OPAQUE);
win_render_log!(Verbose, "Format: \"{:?}\" \"{:?}\"",
surface_format.format,
surface_format.color_space
);
win_render_log!(Verbose, "Present mode: \"{:?}\"", present_mode);
win_render_log!(Verbose, "Extent: {}x{}", extent.width, extent.height);
win_render_log!(Verbose, "Image count: {}", image_count);
let swapchain = unsafe {
vulkan_context.swapchain_loader.create_swapchain(&create_info, None)
.expect("Unable to create swapchain")
};
let swapchain_images = unsafe {
vulkan_context.swapchain_loader.get_swapchain_images(swapchain)
.expect("Unable to get swapchain images")
};
let frames =
swapchain_images.iter().map(|&image| {
Frame::new(
vulkan_context,
&image,
surface_format.format,
extent,
)
}).collect::<Vec<_>>();
Self {
vulkan_context: Rc::downgrade(vulkan_context),
raw: swapchain,
frames,
}
}
}
impl Drop for Swapchain
{
fn drop(&mut self)
{
win_render_log!(Verbose, "Dropping swapchain...");
let vulkan_context = {
let rc = self.vulkan_context.upgrade();
if rc.is_none() {
win_render_log!(Error, "Swapchain cleanup failed! Vulkan context is dropped!");
return;
}
rc.unwrap()
};
self.frames.clear();
unsafe {
vulkan_context.swapchain_loader.destroy_swapchain(self.raw, None);
}
}
} nothing fency, creating a swapchain then calling the delete function when Dropped. Before you ask:
(edit): I've made a very simple program that trigger this crash down the comment Thanks for your time :) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
Did you keep your |
Beta Was this translation helpful? Give feedback.
-
The |
Beta Was this translation helpful? Give feedback.
The values your builders point to are too short lived, resulting in a use after free. Extend the lifetime of these values by placing them in their own variable.
Fixed version (click to expand)