The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
3.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. static SpinLock deletedAtShutdownLock; // use a spin lock because it can be statically initialised
  18. static Array<DeletedAtShutdown*>& getDeletedAtShutdownObjects()
  19. {
  20. static Array<DeletedAtShutdown*> objects;
  21. return objects;
  22. }
  23. DeletedAtShutdown::DeletedAtShutdown()
  24. {
  25. const SpinLock::ScopedLockType sl (deletedAtShutdownLock);
  26. getDeletedAtShutdownObjects().add (this);
  27. }
  28. DeletedAtShutdown::~DeletedAtShutdown()
  29. {
  30. const SpinLock::ScopedLockType sl (deletedAtShutdownLock);
  31. getDeletedAtShutdownObjects().removeFirstMatchingValue (this);
  32. }
  33. #if JUCE_MSVC
  34. // Disable unreachable code warning, in case the compiler manages to figure out that
  35. // you have no classes of DeletedAtShutdown that could throw an exception in their destructor.
  36. #pragma warning (push)
  37. #pragma warning (disable: 4702)
  38. #endif
  39. void DeletedAtShutdown::deleteAll()
  40. {
  41. // make a local copy of the array, so it can't get into a loop if something
  42. // creates another DeletedAtShutdown object during its destructor.
  43. Array<DeletedAtShutdown*> localCopy;
  44. {
  45. const SpinLock::ScopedLockType sl (deletedAtShutdownLock);
  46. localCopy = getDeletedAtShutdownObjects();
  47. }
  48. for (int i = localCopy.size(); --i >= 0;)
  49. {
  50. JUCE_TRY
  51. {
  52. auto* deletee = localCopy.getUnchecked(i);
  53. // double-check that it's not already been deleted during another object's destructor.
  54. {
  55. const SpinLock::ScopedLockType sl (deletedAtShutdownLock);
  56. if (! getDeletedAtShutdownObjects().contains (deletee))
  57. deletee = nullptr;
  58. }
  59. delete deletee;
  60. }
  61. JUCE_CATCH_EXCEPTION
  62. }
  63. // if this fails, then it's likely that some new DeletedAtShutdown objects were
  64. // created while executing the destructors of the other ones.
  65. jassert (getDeletedAtShutdownObjects().isEmpty());
  66. getDeletedAtShutdownObjects().clear(); // just to make sure the array doesn't have any memory still allocated
  67. }
  68. #if JUCE_MSVC
  69. #pragma warning (pop)
  70. #endif