From fe0a6b00ceb695547b33fda10c846ea51f42c0ab Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Wed, 1 Jan 2020 16:11:39 +0100 Subject: [PATCH] - blocked the destructor in the sound font reader base class. If ZMusic is to act like an external library it may not call delete on external objects because there is no guarantee that they use the same allocator. Deletion must be done as a virtual function to ensure that the correct operator delete gets called, which, unlike the actual destructor is not virtual itself. --- libraries/music_common/fileio.h | 8 +++++--- libraries/timidity/instrum.cpp | 2 +- libraries/timidityplus/instrum.cpp | 2 +- libraries/wildmidi/wildmidi_lib.cpp | 2 +- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/libraries/music_common/fileio.h b/libraries/music_common/fileio.h index 1a26f2db4..0c592a3da 100644 --- a/libraries/music_common/fileio.h +++ b/libraries/music_common/fileio.h @@ -224,8 +224,8 @@ struct VectorReader : public MemoryReader //========================================================================== // -// The follpwing two functions are needed to allow using UTF-8 in the file interface. -// fopen on Windows is only safe for ASCII, +// The following two functions are needed to allow using UTF-8 in the file interface. +// fopen on Windows is only safe for ASCII. // //========================================================================== @@ -271,10 +271,12 @@ inline bool fileExists(const char *fn) class SoundFontReaderInterface { -public: +protected: virtual ~SoundFontReaderInterface() {} +public: virtual struct FileInterface* open_file(const char* fn) = 0; virtual void add_search_path(const char* path) = 0; + virtual void close() { delete this; } }; diff --git a/libraries/timidity/instrum.cpp b/libraries/timidity/instrum.cpp index bd6224cc4..9240de456 100644 --- a/libraries/timidity/instrum.cpp +++ b/libraries/timidity/instrum.cpp @@ -703,7 +703,7 @@ Instruments::~Instruments() drumset[i] = NULL; } } - if (sfreader != nullptr) delete sfreader; + if (sfreader != nullptr) sfreader->close(); sfreader = nullptr; } diff --git a/libraries/timidityplus/instrum.cpp b/libraries/timidityplus/instrum.cpp index b133af280..431147dcf 100644 --- a/libraries/timidityplus/instrum.cpp +++ b/libraries/timidityplus/instrum.cpp @@ -73,7 +73,7 @@ Instruments::~Instruments() free_tone_bank(); free_instrument_map(); - if (sfreader != nullptr) delete sfreader; + if (sfreader != nullptr) sfreader->close(); } void Instruments::free_instrument(Instrument *ip) diff --git a/libraries/wildmidi/wildmidi_lib.cpp b/libraries/wildmidi/wildmidi_lib.cpp index 3401e997a..9286b9825 100644 --- a/libraries/wildmidi/wildmidi_lib.cpp +++ b/libraries/wildmidi/wildmidi_lib.cpp @@ -1326,7 +1326,7 @@ void Instruments::load_patch(struct _mdi *mdi, unsigned short patchid) Instruments::~Instruments() { FreePatches(); - delete sfreader; + sfreader->close(); }