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.

juce_DeletedAtShutdown.cpp 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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. // Disable unreachable code warning, in case the compiler manages to figure out that
  36. // you have no classes of DeletedAtShutdown that could throw an exception in their destructor.
  37. JUCE_BEGIN_IGNORE_WARNINGS_MSVC (4702)
  38. void DeletedAtShutdown::deleteAll()
  39. {
  40. // make a local copy of the array, so it can't get into a loop if something
  41. // creates another DeletedAtShutdown object during its destructor.
  42. Array<DeletedAtShutdown*> localCopy;
  43. {
  44. const SpinLock::ScopedLockType sl (deletedAtShutdownLock);
  45. localCopy = getDeletedAtShutdownObjects();
  46. }
  47. for (int i = localCopy.size(); --i >= 0;)
  48. {
  49. JUCE_TRY
  50. {
  51. auto* deletee = localCopy.getUnchecked(i);
  52. // double-check that it's not already been deleted during another object's destructor.
  53. {
  54. const SpinLock::ScopedLockType sl (deletedAtShutdownLock);
  55. if (! getDeletedAtShutdownObjects().contains (deletee))
  56. deletee = nullptr;
  57. }
  58. delete deletee;
  59. }
  60. JUCE_CATCH_EXCEPTION
  61. }
  62. // if this fails, then it's likely that some new DeletedAtShutdown objects were
  63. // created while executing the destructors of the other ones.
  64. jassert (getDeletedAtShutdownObjects().isEmpty());
  65. getDeletedAtShutdownObjects().clear(); // just to make sure the array doesn't have any memory still allocated
  66. }
  67. JUCE_END_IGNORE_WARNINGS_MSVC
  68. } // namespace juce