diff --git a/aegisub/src/ass_file.cpp b/aegisub/src/ass_file.cpp index 51e47fde1..27e8643aa 100644 --- a/aegisub/src/ass_file.cpp +++ b/aegisub/src/ass_file.cpp @@ -68,16 +68,14 @@ namespace std { } } -/// @brief AssFile constructor AssFile::AssFile () : commitId(0) , loaded(false) { } -/// @brief AssFile destructor AssFile::~AssFile() { - delete_clear(Line); + background_delete_clear(Line); } /// @brief Load generic subs @@ -386,7 +384,7 @@ void AssFile::AddLine(wxString data, int *version, AssAttachment **attach) { } void AssFile::Clear() { - delete_clear(Line); + background_delete_clear(Line); loaded = false; filename.clear(); diff --git a/aegisub/src/utils.h b/aegisub/src/utils.h index 09a4b4bf1..ee6278a87 100644 --- a/aegisub/src/utils.h +++ b/aegisub/src/utils.h @@ -46,6 +46,7 @@ #include #include +#include #endif class wxMouseEvent; @@ -97,18 +98,53 @@ template inline T mid(T a, T b, T c) { return std::max(a, std::min(b #endif #endif +/// Polymorphic delete functor struct delete_ptr { template void operator()(T* ptr) const { delete ptr; } }; + +/// Delete all of the items in a container of pointers and clear the container template void delete_clear(T& container) { - std::for_each(container.begin(), container.end(), delete_ptr()); - container.clear(); + if (!container.empty()) { + std::for_each(container.begin(), container.end(), delete_ptr()); + container.clear(); + } } +/// Helper class for background_delete_clear +template +class BackgroundDeleter : public wxThread { + Container cont; + wxThread::ExitCode Entry() { + delete_clear(cont); + return (wxThread::ExitCode)0; + } +public: + BackgroundDeleter(Container &source) + : wxThread(wxTHREAD_DETACHED) + { + using std::swap; + swap(cont, source); + + SetPriority(WXTHREAD_MIN_PRIORITY); + Create(); + Run(); + } +}; + +/// Clear a container of pointers and delete the pointed to members on a +/// background thread +template +void background_delete_clear(T& container) { + if (!container.empty()) + new BackgroundDeleter(container); +} + + template struct cast { template