Skip to content

Commit 98101f5

Browse files
strukturedclaude
authored andcommitted
Detach textures from framebuffers before deleting
GL drivers keep internal references to textures attached to framebuffers. Deleting a texture while still attached can cause use-after-free in driver memory, observed as crashes on NVIDIA during rapid preset switching or window resize. Detach all textures before deletion in both ~Framebuffer() and SetSize(), using a three-phase detach → resize → reattach pattern in SetSize() to avoid referencing stale texture IDs. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 16f40af commit 98101f5

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

src/libprojectM/Renderer/Framebuffer.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ Framebuffer::~Framebuffer()
2424
{
2525
if (!m_framebufferIds.empty())
2626
{
27-
// Delete attached textures first
28-
m_attachments.clear();
29-
27+
// Delete FBOs first — this also releases driver references to attached textures.
3028
glDeleteFramebuffers(static_cast<int>(m_framebufferIds.size()), m_framebufferIds.data());
3129
m_framebufferIds.clear();
30+
31+
m_attachments.clear();
3232
}
3333
}
3434

@@ -94,6 +94,8 @@ bool Framebuffer::SetSize(int width, int height)
9494
Bind(attachments.first);
9595
for (auto& texture : attachments.second)
9696
{
97+
// Detach old texture, resize (destroys old and creates new), reattach new.
98+
glFramebufferTexture2D(GL_FRAMEBUFFER, texture.first, GL_TEXTURE_2D, 0, 0);
9799
texture.second->SetSize(width, height);
98100
glFramebufferTexture2D(GL_FRAMEBUFFER, texture.first, GL_TEXTURE_2D, texture.second->Texture()->TextureID(), 0);
99101
}

0 commit comments

Comments
 (0)