From 6d122f428a1e68d922164d85aee4b4edda0bf6c0 Mon Sep 17 00:00:00 2001 From: Marco Antognini Date: Sun, 3 Aug 2014 18:43:53 +0100 Subject: [PATCH] Updated OS X Pool Wrapper --- src/SFML/Window/OSX/AutoreleasePoolWrapper.h | 22 +++-- src/SFML/Window/OSX/AutoreleasePoolWrapper.mm | 80 +++++++------------ src/SFML/Window/OSX/WindowImplCocoa.mm | 7 +- 3 files changed, 44 insertions(+), 65 deletions(-) diff --git a/src/SFML/Window/OSX/AutoreleasePoolWrapper.h b/src/SFML/Window/OSX/AutoreleasePoolWrapper.h index 8efa8b06..90bc9741 100644 --- a/src/SFML/Window/OSX/AutoreleasePoolWrapper.h +++ b/src/SFML/Window/OSX/AutoreleasePoolWrapper.h @@ -26,26 +26,24 @@ //////////////////////////////////////////////////////////// /// \brief Ensure at least one autorelease pool is available on this thread /// -/// Increment a retain count. -/// See SPECIAL CONSIDERATION in implementation file. +/// Increment a retain count for *this* thread. /// //////////////////////////////////////////////////////////// void retainPool(void); +//////////////////////////////////////////////////////////// +/// \brief Drain the pool +/// +/// The pool retain count should be absolutely positive before calling this function on this thread. +/// +//////////////////////////////////////////////////////////// +void drainCurrentPool(void); + //////////////////////////////////////////////////////////// /// \brief Release the pool. /// -/// Drain the pool if it is no more needed (retain count is zero) -/// See SPECIAL CONSIDERATION in implementation file. +/// Decrease the retain count for *this* thread. /// //////////////////////////////////////////////////////////// void releasePool(void); -//////////////////////////////////////////////////////////// -/// \brief Drain the pool -/// -/// releasePool must be called at least once before drainPool. -/// -//////////////////////////////////////////////////////////// -void drainPool(); - diff --git a/src/SFML/Window/OSX/AutoreleasePoolWrapper.mm b/src/SFML/Window/OSX/AutoreleasePoolWrapper.mm index 1e7d3836..1745abd3 100644 --- a/src/SFML/Window/OSX/AutoreleasePoolWrapper.mm +++ b/src/SFML/Window/OSX/AutoreleasePoolWrapper.mm @@ -30,6 +30,8 @@ #include #include +#include + #import #import @@ -70,6 +72,8 @@ public : //////////////////////////////////////////////////////////// /// \brief Default destructor /// + /// Make sure the pool is drained (if appropriate) + /// //////////////////////////////////////////////////////////// ~PoolWrapper(); @@ -82,8 +86,10 @@ public : //////////////////////////////////////////////////////////// /// \brief Decrement retain count and releasing memory if needed /// + /// \return true if the pool wrapper can be released + /// //////////////////////////////////////////////////////////// - void release(); + bool release(); //////////////////////////////////////////////////////////// /// \brief Drain the pool @@ -104,7 +110,7 @@ private: //////////////////////////////////////////////////////////// PoolWrapper::PoolWrapper() : m_count(0), -m_pool(0) +m_pool(nil) { /* Nothing else */ } @@ -113,58 +119,37 @@ m_pool(0) //////////////////////////////////////////////////////////// PoolWrapper::~PoolWrapper() { -#ifdef SFML_DEBUG - if (m_count < 0) - sf::err() << "~PoolWrapper : m_count is less than zero! " - "You called releasePool from a thread too many times." - << std::endl; - else if (m_count > 0) - sf::err() << "~PoolWrapper : m_count is greater than zero! " - "You called releasePool from a thread to few times." - << std::endl; - else // m_count == 0 - sf::err() << "~PoolWrapper is HAPPY!" << std::endl; -#endif + // Make sure everything is drained + m_count = 0; + drain(); } //////////////////////////////////////////////////////////// void PoolWrapper::retain() { - // Increase counter. + // Increase counter ++m_count; - // Allocate pool if required. - if (m_pool == 0) + // Allocate pool if required + if (m_pool == nil) m_pool = [[NSAutoreleasePool alloc] init]; - -#ifdef SFML_DEBUG - if (m_count <= 0) - sf::err() << "PoolWrapper::retain : m_count <= 0! " << std::endl; -#endif } //////////////////////////////////////////////////////////// -void PoolWrapper::release() +bool PoolWrapper::release() { - // Decrease counter. + // Decrease counter --m_count; - // Drain pool if required. - if (m_count == 0) - drain(); - -#ifdef SFML_DEBUG - if (m_count < 0) - sf::err() << "PoolWrapper::release : m_count < 0! " << std::endl; -#endif + return m_count == 0; } void PoolWrapper::drain() { [m_pool drain]; - m_pool = 0; + m_pool = nil; if (m_count != 0) m_pool = [[NSAutoreleasePool alloc] init]; @@ -198,28 +183,23 @@ void retainPool(void) //////////////////////////////////////////////////////////// -void releasePool(void) +void drainCurrentPool(void) { - if (localPool != NULL) - localPool->release(); -#ifdef SFML_DEBUG - else - sf::err() << "releasePool : You must call retainPool at least once " - "in this thread before calling releasePool." - << std::endl; -#endif + assert(localPool != NULL); + localPool->drain(); } //////////////////////////////////////////////////////////// -void drainPool() +void releasePool(void) { - if (localPool != NULL) - localPool->drain(); -#ifdef SFML_DEBUG - else - sf::err() << "releasePool must be called at least one before drainPool" - << std::endl; -#endif + assert(localPool != NULL); + + // If we're done with the pool, let's release the memory + if (localPool->release()) + { + delete localPool; + localPool = NULL; + } } diff --git a/src/SFML/Window/OSX/WindowImplCocoa.mm b/src/SFML/Window/OSX/WindowImplCocoa.mm index 2484c38c..6de74d23 100644 --- a/src/SFML/Window/OSX/WindowImplCocoa.mm +++ b/src/SFML/Window/OSX/WindowImplCocoa.mm @@ -191,11 +191,11 @@ WindowImplCocoa::~WindowImplCocoa() if ([windows count] > 0) [[windows objectAtIndex:0] makeKeyAndOrderFront:nil]; - releasePool(); - - drainPool(); // Make sure everything was freed + drainCurrentPool(); // Make sure everything was freed // This solve some issue when sf::Window::Create is called for the // second time (nothing was render until the function was called again) + + releasePool(); } @@ -416,6 +416,7 @@ void WindowImplCocoa::textEntered(unichar charcode) void WindowImplCocoa::processEvents() { [m_delegate processEvent]; + drainCurrentPool(); // Reduce memory footprint } #pragma mark