Audio plugin host https://kx.studio/carla
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.

95 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. namespace juce
  18. {
  19. static SpinLock deletedAtShutdownLock; // use a spin lock because it can be statically initialised
  20. static Array<DeletedAtShutdown*>& getDeletedAtShutdownObjects()
  21. {
  22. static Array<DeletedAtShutdown*> objects;
  23. return objects;
  24. }
  25. DeletedAtShutdown::DeletedAtShutdown()
  26. {
  27. const SpinLock::ScopedLockType sl (deletedAtShutdownLock);
  28. getDeletedAtShutdownObjects().add (this);
  29. }
  30. DeletedAtShutdown::~DeletedAtShutdown()
  31. {
  32. const SpinLock::ScopedLockType sl (deletedAtShutdownLock);
  33. getDeletedAtShutdownObjects().removeFirstMatchingValue (this);
  34. }
  35. #if JUCE_MSVC
  36. // Disable unreachable code warning, in case the compiler manages to figure out that
  37. // you have no classes of DeletedAtShutdown that could throw an exception in their destructor.
  38. #pragma warning (push)
  39. #pragma warning (disable: 4702)
  40. #endif
  41. void DeletedAtShutdown::deleteAll()
  42. {
  43. // make a local copy of the array, so it can't get into a loop if something
  44. // creates another DeletedAtShutdown object during its destructor.
  45. Array<DeletedAtShutdown*> localCopy;
  46. {
  47. const SpinLock::ScopedLockType sl (deletedAtShutdownLock);
  48. localCopy = getDeletedAtShutdownObjects();
  49. }
  50. for (int i = localCopy.size(); --i >= 0;)
  51. {
  52. JUCE_TRY
  53. {
  54. auto* deletee = localCopy.getUnchecked(i);
  55. // double-check that it's not already been deleted during another object's destructor.
  56. {
  57. const SpinLock::ScopedLockType sl (deletedAtShutdownLock);
  58. if (! getDeletedAtShutdownObjects().contains (deletee))
  59. deletee = nullptr;
  60. }
  61. delete deletee;
  62. }
  63. JUCE_CATCH_EXCEPTION
  64. }
  65. // if this fails, then it's likely that some new DeletedAtShutdown objects were
  66. // created while executing the destructors of the other ones.
  67. jassert (getDeletedAtShutdownObjects().isEmpty());
  68. getDeletedAtShutdownObjects().clear(); // just to make sure the array doesn't have any memory still allocated
  69. }
  70. #if JUCE_MSVC
  71. #pragma warning (pop)
  72. #endif
  73. } // namespace juce