Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Race condition possible in LoadTextureFromImage from NativeEngine.cpp #1398

Open
bghgary opened this issue Jun 28, 2024 · 1 comment
Open
Milestone

Comments

@bghgary
Copy link
Contributor

bghgary commented Jun 28, 2024

for (uint8_t mip = 0; mip < image->m_numMips; ++mip)
{
bimg::ImageMip imageMip{};
if (bimg::imageGetRawData(*image, 0, mip, image->m_data, image->m_size, imageMip))
{
bgfx::ReleaseFn releaseFn{};
if (mip == image->m_numMips - 1)
{
releaseFn = [](void*, void* userData) {
bimg::imageFree(static_cast<bimg::ImageContainer*>(userData));
};
}
const bgfx::Memory* mem{bgfx::makeRef(imageMip.m_data, imageMip.m_size, releaseFn, image)};
texture->Update2D(0, mip, 0, 0, static_cast<uint16_t>(imageMip.m_width), static_cast<uint16_t>(imageMip.m_height), mem);
}
}

The problem is that if Start/FinishRenderingCurrentFrame is being called on the graphics thread, then bgfx::frame can cause releaseFn to be called before this loop is done with image. One possible somewhat hacky solution is to save the image->m_numMips in a local variable to prevent usage of image as it is only possible for the releaseFn to be called on the last mip.

@CedricGuillemet
Copy link
Contributor

Also, the possibility of a memory leak if the last mipmap update goes wrong is a bit scary.

@thomlucc thomlucc added this to the 8.0 milestone Jul 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants