From fd0d18f12e0793911ea560f7afc4c79f4421f70c Mon Sep 17 00:00:00 2001 From: Laurent Gomila Date: Wed, 13 Apr 2011 23:16:20 +0200 Subject: [PATCH] Fixed the crash that happened when destroying then creating again a render target --- src/SFML/Window/GlContext.cpp | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/SFML/Window/GlContext.cpp b/src/SFML/Window/GlContext.cpp index 350000d7..40252b49 100644 --- a/src/SFML/Window/GlContext.cpp +++ b/src/SFML/Window/GlContext.cpp @@ -31,7 +31,7 @@ #include #include #include -#include +#include #include #include @@ -67,17 +67,29 @@ namespace // Internal contexts sf::ThreadLocalPtr internalContext(NULL); - std::vector internalContexts; + std::set internalContexts; sf::Mutex internalContextsMutex; + // Check if the internal context of the current thread is valid + bool HasInternalContext() + { + // The internal context can be null... + if (!internalContext) + return false; + + // ... or non-null but deleted from the list of internal contexts + sf::Lock lock(internalContextsMutex); + return internalContexts.find(internalContext) != internalContexts.end(); + } + // Retrieve the internal context for the current thread sf::priv::GlContext* GetInternalContext() { - if (!internalContext) + if (!HasInternalContext()) { internalContext = sf::priv::GlContext::New(); sf::Lock lock(internalContextsMutex); - internalContexts.push_back(internalContext); + internalContexts.insert(internalContext); } return internalContext; @@ -111,7 +123,7 @@ void GlContext::Cleanup() // Destroy the internal contexts sf::Lock lock(internalContextsMutex); - for (std::vector::iterator it = internalContexts.begin(); it != internalContexts.end(); ++it) + for (std::set::iterator it = internalContexts.begin(); it != internalContexts.end(); ++it) delete *it; internalContexts.clear(); }