From 1138cc0cc95fec3f1b152e9d69fc4969a1f956ed Mon Sep 17 00:00:00 2001 From: Aleksandr Bazhin Date: Thu, 25 Apr 2024 11:25:35 +0700 Subject: [PATCH] Examples: change order of calls in loadTexture() (#3281) Fixes possible concurrency issue - makes code usable in a multithreaded environment. imageReleasCb releases imageContainer after bgfx::createTexture...() is called. If bgfx::frame() is called from another thread, the release can happen before imageContainer is used in bgfx::calcTextureSize(), leading to a crash. Although loadTexture() is not used in multithreaded environment in the examples, calling bgfx::calcTextureSize() before bgfx::createTexture...() is a better reference for users. --- examples/common/bgfx_utils.cpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/examples/common/bgfx_utils.cpp b/examples/common/bgfx_utils.cpp index aa8af1ac0..f9116d1d1 100644 --- a/examples/common/bgfx_utils.cpp +++ b/examples/common/bgfx_utils.cpp @@ -185,6 +185,20 @@ bgfx::TextureHandle loadTexture(bx::FileReaderI* _reader, const bx::FilePath& _f ); unload(data); + if (NULL != _info) + { + bgfx::calcTextureSize( + *_info + , uint16_t(imageContainer->m_width) + , uint16_t(imageContainer->m_height) + , uint16_t(imageContainer->m_depth) + , imageContainer->m_cubeMap + , 1 < imageContainer->m_numMips + , imageContainer->m_numLayers + , bgfx::TextureFormat::Enum(imageContainer->m_format) + ); + } + if (imageContainer->m_cubeMap) { handle = bgfx::createTextureCube( @@ -226,20 +240,6 @@ bgfx::TextureHandle loadTexture(bx::FileReaderI* _reader, const bx::FilePath& _f const bx::StringView name(_filePath); bgfx::setName(handle, name.getPtr(), name.getLength() ); } - - if (NULL != _info) - { - bgfx::calcTextureSize( - *_info - , uint16_t(imageContainer->m_width) - , uint16_t(imageContainer->m_height) - , uint16_t(imageContainer->m_depth) - , imageContainer->m_cubeMap - , 1 < imageContainer->m_numMips - , imageContainer->m_numLayers - , bgfx::TextureFormat::Enum(imageContainer->m_format) - ); - } } }