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.

86 lines
2.8KB

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