diff --git a/src/driver.cpp b/src/driver.cpp index 86a823b71d..28dec081f4 100644 --- a/src/driver.cpp +++ b/src/driver.cpp @@ -139,20 +139,18 @@ bool DriverFactoryBase::SelectDriverImpl(const std::string &name, Driver::Type t FioFOpenFile(HWACCELERATION_TEST_FILE, "w", BASE_DIR); } - Driver *oldd = *GetActiveDriver(type); - Driver *newd = d->CreateInstance(); - *GetActiveDriver(type) = newd; + /* Keep old driver in case we need to switch back, or may still need to process an OS callback. */ + auto oldd = std::move(GetActiveDriver(type)); + GetActiveDriver(type) = d->CreateInstance(); - auto err = newd->Start({}); + auto err = GetActiveDriver(type)->Start({}); if (!err) { Debug(driver, 1, "Successfully probed {} driver '{}'", GetDriverTypeName(type), d->name); - delete oldd; return true; } - *GetActiveDriver(type) = oldd; + GetActiveDriver(type) = std::move(oldd); Debug(driver, 1, "Probing {} driver '{}' failed with error: {}", GetDriverTypeName(type), d->name, *err); - delete newd; if (type == Driver::DT_VIDEO && _video_hw_accel && d->UsesHardwareAcceleration()) { _video_hw_accel = false; @@ -185,17 +183,14 @@ bool DriverFactoryBase::SelectDriverImpl(const std::string &name, Driver::Type t if (!StrEqualsIgnoreCase(dname, d->name)) continue; /* Found our driver, let's try it */ - Driver *newd = d->CreateInstance(); - + auto newd = d->CreateInstance(); auto err = newd->Start(parms); if (err) { - delete newd; UserError("Unable to load driver '{}'. The error was: {}", d->name, *err); } Debug(driver, 1, "Successfully loaded {} driver '{}'", GetDriverTypeName(type), d->name); - delete *GetActiveDriver(type); - *GetActiveDriver(type) = newd; + GetActiveDriver(type) = std::move(newd); return true; } UserError("No such {} driver: {}\n", GetDriverTypeName(type), dname); diff --git a/src/driver.h b/src/driver.h index 73fb677464..e827a61898 100644 --- a/src/driver.h +++ b/src/driver.h @@ -81,10 +81,10 @@ private: * @param type The type to get the driver for. * @return The active driver. */ - static Driver **GetActiveDriver(Driver::Type type) + static std::unique_ptr &GetActiveDriver(Driver::Type type) { - static Driver *s_driver[3] = { nullptr, nullptr, nullptr }; - return &s_driver[type]; + static std::array, Driver::DT_END> s_driver{}; + return s_driver[type]; } /** @@ -123,7 +123,7 @@ public: static void ShutdownDrivers() { for (Driver::Type dt = Driver::DT_BEGIN; dt < Driver::DT_END; dt++) { - Driver *driver = *GetActiveDriver(dt); + auto &driver = GetActiveDriver(dt); if (driver != nullptr) driver->Stop(); } } @@ -144,7 +144,7 @@ public: * Create an instance of this driver-class. * @return The instance. */ - virtual Driver *CreateInstance() const = 0; + virtual std::unique_ptr CreateInstance() const = 0; }; #endif /* DRIVER_H */ diff --git a/src/music/allegro_m.h b/src/music/allegro_m.h index 6e1588b9a5..eee250897b 100644 --- a/src/music/allegro_m.h +++ b/src/music/allegro_m.h @@ -41,7 +41,7 @@ public: static const int PRIORITY = 2; #endif FMusicDriver_Allegro() : DriverFactoryBase(Driver::DT_MUSIC, PRIORITY, "allegro", "Allegro MIDI Driver") {} - Driver *CreateInstance() const override { return new MusicDriver_Allegro(); } + std::unique_ptr CreateInstance() const override { return std::make_unique(); } }; #endif /* MUSIC_ALLEGRO_H */ diff --git a/src/music/bemidi.h b/src/music/bemidi.h index 47529aa51d..d63d93c046 100644 --- a/src/music/bemidi.h +++ b/src/music/bemidi.h @@ -41,7 +41,7 @@ private: class FMusicDriver_BeMidi : public DriverFactoryBase { public: FMusicDriver_BeMidi() : DriverFactoryBase(Driver::DT_MUSIC, 10, "bemidi", "BeOS MIDI Driver") {} - Driver *CreateInstance() const override { return new MusicDriver_BeMidi(); } + std::unique_ptr CreateInstance() const override { return std::make_unique(); } }; #endif /* MUSIC_BEMIDI_H */ diff --git a/src/music/cocoa_m.h b/src/music/cocoa_m.h index dbf58e425e..c0387302ac 100644 --- a/src/music/cocoa_m.h +++ b/src/music/cocoa_m.h @@ -31,7 +31,7 @@ public: class FMusicDriver_Cocoa : public DriverFactoryBase { public: FMusicDriver_Cocoa() : DriverFactoryBase(Driver::DT_MUSIC, 10, "cocoa", "Cocoa MIDI Driver") {} - Driver *CreateInstance() const override { return new MusicDriver_Cocoa(); } + std::unique_ptr CreateInstance() const override { return std::make_unique(); } }; #endif /* MUSIC_MACOSX_COCOA_H */ diff --git a/src/music/dmusic.h b/src/music/dmusic.h index 32f0943d85..a988539861 100644 --- a/src/music/dmusic.h +++ b/src/music/dmusic.h @@ -35,7 +35,7 @@ public: class FMusicDriver_DMusic : public DriverFactoryBase { public: FMusicDriver_DMusic() : DriverFactoryBase(Driver::DT_MUSIC, 10, "dmusic", "DirectMusic MIDI Driver") {} - Driver *CreateInstance() const override { return new MusicDriver_DMusic(); } + std::unique_ptr CreateInstance() const override { return std::make_unique(); } }; #endif /* MUSIC_DMUSIC_H */ diff --git a/src/music/extmidi.h b/src/music/extmidi.h index 42db725f34..cb62b396dd 100644 --- a/src/music/extmidi.h +++ b/src/music/extmidi.h @@ -39,7 +39,7 @@ public: class FMusicDriver_ExtMidi : public DriverFactoryBase { public: FMusicDriver_ExtMidi() : DriverFactoryBase(Driver::DT_MUSIC, 3, "extmidi", "External MIDI Driver") {} - Driver *CreateInstance() const override { return new MusicDriver_ExtMidi(); } + std::unique_ptr CreateInstance() const override { return std::make_unique(); } }; #endif /* MUSIC_EXTERNAL_H */ diff --git a/src/music/fluidsynth.h b/src/music/fluidsynth.h index ae7a8385d8..6a20ffd81d 100644 --- a/src/music/fluidsynth.h +++ b/src/music/fluidsynth.h @@ -33,7 +33,7 @@ public: class FMusicDriver_FluidSynth : public DriverFactoryBase { public: FMusicDriver_FluidSynth() : DriverFactoryBase(Driver::DT_MUSIC, 5, "fluidsynth", "FluidSynth MIDI Driver") {} - Driver *CreateInstance() const override { return new MusicDriver_FluidSynth(); } + std::unique_ptr CreateInstance() const override { return std::make_unique(); } }; #endif /* MUSIC_FLUIDSYNTH_H */ diff --git a/src/music/music_driver.hpp b/src/music/music_driver.hpp index eaa5d63ddb..80b889e647 100644 --- a/src/music/music_driver.hpp +++ b/src/music/music_driver.hpp @@ -45,7 +45,7 @@ public: */ static MusicDriver *GetInstance() { - return static_cast(*DriverFactoryBase::GetActiveDriver(Driver::DT_MUSIC)); + return static_cast(DriverFactoryBase::GetActiveDriver(Driver::DT_MUSIC).get()); } }; diff --git a/src/music/null_m.h b/src/music/null_m.h index 9c2ca82855..09b0c57404 100644 --- a/src/music/null_m.h +++ b/src/music/null_m.h @@ -33,7 +33,7 @@ public: class FMusicDriver_Null : public DriverFactoryBase { public: FMusicDriver_Null() : DriverFactoryBase(Driver::DT_MUSIC, 1, "null", "Null Music Driver") {} - Driver *CreateInstance() const override { return new MusicDriver_Null(); } + std::unique_ptr CreateInstance() const override { return std::make_unique(); } }; #endif /* MUSIC_NULL_H */ diff --git a/src/music/win32_m.h b/src/music/win32_m.h index bd6125420b..1f860f3d96 100644 --- a/src/music/win32_m.h +++ b/src/music/win32_m.h @@ -33,7 +33,7 @@ public: class FMusicDriver_Win32 : public DriverFactoryBase { public: FMusicDriver_Win32() : DriverFactoryBase(Driver::DT_MUSIC, 5, "win32", "Win32 Music Driver") {} - Driver *CreateInstance() const override { return new MusicDriver_Win32(); } + std::unique_ptr CreateInstance() const override { return std::make_unique(); } }; #endif /* MUSIC_WIN32_H */ diff --git a/src/sound/allegro_s.h b/src/sound/allegro_s.h index 1026038355..658d9dfd83 100644 --- a/src/sound/allegro_s.h +++ b/src/sound/allegro_s.h @@ -27,7 +27,7 @@ public: class FSoundDriver_Allegro : public DriverFactoryBase { public: FSoundDriver_Allegro() : DriverFactoryBase(Driver::DT_SOUND, 4, "allegro", "Allegro Sound Driver (param hz,samples)") {} - Driver *CreateInstance() const override { return new SoundDriver_Allegro(); } + std::unique_ptr CreateInstance() const override { return std::make_unique(); } }; #endif /* SOUND_ALLEGRO_H */ diff --git a/src/sound/cocoa_s.h b/src/sound/cocoa_s.h index 0f32e25b4d..74bb83783f 100644 --- a/src/sound/cocoa_s.h +++ b/src/sound/cocoa_s.h @@ -23,7 +23,7 @@ public: class FSoundDriver_Cocoa : public DriverFactoryBase { public: FSoundDriver_Cocoa() : DriverFactoryBase(Driver::DT_SOUND, 10, "cocoa", "Cocoa Sound Driver (param hz)") {} - Driver *CreateInstance() const override { return new SoundDriver_Cocoa(); } + std::unique_ptr CreateInstance() const override { return std::make_unique(); } }; #endif /* SOUND_COCOA_H */ diff --git a/src/sound/null_s.h b/src/sound/null_s.h index fb931b84dd..e6c041ac70 100644 --- a/src/sound/null_s.h +++ b/src/sound/null_s.h @@ -26,7 +26,7 @@ public: class FSoundDriver_Null : public DriverFactoryBase { public: FSoundDriver_Null() : DriverFactoryBase(Driver::DT_SOUND, 1, "null", "Null Sound Driver") {} - Driver *CreateInstance() const override { return new SoundDriver_Null(); } + std::unique_ptr CreateInstance() const override { return std::make_unique(); } }; #endif /* SOUND_NULL_H */ diff --git a/src/sound/sdl_s.h b/src/sound/sdl_s.h index abf9ac6a20..5633fd7149 100644 --- a/src/sound/sdl_s.h +++ b/src/sound/sdl_s.h @@ -25,7 +25,7 @@ public: class FSoundDriver_SDL : public DriverFactoryBase { public: FSoundDriver_SDL() : DriverFactoryBase(Driver::DT_SOUND, 5, "sdl", "SDL Sound Driver (param hz,samples)") {} - Driver *CreateInstance() const override { return new SoundDriver_SDL(); } + std::unique_ptr CreateInstance() const override { return std::make_unique(); } }; #endif /* SOUND_SDL_H */ diff --git a/src/sound/sound_driver.hpp b/src/sound/sound_driver.hpp index 300433e5ae..fcf2ec2775 100644 --- a/src/sound/sound_driver.hpp +++ b/src/sound/sound_driver.hpp @@ -34,7 +34,7 @@ public: */ static SoundDriver *GetInstance() { - return static_cast(*DriverFactoryBase::GetActiveDriver(Driver::DT_SOUND)); + return static_cast(DriverFactoryBase::GetActiveDriver(Driver::DT_SOUND).get()); } }; diff --git a/src/sound/win32_s.h b/src/sound/win32_s.h index c89e8596d4..9fee145393 100644 --- a/src/sound/win32_s.h +++ b/src/sound/win32_s.h @@ -25,7 +25,7 @@ public: class FSoundDriver_Win32 : public DriverFactoryBase { public: FSoundDriver_Win32() : DriverFactoryBase(Driver::DT_SOUND, 9, "win32", "Win32 WaveOut Sound Driver (param hz,samples)") {} - Driver *CreateInstance() const override { return new SoundDriver_Win32(); } + std::unique_ptr CreateInstance() const override { return std::make_unique(); } }; #endif /* SOUND_WIN32_H */ diff --git a/src/sound/xaudio2_s.h b/src/sound/xaudio2_s.h index b4e2591fb4..5f378fa98b 100644 --- a/src/sound/xaudio2_s.h +++ b/src/sound/xaudio2_s.h @@ -25,7 +25,7 @@ public: class FSoundDriver_XAudio2 : public DriverFactoryBase { public: FSoundDriver_XAudio2() : DriverFactoryBase(Driver::DT_SOUND, 10, "xaudio2", "XAudio2 Sound Driver (param hz,samples)") {} - Driver *CreateInstance() const override { return new SoundDriver_XAudio2(); } + std::unique_ptr CreateInstance() const override { return std::make_unique(); } }; #endif /* SOUND_XAUDIO2_H */ diff --git a/src/video/allegro_v.h b/src/video/allegro_v.h index 8789b1158f..551112c91e 100644 --- a/src/video/allegro_v.h +++ b/src/video/allegro_v.h @@ -46,7 +46,7 @@ protected: class FVideoDriver_Allegro : public DriverFactoryBase { public: FVideoDriver_Allegro() : DriverFactoryBase(Driver::DT_VIDEO, 4, "allegro", "Allegro Video Driver") {} - Driver *CreateInstance() const override { return new VideoDriver_Allegro(); } + std::unique_ptr CreateInstance() const override { return std::make_unique(); } }; #endif /* VIDEO_ALLEGRO_H */ diff --git a/src/video/cocoa/cocoa_ogl.h b/src/video/cocoa/cocoa_ogl.h index a238fbce63..e22e11c9df 100644 --- a/src/video/cocoa/cocoa_ogl.h +++ b/src/video/cocoa/cocoa_ogl.h @@ -58,7 +58,7 @@ protected: class FVideoDriver_CocoaOpenGL : public DriverFactoryBase { public: FVideoDriver_CocoaOpenGL() : DriverFactoryBase(Driver::DT_VIDEO, 9, "cocoa-opengl", "Cocoa OpenGL Video Driver") {} - Driver *CreateInstance() const override { return new VideoDriver_CocoaOpenGL(); } + std::unique_ptr CreateInstance() const override { return std::make_unique(); } protected: bool UsesHardwareAcceleration() const override { return true; } diff --git a/src/video/cocoa/cocoa_v.h b/src/video/cocoa/cocoa_v.h index 24e606c052..727a9b140b 100644 --- a/src/video/cocoa/cocoa_v.h +++ b/src/video/cocoa/cocoa_v.h @@ -129,7 +129,7 @@ protected: class FVideoDriver_CocoaQuartz : public DriverFactoryBase { public: FVideoDriver_CocoaQuartz() : DriverFactoryBase(Driver::DT_VIDEO, 8, "cocoa", "Cocoa Video Driver") {} - Driver *CreateInstance() const override { return new VideoDriver_CocoaQuartz(); } + std::unique_ptr CreateInstance() const override { return std::make_unique(); } }; #endif /* VIDEO_COCOA_H */ diff --git a/src/video/dedicated_v.h b/src/video/dedicated_v.h index f1271075d8..1c64386440 100644 --- a/src/video/dedicated_v.h +++ b/src/video/dedicated_v.h @@ -41,7 +41,7 @@ public: static const int PRIORITY = 0; #endif FVideoDriver_Dedicated() : DriverFactoryBase(Driver::DT_VIDEO, PRIORITY, "dedicated", "Dedicated Video Driver") {} - Driver *CreateInstance() const override { return new VideoDriver_Dedicated(); } + std::unique_ptr CreateInstance() const override { return std::make_unique(); } }; #endif /* VIDEO_DEDICATED_H */ diff --git a/src/video/null_v.h b/src/video/null_v.h index 23f3e64038..407e447e7d 100644 --- a/src/video/null_v.h +++ b/src/video/null_v.h @@ -37,7 +37,7 @@ public: class FVideoDriver_Null : public DriverFactoryBase { public: FVideoDriver_Null() : DriverFactoryBase(Driver::DT_VIDEO, 0, "null", "Null Video Driver") {} - Driver *CreateInstance() const override { return new VideoDriver_Null(); } + std::unique_ptr CreateInstance() const override { return std::make_unique(); } }; #endif /* VIDEO_NULL_H */ diff --git a/src/video/sdl2_default_v.h b/src/video/sdl2_default_v.h index 066aa5e056..86b09bcbd8 100644 --- a/src/video/sdl2_default_v.h +++ b/src/video/sdl2_default_v.h @@ -33,7 +33,7 @@ private: class FVideoDriver_SDL_Default : public DriverFactoryBase { public: FVideoDriver_SDL_Default() : DriverFactoryBase(Driver::DT_VIDEO, 5, "sdl", "SDL Video Driver") {} - Driver *CreateInstance() const override { return new VideoDriver_SDL_Default(); } + std::unique_ptr CreateInstance() const override { return std::make_unique(); } }; #endif /* VIDEO_SDL2_DEFAULT_H */ diff --git a/src/video/sdl2_opengl_v.h b/src/video/sdl2_opengl_v.h index b1e2f09f04..f237d420a8 100644 --- a/src/video/sdl2_opengl_v.h +++ b/src/video/sdl2_opengl_v.h @@ -52,7 +52,7 @@ private: class FVideoDriver_SDL_OpenGL : public DriverFactoryBase { public: FVideoDriver_SDL_OpenGL() : DriverFactoryBase(Driver::DT_VIDEO, 8, "sdl-opengl", "SDL OpenGL Video Driver") {} - /* virtual */ Driver *CreateInstance() const override { return new VideoDriver_SDL_OpenGL(); } + /* virtual */ std::unique_ptr CreateInstance() const override { return std::make_unique(); } protected: bool UsesHardwareAcceleration() const override { return true; } diff --git a/src/video/video_driver.hpp b/src/video/video_driver.hpp index 7d13e0a80f..d79d4c393c 100644 --- a/src/video/video_driver.hpp +++ b/src/video/video_driver.hpp @@ -200,7 +200,7 @@ public: */ static VideoDriver *GetInstance() { - return static_cast(*DriverFactoryBase::GetActiveDriver(Driver::DT_VIDEO)); + return static_cast(DriverFactoryBase::GetActiveDriver(Driver::DT_VIDEO).get()); } static std::string GetCaption(); diff --git a/src/video/win32_v.h b/src/video/win32_v.h index 4344910b01..9b2cb8535e 100644 --- a/src/video/win32_v.h +++ b/src/video/win32_v.h @@ -110,7 +110,7 @@ public: class FVideoDriver_Win32GDI : public DriverFactoryBase { public: FVideoDriver_Win32GDI() : DriverFactoryBase(Driver::DT_VIDEO, 9, "win32", "Win32 GDI Video Driver") {} - Driver *CreateInstance() const override { return new VideoDriver_Win32GDI(); } + std::unique_ptr CreateInstance() const override { return std::make_unique(); } }; #ifdef WITH_OPENGL @@ -168,7 +168,7 @@ protected: class FVideoDriver_Win32OpenGL : public DriverFactoryBase { public: FVideoDriver_Win32OpenGL() : DriverFactoryBase(Driver::DT_VIDEO, 10, "win32-opengl", "Win32 OpenGL Video Driver") {} - /* virtual */ Driver *CreateInstance() const override { return new VideoDriver_Win32OpenGL(); } + /* virtual */ std::unique_ptr CreateInstance() const override { return std::make_unique(); } protected: bool UsesHardwareAcceleration() const override { return true; }