Skip to content

Commit

Permalink
Fix use after delete on resize
Browse files Browse the repository at this point in the history
  • Loading branch information
krOoze committed May 16, 2024
1 parent b5ed9bb commit 4f249ca
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/HelloTriangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ int helloTriangle() try{


// cleanup old
vector<VkSemaphore> oldImageReadySs = imageReadySs; imageReadySs.clear();
vector<VkSemaphore> oldImageReadySs;
oldImageReadySs.swap( imageReadySs );
if( oldSwapchain ){
{VkResult errorCode = vkDeviceWaitIdle( device ); RESULT_HANDLER( errorCode, "vkDeviceWaitIdle" );}

Expand Down
19 changes: 11 additions & 8 deletions src/WSI/DxgiWsi.h
Original file line number Diff line number Diff line change
Expand Up @@ -491,22 +491,26 @@ PlatformSwapchain initSwapchain(

const auto& newSurfaceImpl = ::platformSurfaces.at( surface );

PlatformSwapchain newSwapchain;
const PlatformSwapchain newSwapchain = newHandle<PlatformSwapchain>();

if( oldSwapchain && newSurfaceImpl.window.hWnd == ::platformSwapchains.at( oldSwapchain ).surface.window.hWnd ){ // mostly everything allowed to change with ResizeBuffers
auto& oldSwapImpl = ::platformSwapchains.at( oldSwapchain );
oldSwapImpl.imageCount = minImageCount;
::platformSwapchains[newSwapchain] = oldSwapImpl;

for( const auto im : oldSwapImpl.importedMemories ) vkFreeMemory( device, im, nullptr );
oldSwapImpl.importedMemories.clear();
for( const auto sh : oldSwapImpl.sharedHandles ) ChB( CloseHandle( sh ) );
oldSwapImpl.sharedHandles.clear();
for( const auto img : oldSwapImpl.swapchainImages ) vkDestroyImage( device, img, nullptr );
oldSwapImpl.swapchainImages.clear();

ChHR( oldSwapImpl.dxgiSwapchain->ResizeBuffers( oldSwapImpl.imageCount, capabilities.currentExtent.width, capabilities.currentExtent.height, format, swapchainFlags) );
auto& newSwapImpl = ::platformSwapchains.at( newSwapchain );
newSwapImpl.imageCount = minImageCount;
for( const auto im : newSwapImpl.importedMemories ) vkFreeMemory( device, im, nullptr );
newSwapImpl.importedMemories.clear();
for( const auto sh : newSwapImpl.sharedHandles ) ChB( CloseHandle( sh ) );
newSwapImpl.sharedHandles.clear();
for( const auto img : newSwapImpl.swapchainImages ) vkDestroyImage( device, img, nullptr );
newSwapImpl.swapchainImages.clear();

newSwapchain = oldSwapchain;
ChHR( newSwapImpl.dxgiSwapchain->ResizeBuffers( newSwapImpl.imageCount, capabilities.currentExtent.width, capabilities.currentExtent.height, format, swapchainFlags) );
}
else{
PlatformSwapchainImpl swapchain;
Expand Down Expand Up @@ -608,7 +612,6 @@ PlatformSwapchain initSwapchain(

ChHR( swapchain.dxgiFac->MakeWindowAssociation( swapchain.surface.window.hWnd, DXGI_MWA_NO_ALT_ENTER ) );

newSwapchain = newHandle<PlatformSwapchain>();
::platformSwapchains[newSwapchain] = swapchain;
}

Expand Down

0 comments on commit 4f249ca

Please sign in to comment.