From a4acac813e30082a551646aaa74b28c59e97b209 Mon Sep 17 00:00:00 2001 From: binary1248 Date: Sun, 2 Apr 2017 01:12:29 +0200 Subject: [PATCH] Removed all manual memory management. --- examples/shader/Shader.cpp | 17 +++---- include/SFML/Audio/InputSoundFile.hpp | 15 +++--- include/SFML/Audio/OutputSoundFile.hpp | 3 +- include/SFML/Audio/SoundFileFactory.hpp | 13 ++--- include/SFML/Audio/SoundFileFactory.inl | 4 +- include/SFML/Graphics/Font.hpp | 7 +-- include/SFML/Graphics/Image.hpp | 8 +-- include/SFML/Graphics/RenderTexture.hpp | 5 +- include/SFML/Network/SocketSelector.hpp | 3 +- include/SFML/System/FileInputStream.hpp | 3 +- include/SFML/System/Mutex.hpp | 3 +- include/SFML/System/Thread.hpp | 5 +- include/SFML/System/Thread.inl | 9 ++-- include/SFML/System/ThreadLocal.hpp | 3 +- include/SFML/Window/Context.hpp | 3 +- include/SFML/Window/Window.hpp | 11 ++-- src/SFML/Audio/AlResource.cpp | 7 +-- src/SFML/Audio/InputSoundFile.cpp | 34 ++++++------- src/SFML/Audio/OutputSoundFile.cpp | 8 ++- src/SFML/Audio/SoundFileFactory.cpp | 8 +-- src/SFML/Graphics/Font.cpp | 43 ++++------------ src/SFML/Graphics/Image.cpp | 22 ++------ src/SFML/Graphics/RenderTexture.cpp | 16 ++---- .../Graphics/RenderTextureImplDefault.cpp | 11 +--- .../Graphics/RenderTextureImplDefault.hpp | 13 ++--- src/SFML/Graphics/RenderTextureImplFBO.cpp | 6 +-- src/SFML/Graphics/RenderTextureImplFBO.hpp | 7 +-- src/SFML/Network/SocketSelector.cpp | 9 ++-- src/SFML/System/Err.cpp | 10 ++-- src/SFML/System/FileInputStream.cpp | 9 +--- src/SFML/System/Mutex.cpp | 7 +-- src/SFML/System/Thread.cpp | 9 ++-- src/SFML/System/ThreadLocal.cpp | 7 +-- src/SFML/Window/Context.cpp | 1 - src/SFML/Window/GlContext.cpp | 50 +++++++------------ src/SFML/Window/GlContext.hpp | 7 +-- src/SFML/Window/Window.cpp | 20 +++----- src/SFML/Window/WindowImpl.cpp | 8 +-- src/SFML/Window/WindowImpl.hpp | 6 ++- 39 files changed, 163 insertions(+), 267 deletions(-) diff --git a/examples/shader/Shader.cpp b/examples/shader/Shader.cpp index 8a81fde3..0b8fd4f1 100644 --- a/examples/shader/Shader.cpp +++ b/examples/shader/Shader.cpp @@ -5,6 +5,7 @@ #include "Effect.hpp" #include #include +#include const sf::Font* Effect::s_font = NULL; @@ -357,12 +358,12 @@ int main() Effect::setFont(font); // Create the effects - std::vector effects; - effects.push_back(new Pixelate); - effects.push_back(new WaveBlur); - effects.push_back(new StormBlink); - effects.push_back(new Edge); - effects.push_back(new Geometry); + std::vector> effects; + effects.push_back(std::make_unique()); + effects.push_back(std::make_unique()); + effects.push_back(std::make_unique()); + effects.push_back(std::make_unique()); + effects.push_back(std::make_unique()); std::size_t current = 0; // Initialize them @@ -452,9 +453,5 @@ int main() window.display(); } - // delete the effects - for (std::size_t i = 0; i < effects.size(); ++i) - delete effects[i]; - return EXIT_SUCCESS; } diff --git a/include/SFML/Audio/InputSoundFile.hpp b/include/SFML/Audio/InputSoundFile.hpp index 031c85f4..9ef1d97a 100644 --- a/include/SFML/Audio/InputSoundFile.hpp +++ b/include/SFML/Audio/InputSoundFile.hpp @@ -33,6 +33,7 @@ #include #include #include +#include namespace sf @@ -217,13 +218,13 @@ private: //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - SoundFileReader* m_reader; ///< Reader that handles I/O on the file's format - InputStream* m_stream; ///< Input stream used to access the file's data - bool m_streamOwned; ///< Is the stream internal or external? - Uint64 m_sampleOffset; ///< Sample Read Position - Uint64 m_sampleCount; ///< Total number of samples in the file - unsigned int m_channelCount; ///< Number of channels of the sound - unsigned int m_sampleRate; ///< Number of samples per second + std::unique_ptr m_reader; ///< Reader that handles I/O on the file's format + std::unique_ptr m_ownedStream; ///< The stream we own if any + InputStream* m_stream; ///< Input stream used to access the file's data + Uint64 m_sampleOffset; ///< Sample Read Position + Uint64 m_sampleCount; ///< Total number of samples in the file + unsigned int m_channelCount; ///< Number of channels of the sound + unsigned int m_sampleRate; ///< Number of samples per second }; } // namespace sf diff --git a/include/SFML/Audio/OutputSoundFile.hpp b/include/SFML/Audio/OutputSoundFile.hpp index 325b38f0..c2044060 100644 --- a/include/SFML/Audio/OutputSoundFile.hpp +++ b/include/SFML/Audio/OutputSoundFile.hpp @@ -31,6 +31,7 @@ #include #include #include +#include namespace sf @@ -93,7 +94,7 @@ private: //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - SoundFileWriter* m_writer; ///< Writer that handles I/O on the file's format + std::unique_ptr m_writer; ///< Writer that handles I/O on the file's format }; } // namespace sf diff --git a/include/SFML/Audio/SoundFileFactory.hpp b/include/SFML/Audio/SoundFileFactory.hpp index b4eadf0f..08d4cf5a 100644 --- a/include/SFML/Audio/SoundFileFactory.hpp +++ b/include/SFML/Audio/SoundFileFactory.hpp @@ -31,6 +31,7 @@ #include #include #include +#include namespace sf @@ -95,7 +96,7 @@ public: /// \see createReaderFromMemory, createReaderFromStream /// //////////////////////////////////////////////////////////// - static SoundFileReader* createReaderFromFilename(const std::string& filename); + static std::unique_ptr createReaderFromFilename(const std::string& filename); //////////////////////////////////////////////////////////// /// \brief Instantiate the right codec for the given file in memory @@ -110,7 +111,7 @@ public: /// \see createReaderFromFilename, createReaderFromStream /// //////////////////////////////////////////////////////////// - static SoundFileReader* createReaderFromMemory(const void* data, std::size_t sizeInBytes); + static std::unique_ptr createReaderFromMemory(const void* data, std::size_t sizeInBytes); //////////////////////////////////////////////////////////// /// \brief Instantiate the right codec for the given file in stream @@ -124,7 +125,7 @@ public: /// \see createReaderFromFilename, createReaderFromMemory /// //////////////////////////////////////////////////////////// - static SoundFileReader* createReaderFromStream(InputStream& stream); + static std::unique_ptr createReaderFromStream(InputStream& stream); //////////////////////////////////////////////////////////// /// \brief Instantiate the right writer for the given file on disk @@ -136,7 +137,7 @@ public: /// \return A new sound file writer that can write given file, or null if no writer can handle it /// //////////////////////////////////////////////////////////// - static SoundFileWriter* createWriterFromFilename(const std::string& filename); + static std::unique_ptr createWriterFromFilename(const std::string& filename); private: @@ -146,14 +147,14 @@ private: struct ReaderFactory { bool (*check)(InputStream&); - SoundFileReader* (*create)(); + std::unique_ptr (*create)(); }; typedef std::vector ReaderFactoryArray; struct WriterFactory { bool (*check)(const std::string&); - SoundFileWriter* (*create)(); + std::unique_ptr (*create)(); }; typedef std::vector WriterFactoryArray; diff --git a/include/SFML/Audio/SoundFileFactory.inl b/include/SFML/Audio/SoundFileFactory.inl index a552ba87..3fb84726 100644 --- a/include/SFML/Audio/SoundFileFactory.inl +++ b/include/SFML/Audio/SoundFileFactory.inl @@ -31,8 +31,8 @@ namespace sf { namespace priv { - template SoundFileReader* createReader() {return new T;} - template SoundFileWriter* createWriter() {return new T;} + template std::unique_ptr createReader() {return std::make_unique();} + template std::unique_ptr createWriter() {return std::make_unique();} } //////////////////////////////////////////////////////////// diff --git a/include/SFML/Graphics/Font.hpp b/include/SFML/Graphics/Font.hpp index 9451f08c..18373855 100644 --- a/include/SFML/Graphics/Font.hpp +++ b/include/SFML/Graphics/Font.hpp @@ -37,6 +37,7 @@ #include #include #include +#include namespace sf @@ -348,14 +349,14 @@ private: //////////////////////////////////////////////////////////// void* m_library; ///< Pointer to the internal library interface (it is typeless to avoid exposing implementation details) void* m_face; ///< Pointer to the internal font face (it is typeless to avoid exposing implementation details) - void* m_streamRec; ///< Pointer to the stream rec instance (it is typeless to avoid exposing implementation details) + std::shared_ptr m_streamRec; ///< Pointer to the stream rec instance (it is typeless to avoid exposing implementation details) void* m_stroker; ///< Pointer to the stroker (it is typeless to avoid exposing implementation details) - int* m_refCount; ///< Reference counter used by implicit sharing + std::shared_ptr m_refCount; ///< Reference counter used by implicit sharing Info m_info; ///< Information about the font mutable PageTable m_pages; ///< Table containing the glyphs pages by character size mutable std::vector m_pixelBuffer; ///< Pixel buffer holding a glyph's pixels before being written to the texture #ifdef SFML_SYSTEM_ANDROID - void* m_stream; ///< Asset file streamer (if loaded from file) + std::shared_ptr m_stream; ///< Asset file streamer (if loaded from file) #endif }; diff --git a/include/SFML/Graphics/Image.hpp b/include/SFML/Graphics/Image.hpp index a9010c0d..a2127d86 100644 --- a/include/SFML/Graphics/Image.hpp +++ b/include/SFML/Graphics/Image.hpp @@ -33,7 +33,7 @@ #include #include #include - +#include namespace sf { @@ -263,10 +263,10 @@ private: //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - Vector2u m_size; ///< Image size - std::vector m_pixels; ///< Pixels of the image + Vector2u m_size; ///< Image size + std::vector m_pixels; ///< Pixels of the image #ifdef SFML_SYSTEM_ANDROID - void* m_stream; ///< Asset file streamer (if loaded from file) + std::shared_ptr m_stream; ///< Asset file streamer (if loaded from file) #endif }; diff --git a/include/SFML/Graphics/RenderTexture.hpp b/include/SFML/Graphics/RenderTexture.hpp index 52d53bd1..040317a3 100644 --- a/include/SFML/Graphics/RenderTexture.hpp +++ b/include/SFML/Graphics/RenderTexture.hpp @@ -31,6 +31,7 @@ #include #include #include +#include namespace sf @@ -207,8 +208,8 @@ private: //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - priv::RenderTextureImpl* m_impl; ///< Platform/hardware specific implementation - Texture m_texture; ///< Target texture to draw on + std::unique_ptr m_impl; ///< Platform/hardware specific implementation + Texture m_texture; ///< Target texture to draw on }; } // namespace sf diff --git a/include/SFML/Network/SocketSelector.hpp b/include/SFML/Network/SocketSelector.hpp index 92dcbad9..1936fb54 100644 --- a/include/SFML/Network/SocketSelector.hpp +++ b/include/SFML/Network/SocketSelector.hpp @@ -30,6 +30,7 @@ //////////////////////////////////////////////////////////// #include #include +#include namespace sf @@ -158,7 +159,7 @@ private: //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - SocketSelectorImpl* m_impl; ///< Opaque pointer to the implementation (which requires OS-specific types) + std::unique_ptr m_impl; ///< Opaque pointer to the implementation (which requires OS-specific types) }; } // namespace sf diff --git a/include/SFML/System/FileInputStream.hpp b/include/SFML/System/FileInputStream.hpp index b1a8c62c..c931dd80 100644 --- a/include/SFML/System/FileInputStream.hpp +++ b/include/SFML/System/FileInputStream.hpp @@ -34,6 +34,7 @@ #include #include #include +#include #ifdef ANDROID namespace sf @@ -123,7 +124,7 @@ private: // Member data //////////////////////////////////////////////////////////// #ifdef ANDROID - priv::ResourceStream* m_file; + std::unique_ptr m_file; #else std::FILE* m_file; ///< stdio file stream #endif diff --git a/include/SFML/System/Mutex.hpp b/include/SFML/System/Mutex.hpp index 18349e42..7ac8e8ad 100644 --- a/include/SFML/System/Mutex.hpp +++ b/include/SFML/System/Mutex.hpp @@ -30,6 +30,7 @@ //////////////////////////////////////////////////////////// #include #include +#include namespace sf @@ -85,7 +86,7 @@ private: //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - priv::MutexImpl* m_mutexImpl; ///< OS-specific implementation + std::shared_ptr m_mutexImpl; ///< OS-specific implementation }; } // namespace sf diff --git a/include/SFML/System/Thread.hpp b/include/SFML/System/Thread.hpp index 65c91271..45c5d62f 100644 --- a/include/SFML/System/Thread.hpp +++ b/include/SFML/System/Thread.hpp @@ -31,6 +31,7 @@ #include #include #include +#include namespace sf @@ -186,8 +187,8 @@ private: //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - priv::ThreadImpl* m_impl; ///< OS-specific implementation of the thread - priv::ThreadFunc* m_entryPoint; ///< Abstraction of the function to run + std::shared_ptr m_impl; ///< OS-specific implementation of the thread + std::shared_ptr m_entryPoint; ///< Abstraction of the function to run }; #include diff --git a/include/SFML/System/Thread.inl b/include/SFML/System/Thread.inl index b707aa30..fd3602fe 100644 --- a/include/SFML/System/Thread.inl +++ b/include/SFML/System/Thread.inl @@ -66,8 +66,7 @@ struct ThreadMemberFunc : ThreadFunc //////////////////////////////////////////////////////////// template Thread::Thread(F functor) : -m_impl (NULL), -m_entryPoint(new priv::ThreadFunctor(functor)) +m_entryPoint(std::make_shared>(functor)) { } @@ -75,8 +74,7 @@ m_entryPoint(new priv::ThreadFunctor(functor)) //////////////////////////////////////////////////////////// template Thread::Thread(F function, A argument) : -m_impl (NULL), -m_entryPoint(new priv::ThreadFunctorWithArg(function, argument)) +m_entryPoint(std::make_shared>(function, argument)) { } @@ -84,7 +82,6 @@ m_entryPoint(new priv::ThreadFunctorWithArg(function, argument)) //////////////////////////////////////////////////////////// template Thread::Thread(void(C::*function)(), C* object) : -m_impl (NULL), -m_entryPoint(new priv::ThreadMemberFunc(function, object)) +m_entryPoint(std::make_shared>(function, object)) { } diff --git a/include/SFML/System/ThreadLocal.hpp b/include/SFML/System/ThreadLocal.hpp index 6975e524..160abdfd 100644 --- a/include/SFML/System/ThreadLocal.hpp +++ b/include/SFML/System/ThreadLocal.hpp @@ -31,6 +31,7 @@ #include #include #include +#include namespace sf @@ -83,7 +84,7 @@ private: //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - priv::ThreadLocalImpl* m_impl; ///< Pointer to the OS specific implementation + std::unique_ptr m_impl; ///< Pointer to the OS specific implementation }; } // namespace sf diff --git a/include/SFML/Window/Context.hpp b/include/SFML/Window/Context.hpp index 9e1eedd4..53e6177b 100644 --- a/include/SFML/Window/Context.hpp +++ b/include/SFML/Window/Context.hpp @@ -32,6 +32,7 @@ #include #include #include +#include namespace sf @@ -135,7 +136,7 @@ private: //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - priv::GlContext* m_context; ///< Internal OpenGL context + std::unique_ptr m_context; ///< Internal OpenGL context }; } // namespace sf diff --git a/include/SFML/Window/Window.hpp b/include/SFML/Window/Window.hpp index 7135fdfe..5b0824f2 100644 --- a/include/SFML/Window/Window.hpp +++ b/include/SFML/Window/Window.hpp @@ -38,6 +38,7 @@ #include #include #include +#include namespace sf @@ -522,11 +523,11 @@ private: //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - priv::WindowImpl* m_impl; ///< Platform-specific implementation of the window - priv::GlContext* m_context; ///< Platform-specific implementation of the OpenGL context - Clock m_clock; ///< Clock for measuring the elapsed time between frames - Time m_frameTimeLimit; ///< Current framerate limit - Vector2u m_size; ///< Current size of the window + std::unique_ptr m_impl; ///< Platform-specific implementation of the window + std::unique_ptr m_context; ///< Platform-specific implementation of the OpenGL context + Clock m_clock; ///< Clock for measuring the elapsed time between frames + Time m_frameTimeLimit; ///< Current framerate limit + Vector2u m_size; ///< Current size of the window }; } // namespace sf diff --git a/src/SFML/Audio/AlResource.cpp b/src/SFML/Audio/AlResource.cpp index df50be0a..6dd1d207 100644 --- a/src/SFML/Audio/AlResource.cpp +++ b/src/SFML/Audio/AlResource.cpp @@ -29,6 +29,7 @@ #include #include #include +#include namespace @@ -40,7 +41,7 @@ namespace // The audio device is instantiated on demand rather than at global startup, // which solves a lot of weird crashes and errors. // It is destroyed when it is no longer needed. - sf::priv::AudioDevice* globalDevice; + std::unique_ptr globalDevice; } @@ -54,7 +55,7 @@ AlResource::AlResource() // If this is the very first resource, trigger the global device initialization if (count == 0) - globalDevice = new priv::AudioDevice; + globalDevice = std::make_unique(); // Increment the resources counter count++; @@ -72,7 +73,7 @@ AlResource::~AlResource() // If there's no more resource alive, we can destroy the device if (count == 0) - delete globalDevice; + globalDevice.reset(); } } // namespace sf diff --git a/src/SFML/Audio/InputSoundFile.cpp b/src/SFML/Audio/InputSoundFile.cpp index 43a3388f..c75e9cc4 100644 --- a/src/SFML/Audio/InputSoundFile.cpp +++ b/src/SFML/Audio/InputSoundFile.cpp @@ -38,10 +38,8 @@ namespace sf { //////////////////////////////////////////////////////////// InputSoundFile::InputSoundFile() : -m_reader (NULL), -m_stream (NULL), -m_streamOwned (false), -m_sampleOffset (0), +m_stream (nullptr), +m_sampleOffset(0), m_sampleCount (0), m_channelCount(0), m_sampleRate (0) @@ -69,9 +67,7 @@ bool InputSoundFile::openFromFile(const std::string& filename) return false; // Wrap the file into a stream - FileInputStream* file = new FileInputStream; - m_stream = file; - m_streamOwned = true; + auto file = std::make_unique(); // Open it if (!file->open(filename)) @@ -93,6 +89,9 @@ bool InputSoundFile::openFromFile(const std::string& filename) m_channelCount = info.channelCount; m_sampleRate = info.sampleRate; + m_ownedStream = std::move(file); + m_stream = m_ownedStream.get(); + return true; } @@ -109,9 +108,7 @@ bool InputSoundFile::openFromMemory(const void* data, std::size_t sizeInBytes) return false; // Wrap the memory file into a stream - MemoryInputStream* memory = new MemoryInputStream; - m_stream = memory; - m_streamOwned = true; + auto memory = std::make_unique(); // Open it memory->open(data, sizeInBytes); @@ -129,6 +126,9 @@ bool InputSoundFile::openFromMemory(const void* data, std::size_t sizeInBytes) m_channelCount = info.channelCount; m_sampleRate = info.sampleRate; + m_ownedStream = std::move(memory); + m_stream = m_ownedStream.get(); + return true; } @@ -146,7 +146,6 @@ bool InputSoundFile::openFromStream(InputStream& stream) // store the stream m_stream = &stream; - m_streamOwned = false; // Don't forget to reset the stream to its beginning before re-opening it if (stream.seek(0) != 0) @@ -257,16 +256,11 @@ Uint64 InputSoundFile::read(Int16* samples, Uint64 maxCount) void InputSoundFile::close() { // Destroy the reader - delete m_reader; - m_reader = NULL; + m_reader.reset(); - // Destroy the stream if we own it - if (m_streamOwned) - { - delete m_stream; - m_streamOwned = false; - } - m_stream = NULL; + m_ownedStream.reset(); + + m_stream = nullptr; m_sampleOffset = 0; // Reset the sound file attributes diff --git a/src/SFML/Audio/OutputSoundFile.cpp b/src/SFML/Audio/OutputSoundFile.cpp index 870e622e..2f5a340d 100644 --- a/src/SFML/Audio/OutputSoundFile.cpp +++ b/src/SFML/Audio/OutputSoundFile.cpp @@ -33,8 +33,7 @@ namespace sf { //////////////////////////////////////////////////////////// -OutputSoundFile::OutputSoundFile() : -m_writer(NULL) +OutputSoundFile::OutputSoundFile() { } @@ -80,9 +79,8 @@ void OutputSoundFile::write(const Int16* samples, Uint64 count) //////////////////////////////////////////////////////////// void OutputSoundFile::close() { - // Destroy the reader - delete m_writer; - m_writer = NULL; + // Destroy the writer + m_writer.reset(); } } // namespace sf diff --git a/src/SFML/Audio/SoundFileFactory.cpp b/src/SFML/Audio/SoundFileFactory.cpp index 181e7775..d2b1fc84 100644 --- a/src/SFML/Audio/SoundFileFactory.cpp +++ b/src/SFML/Audio/SoundFileFactory.cpp @@ -63,7 +63,7 @@ SoundFileFactory::WriterFactoryArray SoundFileFactory::s_writers; //////////////////////////////////////////////////////////// -SoundFileReader* SoundFileFactory::createReaderFromFilename(const std::string& filename) +std::unique_ptr SoundFileFactory::createReaderFromFilename(const std::string& filename) { // Register the built-in readers/writers on first call ensureDefaultReadersWritersRegistered(); @@ -90,7 +90,7 @@ SoundFileReader* SoundFileFactory::createReaderFromFilename(const std::string& f //////////////////////////////////////////////////////////// -SoundFileReader* SoundFileFactory::createReaderFromMemory(const void* data, std::size_t sizeInBytes) +std::unique_ptr SoundFileFactory::createReaderFromMemory(const void* data, std::size_t sizeInBytes) { // Register the built-in readers/writers on first call ensureDefaultReadersWritersRegistered(); @@ -114,7 +114,7 @@ SoundFileReader* SoundFileFactory::createReaderFromMemory(const void* data, std: //////////////////////////////////////////////////////////// -SoundFileReader* SoundFileFactory::createReaderFromStream(InputStream& stream) +std::unique_ptr SoundFileFactory::createReaderFromStream(InputStream& stream) { // Register the built-in readers/writers on first call ensureDefaultReadersWritersRegistered(); @@ -134,7 +134,7 @@ SoundFileReader* SoundFileFactory::createReaderFromStream(InputStream& stream) //////////////////////////////////////////////////////////// -SoundFileWriter* SoundFileFactory::createWriterFromFilename(const std::string& filename) +std::unique_ptr SoundFileFactory::createWriterFromFilename(const std::string& filename) { // Register the built-in readers/writers on first call ensureDefaultReadersWritersRegistered(); diff --git a/src/SFML/Graphics/Font.cpp b/src/SFML/Graphics/Font.cpp index bf182333..4829a526 100644 --- a/src/SFML/Graphics/Font.cpp +++ b/src/SFML/Graphics/Font.cpp @@ -70,14 +70,9 @@ namespace sf Font::Font() : m_library (NULL), m_face (NULL), -m_streamRec(NULL), m_stroker (NULL), -m_refCount (NULL), m_info () { - #ifdef SFML_SYSTEM_ANDROID - m_stream = NULL; - #endif } @@ -92,10 +87,6 @@ m_info (copy.m_info), m_pages (copy.m_pages), m_pixelBuffer(copy.m_pixelBuffer) { - #ifdef SFML_SYSTEM_ANDROID - m_stream = NULL; - #endif - // Note: as FreeType doesn't provide functions for copying/cloning, // we must share all the FreeType pointers @@ -108,13 +99,6 @@ m_pixelBuffer(copy.m_pixelBuffer) Font::~Font() { cleanup(); - - #ifdef SFML_SYSTEM_ANDROID - - if (m_stream) - delete (priv::ResourceStream*)m_stream; - - #endif } @@ -125,7 +109,7 @@ bool Font::loadFromFile(const std::string& filename) // Cleanup the previous resources cleanup(); - m_refCount = new int(1); + m_refCount = std::make_shared(1); // Initialize FreeType // Note: we initialize FreeType for every font instance in order to avoid having a single @@ -173,11 +157,8 @@ bool Font::loadFromFile(const std::string& filename) #else - if (m_stream) - delete (priv::ResourceStream*)m_stream; - - m_stream = new priv::ResourceStream(filename); - return loadFromStream(*(priv::ResourceStream*)m_stream); + m_stream = std::make_shared(filename); + return loadFromStream(*std::static_pointer_cast(m_stream)); #endif } @@ -188,7 +169,7 @@ bool Font::loadFromMemory(const void* data, std::size_t sizeInBytes) { // Cleanup the previous resources cleanup(); - m_refCount = new int(1); + m_refCount = std::make_shared(1); // Initialize FreeType // Note: we initialize FreeType for every font instance in order to avoid having a single @@ -241,7 +222,7 @@ bool Font::loadFromStream(InputStream& stream) { // Cleanup the previous resources cleanup(); - m_refCount = new int(1); + m_refCount = std::make_shared(1); // Initialize FreeType // Note: we initialize FreeType for every font instance in order to avoid having a single @@ -258,8 +239,8 @@ bool Font::loadFromStream(InputStream& stream) stream.seek(0); // Prepare a wrapper for our stream, that we'll pass to FreeType callbacks - FT_StreamRec* rec = new FT_StreamRec; - std::memset(rec, 0, sizeof(*rec)); + auto rec = std::make_shared(); + std::memset(rec.get(), 0, sizeof(FT_StreamRec)); rec->base = NULL; rec->size = static_cast(stream.getSize()); rec->pos = 0; @@ -270,7 +251,7 @@ bool Font::loadFromStream(InputStream& stream) // Setup the FreeType callbacks that will read our stream FT_Open_Args args; args.flags = FT_OPEN_STREAM; - args.stream = rec; + args.stream = rec.get(); args.driver = 0; // Load the new font face from the specified stream @@ -278,7 +259,6 @@ bool Font::loadFromStream(InputStream& stream) if (FT_Open_Face(static_cast(m_library), &args, 0, &face) != 0) { err() << "Failed to load font from stream (failed to create the font face)" << std::endl; - delete rec; return false; } @@ -296,7 +276,6 @@ bool Font::loadFromStream(InputStream& stream) { err() << "Failed to load font from stream (failed to set the Unicode character set)" << std::endl; FT_Done_Face(face); - delete rec; return false; } @@ -477,7 +456,7 @@ void Font::cleanup() if (*m_refCount == 0) { // Delete the reference counter - delete m_refCount; + m_refCount.reset(); // Destroy the stroker if (m_stroker) @@ -488,8 +467,7 @@ void Font::cleanup() FT_Done_Face(static_cast(m_face)); // Destroy the stream rec instance, if any (must be done after FT_Done_Face!) - if (m_streamRec) - delete static_cast(m_streamRec); + m_streamRec.reset(); // Close the library if (m_library) @@ -502,7 +480,6 @@ void Font::cleanup() m_face = NULL; m_stroker = NULL; m_streamRec = NULL; - m_refCount = NULL; m_pages.clear(); std::vector().swap(m_pixelBuffer); } diff --git a/src/SFML/Graphics/Image.cpp b/src/SFML/Graphics/Image.cpp index 8dae99b0..8fc77a1b 100644 --- a/src/SFML/Graphics/Image.cpp +++ b/src/SFML/Graphics/Image.cpp @@ -41,24 +41,11 @@ namespace sf Image::Image() : m_size(0, 0) { - #ifdef SFML_SYSTEM_ANDROID - - m_stream = NULL; - - #endif } //////////////////////////////////////////////////////////// -Image::~Image() -{ - #ifdef SFML_SYSTEM_ANDROID - - if (m_stream) - delete (priv::ResourceStream*)m_stream; - - #endif -} +Image::~Image() = default; //////////////////////////////////////////////////////////// @@ -135,11 +122,8 @@ bool Image::loadFromFile(const std::string& filename) #else - if (m_stream) - delete (priv::ResourceStream*)m_stream; - - m_stream = new priv::ResourceStream(filename); - return loadFromStream(*(priv::ResourceStream*)m_stream); + m_stream = std::make_shared(filename); + return loadFromStream(*std::static_pointer_cast(m_stream)); #endif } diff --git a/src/SFML/Graphics/RenderTexture.cpp b/src/SFML/Graphics/RenderTexture.cpp index 12e8edf6..cf4cf50c 100644 --- a/src/SFML/Graphics/RenderTexture.cpp +++ b/src/SFML/Graphics/RenderTexture.cpp @@ -34,18 +34,11 @@ namespace sf { //////////////////////////////////////////////////////////// -RenderTexture::RenderTexture() : -m_impl(NULL) -{ - -} +RenderTexture::RenderTexture() = default; //////////////////////////////////////////////////////////// -RenderTexture::~RenderTexture() -{ - delete m_impl; -} +RenderTexture::~RenderTexture() = default; //////////////////////////////////////////////////////////// @@ -62,11 +55,10 @@ bool RenderTexture::create(unsigned int width, unsigned int height, bool depthBu setSmooth(false); // Create the implementation - delete m_impl; if (priv::RenderTextureImplFBO::isAvailable()) { // Use frame-buffer object (FBO) - m_impl = new priv::RenderTextureImplFBO; + m_impl = std::make_unique(); // Mark the texture as being a framebuffer object attachment m_texture.m_fboAttachment = true; @@ -74,7 +66,7 @@ bool RenderTexture::create(unsigned int width, unsigned int height, bool depthBu else { // Use default implementation - m_impl = new priv::RenderTextureImplDefault; + m_impl = std::make_unique(); } // Initialize the render texture diff --git a/src/SFML/Graphics/RenderTextureImplDefault.cpp b/src/SFML/Graphics/RenderTextureImplDefault.cpp index c52679dc..1d011eae 100644 --- a/src/SFML/Graphics/RenderTextureImplDefault.cpp +++ b/src/SFML/Graphics/RenderTextureImplDefault.cpp @@ -38,7 +38,6 @@ namespace priv { //////////////////////////////////////////////////////////// RenderTextureImplDefault::RenderTextureImplDefault() : -m_context(0), m_width (0), m_height (0) { @@ -46,14 +45,6 @@ m_height (0) } -//////////////////////////////////////////////////////////// -RenderTextureImplDefault::~RenderTextureImplDefault() -{ - // Destroy the context - delete m_context; -} - - //////////////////////////////////////////////////////////// bool RenderTextureImplDefault::create(unsigned int width, unsigned int height, unsigned int, bool depthBuffer) { @@ -62,7 +53,7 @@ bool RenderTextureImplDefault::create(unsigned int width, unsigned int height, u m_height = height; // Create the in-memory OpenGL context - m_context = new Context(ContextSettings(depthBuffer ? 32 : 0), width, height); + m_context = std::make_unique(ContextSettings(depthBuffer ? 32 : 0), width, height); return true; } diff --git a/src/SFML/Graphics/RenderTextureImplDefault.hpp b/src/SFML/Graphics/RenderTextureImplDefault.hpp index 803b18f9..8d2a8b47 100644 --- a/src/SFML/Graphics/RenderTextureImplDefault.hpp +++ b/src/SFML/Graphics/RenderTextureImplDefault.hpp @@ -31,6 +31,7 @@ #include #include #include +#include namespace sf @@ -52,12 +53,6 @@ public: //////////////////////////////////////////////////////////// RenderTextureImplDefault(); - //////////////////////////////////////////////////////////// - /// \brief Destructor - /// - //////////////////////////////////////////////////////////// - ~RenderTextureImplDefault(); - private: //////////////////////////////////////////////////////////// @@ -94,9 +89,9 @@ private: //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - Context* m_context; ///< P-Buffer based context - unsigned int m_width; ///< Width of the P-Buffer - unsigned int m_height; ///< Height of the P-Buffer + std::unique_ptr m_context; ///< P-Buffer based context + unsigned int m_width; ///< Width of the P-Buffer + unsigned int m_height; ///< Height of the P-Buffer }; } // namespace priv diff --git a/src/SFML/Graphics/RenderTextureImplFBO.cpp b/src/SFML/Graphics/RenderTextureImplFBO.cpp index 58dcb464..ef332a30 100644 --- a/src/SFML/Graphics/RenderTextureImplFBO.cpp +++ b/src/SFML/Graphics/RenderTextureImplFBO.cpp @@ -37,7 +37,6 @@ namespace priv { //////////////////////////////////////////////////////////// RenderTextureImplFBO::RenderTextureImplFBO() : -m_context (NULL), m_frameBuffer(0), m_depthBuffer(0) { @@ -63,9 +62,6 @@ RenderTextureImplFBO::~RenderTextureImplFBO() GLuint frameBuffer = static_cast(m_frameBuffer); glCheck(GLEXT_glDeleteFramebuffers(1, &frameBuffer)); } - - // Delete the context - delete m_context; } @@ -85,7 +81,7 @@ bool RenderTextureImplFBO::isAvailable() bool RenderTextureImplFBO::create(unsigned int width, unsigned int height, unsigned int textureId, bool depthBuffer) { // Create the context - m_context = new Context; + m_context = std::make_unique(); // Create the framebuffer object GLuint frameBuffer = 0; diff --git a/src/SFML/Graphics/RenderTextureImplFBO.hpp b/src/SFML/Graphics/RenderTextureImplFBO.hpp index c987281f..321a9878 100644 --- a/src/SFML/Graphics/RenderTextureImplFBO.hpp +++ b/src/SFML/Graphics/RenderTextureImplFBO.hpp @@ -31,6 +31,7 @@ #include #include #include +#include namespace sf @@ -102,9 +103,9 @@ private: //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - Context* m_context; ///< Needs a separate OpenGL context for not messing up the other ones - unsigned int m_frameBuffer; ///< OpenGL frame buffer object - unsigned int m_depthBuffer; ///< Optional depth buffer attached to the frame buffer + std::unique_ptr m_context; ///< Needs a separate OpenGL context for not messing up the other ones + unsigned int m_frameBuffer; ///< OpenGL frame buffer object + unsigned int m_depthBuffer; ///< Optional depth buffer attached to the frame buffer }; } // namespace priv diff --git a/src/SFML/Network/SocketSelector.cpp b/src/SFML/Network/SocketSelector.cpp index d7c9c564..9159391a 100644 --- a/src/SFML/Network/SocketSelector.cpp +++ b/src/SFML/Network/SocketSelector.cpp @@ -51,7 +51,7 @@ struct SocketSelector::SocketSelectorImpl //////////////////////////////////////////////////////////// SocketSelector::SocketSelector() : -m_impl(new SocketSelectorImpl) +m_impl(std::make_unique()) { clear(); } @@ -59,17 +59,14 @@ m_impl(new SocketSelectorImpl) //////////////////////////////////////////////////////////// SocketSelector::SocketSelector(const SocketSelector& copy) : -m_impl(new SocketSelectorImpl(*copy.m_impl)) +m_impl(std::make_unique(*copy.m_impl)) { } //////////////////////////////////////////////////////////// -SocketSelector::~SocketSelector() -{ - delete m_impl; -} +SocketSelector::~SocketSelector() = default; //////////////////////////////////////////////////////////// diff --git a/src/SFML/System/Err.cpp b/src/SFML/System/Err.cpp index 51be9aa3..d4463251 100644 --- a/src/SFML/System/Err.cpp +++ b/src/SFML/System/Err.cpp @@ -40,19 +40,13 @@ public: DefaultErrStreamBuf() { - // Allocate the write buffer - constexpr auto size = 64; - char* buffer = new char[size]; - setp(buffer, buffer + size); + setp(m_buffer, m_buffer + sizeof(m_buffer)); } ~DefaultErrStreamBuf() { // Synchronize sync(); - - // Delete the write buffer - delete[] pbase(); } private: @@ -92,6 +86,8 @@ private: return 0; } + + char m_buffer[64]; }; } diff --git a/src/SFML/System/FileInputStream.cpp b/src/SFML/System/FileInputStream.cpp index e6f1c838..924b6c43 100644 --- a/src/SFML/System/FileInputStream.cpp +++ b/src/SFML/System/FileInputStream.cpp @@ -44,10 +44,7 @@ FileInputStream::FileInputStream() //////////////////////////////////////////////////////////// FileInputStream::~FileInputStream() { -#ifdef ANDROID - if (m_file) - delete m_file; -#else +#ifndef ANDROID if (m_file) std::fclose(m_file); #endif @@ -58,9 +55,7 @@ FileInputStream::~FileInputStream() bool FileInputStream::open(const std::string& filename) { #ifdef ANDROID - if (m_file) - delete m_file; - m_file = new priv::ResourceStream(filename); + m_file = std::make_unique(filename); return m_file->tell() != -1; #else if (m_file) diff --git a/src/SFML/System/Mutex.cpp b/src/SFML/System/Mutex.cpp index 36d48ee6..e204ae04 100644 --- a/src/SFML/System/Mutex.cpp +++ b/src/SFML/System/Mutex.cpp @@ -39,15 +39,12 @@ namespace sf //////////////////////////////////////////////////////////// Mutex::Mutex() { - m_mutexImpl = new priv::MutexImpl; + m_mutexImpl = std::make_shared(); } //////////////////////////////////////////////////////////// -Mutex::~Mutex() -{ - delete m_mutexImpl; -} +Mutex::~Mutex() = default; //////////////////////////////////////////////////////////// diff --git a/src/SFML/System/Thread.cpp b/src/SFML/System/Thread.cpp index 3a2c0f90..c708fdc0 100644 --- a/src/SFML/System/Thread.cpp +++ b/src/SFML/System/Thread.cpp @@ -41,7 +41,6 @@ namespace sf Thread::~Thread() { wait(); - delete m_entryPoint; } @@ -49,7 +48,7 @@ Thread::~Thread() void Thread::launch() { wait(); - m_impl = new priv::ThreadImpl(this); + m_impl = std::make_shared(this); } @@ -59,8 +58,7 @@ void Thread::wait() if (m_impl) { m_impl->wait(); - delete m_impl; - m_impl = NULL; + m_impl.reset(); } } @@ -71,8 +69,7 @@ void Thread::terminate() if (m_impl) { m_impl->terminate(); - delete m_impl; - m_impl = NULL; + m_impl.reset(); } } diff --git a/src/SFML/System/ThreadLocal.cpp b/src/SFML/System/ThreadLocal.cpp index 01e3a352..6a3cc8c7 100644 --- a/src/SFML/System/ThreadLocal.cpp +++ b/src/SFML/System/ThreadLocal.cpp @@ -39,16 +39,13 @@ namespace sf //////////////////////////////////////////////////////////// ThreadLocal::ThreadLocal(void* value) { - m_impl = new priv::ThreadLocalImpl; + m_impl = std::make_unique(); setValue(value); } //////////////////////////////////////////////////////////// -ThreadLocal::~ThreadLocal() -{ - delete m_impl; -} +ThreadLocal::~ThreadLocal() = default; //////////////////////////////////////////////////////////// diff --git a/src/SFML/Window/Context.cpp b/src/SFML/Window/Context.cpp index 321dc2cc..05bddfc0 100644 --- a/src/SFML/Window/Context.cpp +++ b/src/SFML/Window/Context.cpp @@ -50,7 +50,6 @@ Context::Context() Context::~Context() { setActive(false); - delete m_context; } diff --git a/src/SFML/Window/GlContext.cpp b/src/SFML/Window/GlContext.cpp index 396d366b..1dbbde33 100644 --- a/src/SFML/Window/GlContext.cpp +++ b/src/SFML/Window/GlContext.cpp @@ -143,7 +143,7 @@ namespace sf::ThreadLocalPtr currentContext(NULL); // The hidden, inactive context that will be shared with all other contexts - ContextType* sharedContext = NULL; + std::unique_ptr sharedContext; // This structure contains all the state necessary to // track TransientContext usage @@ -155,17 +155,15 @@ namespace //////////////////////////////////////////////////////////// TransientContext() : referenceCount (0), - context (0), - sharedContextLock(0), useSharedContext (false) { if (resourceCount == 0) { - context = new sf::Context; + context = std::make_unique(); } else if (!currentContext) { - sharedContextLock = new sf::Lock(mutex); + sharedContextLock = std::make_unique(mutex); useSharedContext = true; sharedContext->setActive(true); } @@ -179,23 +177,20 @@ namespace { if (useSharedContext) sharedContext->setActive(false); - - delete sharedContextLock; - delete context; } /////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// unsigned int referenceCount; - sf::Context* context; - sf::Lock* sharedContextLock; + std::unique_ptr context; + std::unique_ptr sharedContextLock; bool useSharedContext; }; // This per-thread variable tracks if and how a transient // context is currently being used on the current thread - sf::ThreadLocalPtr transientContext(NULL); + thread_local std::unique_ptr transientContext; // Supported OpenGL extensions std::vector extensions; @@ -224,7 +219,7 @@ void GlContext::initResource() } // Create the shared context - sharedContext = new ContextType(NULL); + sharedContext = std::make_unique(nullptr); sharedContext->initialize(ContextSettings()); // Load our extensions vector @@ -294,12 +289,8 @@ void GlContext::cleanupResource() // If there's no more resource alive, we can trigger the global context cleanup if (resourceCount == 0) { - if (!sharedContext) - return; - // Destroy the shared context - delete sharedContext; - sharedContext = NULL; + sharedContext.reset(); } } @@ -313,7 +304,7 @@ void GlContext::acquireTransientContext() // If this is the first TransientContextLock on this thread // construct the state object if (!transientContext) - transientContext = new TransientContext; + transientContext = std::make_unique(); // Increase the reference count transientContext->referenceCount++; @@ -335,22 +326,19 @@ void GlContext::releaseTransientContext() // If this is the last TransientContextLock that is released // destroy the state object if (transientContext->referenceCount == 0) - { - delete transientContext; - transientContext = NULL; - } + transientContext.reset(); } //////////////////////////////////////////////////////////// -GlContext* GlContext::create() +std::unique_ptr GlContext::create() { // Make sure that there's an active context (context creation may need extensions, and thus a valid context) assert(sharedContext != NULL); Lock lock(mutex); - GlContext* context = NULL; + std::unique_ptr context; // We don't use acquireTransientContext here since we have // to ensure we have exclusive access to the shared context @@ -359,7 +347,7 @@ GlContext* GlContext::create() sharedContext->setActive(true); // Create the context - context = new ContextType(sharedContext); + context = std::make_unique(sharedContext.get()); sharedContext->setActive(false); } @@ -371,14 +359,14 @@ GlContext* GlContext::create() //////////////////////////////////////////////////////////// -GlContext* GlContext::create(const ContextSettings& settings, const WindowImpl* owner, unsigned int bitsPerPixel) +std::unique_ptr GlContext::create(const ContextSettings& settings, const WindowImpl* owner, unsigned int bitsPerPixel) { // Make sure that there's an active context (context creation may need extensions, and thus a valid context) assert(sharedContext != NULL); Lock lock(mutex); - GlContext* context = NULL; + std::unique_ptr context; // We don't use acquireTransientContext here since we have // to ensure we have exclusive access to the shared context @@ -387,7 +375,7 @@ GlContext* GlContext::create(const ContextSettings& settings, const WindowImpl* sharedContext->setActive(true); // Create the context - context = new ContextType(sharedContext, settings, owner, bitsPerPixel); + context = std::make_unique(sharedContext.get(), settings, owner, bitsPerPixel); sharedContext->setActive(false); } @@ -400,14 +388,14 @@ GlContext* GlContext::create(const ContextSettings& settings, const WindowImpl* //////////////////////////////////////////////////////////// -GlContext* GlContext::create(const ContextSettings& settings, unsigned int width, unsigned int height) +std::unique_ptr GlContext::create(const ContextSettings& settings, unsigned int width, unsigned int height) { // Make sure that there's an active context (context creation may need extensions, and thus a valid context) assert(sharedContext != NULL); Lock lock(mutex); - GlContext* context = NULL; + std::unique_ptr context; // We don't use acquireTransientContext here since we have // to ensure we have exclusive access to the shared context @@ -416,7 +404,7 @@ GlContext* GlContext::create(const ContextSettings& settings, unsigned int width sharedContext->setActive(true); // Create the context - context = new ContextType(sharedContext, settings, width, height); + context = std::make_unique(sharedContext.get(), settings, width, height); sharedContext->setActive(false); } diff --git a/src/SFML/Window/GlContext.hpp b/src/SFML/Window/GlContext.hpp index 22efa626..5ae647cc 100644 --- a/src/SFML/Window/GlContext.hpp +++ b/src/SFML/Window/GlContext.hpp @@ -32,6 +32,7 @@ #include #include #include +#include namespace sf @@ -90,7 +91,7 @@ public: /// \return Pointer to the created context (don't forget to delete it) /// //////////////////////////////////////////////////////////// - static GlContext* create(); + static std::unique_ptr create(); //////////////////////////////////////////////////////////// /// \brief Create a new context attached to a window @@ -105,7 +106,7 @@ public: /// \return Pointer to the created context /// //////////////////////////////////////////////////////////// - static GlContext* create(const ContextSettings& settings, const WindowImpl* owner, unsigned int bitsPerPixel); + static std::unique_ptr create(const ContextSettings& settings, const WindowImpl* owner, unsigned int bitsPerPixel); //////////////////////////////////////////////////////////// /// \brief Create a new context that embeds its own rendering target @@ -120,7 +121,7 @@ public: /// \return Pointer to the created context /// //////////////////////////////////////////////////////////// - static GlContext* create(const ContextSettings& settings, unsigned int width, unsigned int height); + static std::unique_ptr create(const ContextSettings& settings, unsigned int width, unsigned int height); public: //////////////////////////////////////////////////////////// diff --git a/src/SFML/Window/Window.cpp b/src/SFML/Window/Window.cpp index 6618e5bb..1657dc99 100644 --- a/src/SFML/Window/Window.cpp +++ b/src/SFML/Window/Window.cpp @@ -42,8 +42,6 @@ namespace sf { //////////////////////////////////////////////////////////// Window::Window() : -m_impl (NULL), -m_context (NULL), m_frameTimeLimit(Time::Zero), m_size (0, 0) { @@ -53,8 +51,6 @@ m_size (0, 0) //////////////////////////////////////////////////////////// Window::Window(VideoMode mode, const String& title, Uint32 style, const ContextSettings& settings) : -m_impl (NULL), -m_context (NULL), m_frameTimeLimit(Time::Zero), m_size (0, 0) { @@ -64,8 +60,6 @@ m_size (0, 0) //////////////////////////////////////////////////////////// Window::Window(WindowHandle handle, const ContextSettings& settings) : -m_impl (NULL), -m_context (NULL), m_frameTimeLimit(Time::Zero), m_size (0, 0) { @@ -124,7 +118,7 @@ void Window::create(VideoMode mode, const String& title, Uint32 style, const Con m_impl = priv::WindowImpl::create(mode, title, style, settings); // Recreate the context - m_context = priv::GlContext::create(settings, m_impl, mode.bitsPerPixel); + m_context = priv::GlContext::create(settings, m_impl.get(), mode.bitsPerPixel); // Perform common initializations initialize(); @@ -141,7 +135,7 @@ void Window::create(WindowHandle handle, const ContextSettings& settings) m_impl = priv::WindowImpl::create(handle); // Recreate the context - m_context = priv::GlContext::create(settings, m_impl, VideoMode::getDesktopMode().bitsPerPixel); + m_context = priv::GlContext::create(settings, m_impl.get(), VideoMode::getDesktopMode().bitsPerPixel); // Perform common initializations initialize(); @@ -151,13 +145,11 @@ void Window::create(WindowHandle handle, const ContextSettings& settings) //////////////////////////////////////////////////////////// void Window::close() { - // Delete the context - delete m_context; - m_context = NULL; + // Reset the context + m_context.reset(); - // Delete the window implementation - delete m_impl; - m_impl = NULL; + // Reset the window implementation + m_impl.reset(); // Update the fullscreen window if (this == fullscreenWindow) diff --git a/src/SFML/Window/WindowImpl.cpp b/src/SFML/Window/WindowImpl.cpp index f258aca5..4df62b69 100644 --- a/src/SFML/Window/WindowImpl.cpp +++ b/src/SFML/Window/WindowImpl.cpp @@ -66,16 +66,16 @@ namespace sf namespace priv { //////////////////////////////////////////////////////////// -WindowImpl* WindowImpl::create(VideoMode mode, const String& title, Uint32 style, const ContextSettings& settings) +std::unique_ptr WindowImpl::create(VideoMode mode, const String& title, Uint32 style, const ContextSettings& settings) { - return new WindowImplType(mode, title, style, settings); + return std::make_unique(mode, title, style, settings); } //////////////////////////////////////////////////////////// -WindowImpl* WindowImpl::create(WindowHandle handle) +std::unique_ptr WindowImpl::create(WindowHandle handle) { - return new WindowImplType(handle); + return std::make_unique(handle); } diff --git a/src/SFML/Window/WindowImpl.hpp b/src/SFML/Window/WindowImpl.hpp index 96f4436e..919e78d3 100644 --- a/src/SFML/Window/WindowImpl.hpp +++ b/src/SFML/Window/WindowImpl.hpp @@ -41,6 +41,8 @@ #include #include #include +#include + namespace sf { @@ -67,7 +69,7 @@ public: /// \return Pointer to the created window (don't forget to delete it) /// //////////////////////////////////////////////////////////// - static WindowImpl* create(VideoMode mode, const String& title, Uint32 style, const ContextSettings& settings); + static std::unique_ptr create(VideoMode mode, const String& title, Uint32 style, const ContextSettings& settings); //////////////////////////////////////////////////////////// /// \brief Create a new window depending on to the current OS @@ -77,7 +79,7 @@ public: /// \return Pointer to the created window (don't forget to delete it) /// //////////////////////////////////////////////////////////// - static WindowImpl* create(WindowHandle handle); + static std::unique_ptr create(WindowHandle handle); public: