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.

81 lines
2.7KB

  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. static SpinLock deletedAtShutdownLock;
  19. DeletedAtShutdown::DeletedAtShutdown()
  20. {
  21. const SpinLock::ScopedLockType sl (deletedAtShutdownLock);
  22. getObjects().add (this);
  23. }
  24. DeletedAtShutdown::~DeletedAtShutdown()
  25. {
  26. const SpinLock::ScopedLockType sl (deletedAtShutdownLock);
  27. getObjects().removeFirstMatchingValue (this);
  28. }
  29. void DeletedAtShutdown::deleteAll()
  30. {
  31. // make a local copy of the array, so it can't get into a loop if something
  32. // creates another DeletedAtShutdown object during its destructor.
  33. Array <DeletedAtShutdown*> localCopy;
  34. {
  35. const SpinLock::ScopedLockType sl (deletedAtShutdownLock);
  36. localCopy = getObjects();
  37. }
  38. for (int i = localCopy.size(); --i >= 0;)
  39. {
  40. JUCE_TRY
  41. {
  42. DeletedAtShutdown* deletee = localCopy.getUnchecked(i);
  43. // double-check that it's not already been deleted during another object's destructor.
  44. {
  45. const SpinLock::ScopedLockType sl (deletedAtShutdownLock);
  46. if (! getObjects().contains (deletee))
  47. deletee = nullptr;
  48. }
  49. delete deletee;
  50. }
  51. JUCE_CATCH_EXCEPTION
  52. }
  53. // if no objects got re-created during shutdown, this should have been emptied by their
  54. // destructors
  55. jassert (getObjects().size() == 0);
  56. getObjects().clear(); // just to make sure the array doesn't have any memory still allocated
  57. }
  58. Array <DeletedAtShutdown*>& DeletedAtShutdown::getObjects()
  59. {
  60. static Array <DeletedAtShutdown*> objects;
  61. return objects;
  62. }