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.

80 lines
2.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. static SpinLock deletedAtShutdownLock;
  18. DeletedAtShutdown::DeletedAtShutdown()
  19. {
  20. const SpinLock::ScopedLockType sl (deletedAtShutdownLock);
  21. getObjects().add (this);
  22. }
  23. DeletedAtShutdown::~DeletedAtShutdown()
  24. {
  25. const SpinLock::ScopedLockType sl (deletedAtShutdownLock);
  26. getObjects().removeFirstMatchingValue (this);
  27. }
  28. void DeletedAtShutdown::deleteAll()
  29. {
  30. // make a local copy of the array, so it can't get into a loop if something
  31. // creates another DeletedAtShutdown object during its destructor.
  32. Array <DeletedAtShutdown*> localCopy;
  33. {
  34. const SpinLock::ScopedLockType sl (deletedAtShutdownLock);
  35. localCopy = getObjects();
  36. }
  37. for (int i = localCopy.size(); --i >= 0;)
  38. {
  39. JUCE_TRY
  40. {
  41. DeletedAtShutdown* deletee = localCopy.getUnchecked(i);
  42. // double-check that it's not already been deleted during another object's destructor.
  43. {
  44. const SpinLock::ScopedLockType sl (deletedAtShutdownLock);
  45. if (! getObjects().contains (deletee))
  46. deletee = nullptr;
  47. }
  48. delete deletee;
  49. }
  50. JUCE_CATCH_EXCEPTION
  51. }
  52. // if no objects got re-created during shutdown, this should have been emptied by their
  53. // destructors
  54. jassert (getObjects().size() == 0);
  55. getObjects().clear(); // just to make sure the array doesn't have any memory still allocated
  56. }
  57. Array <DeletedAtShutdown*>& DeletedAtShutdown::getObjects()
  58. {
  59. static Array <DeletedAtShutdown*> objects;
  60. return objects;
  61. }