OpenShot Audio Library | OpenShotAudio  0.6.0
juce_DeletedAtShutdown.cpp
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2022 - Raw Material Software Limited
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  The code included in this file is provided under the terms of the ISC license
11  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12  To use, copy, modify, and/or distribute this software for any purpose with or
13  without fee is hereby granted provided that the above copyright notice and
14  this permission notice appear in all copies.
15 
16  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18  DISCLAIMED.
19 
20  ==============================================================================
21 */
22 
23 namespace juce
24 {
25 
26 static SpinLock deletedAtShutdownLock; // use a spin lock because it can be statically initialised
27 
28 static Array<DeletedAtShutdown*>& getDeletedAtShutdownObjects()
29 {
30  static Array<DeletedAtShutdown*> objects;
31  return objects;
32 }
33 
35 {
36  const SpinLock::ScopedLockType sl (deletedAtShutdownLock);
37  getDeletedAtShutdownObjects().add (this);
38 }
39 
41 {
42  const SpinLock::ScopedLockType sl (deletedAtShutdownLock);
43  getDeletedAtShutdownObjects().removeFirstMatchingValue (this);
44 }
45 
46 // Disable unreachable code warning, in case the compiler manages to figure out that
47 // you have no classes of DeletedAtShutdown that could throw an exception in their destructor.
48 JUCE_BEGIN_IGNORE_WARNINGS_MSVC (4702)
49 
50 void DeletedAtShutdown::deleteAll()
51 {
52  // make a local copy of the array, so it can't get into a loop if something
53  // creates another DeletedAtShutdown object during its destructor.
54  Array<DeletedAtShutdown*> localCopy;
55 
56  {
57  const SpinLock::ScopedLockType sl (deletedAtShutdownLock);
58  localCopy = getDeletedAtShutdownObjects();
59  }
60 
61  for (int i = localCopy.size(); --i >= 0;)
62  {
63  JUCE_TRY
64  {
65  auto* deletee = localCopy.getUnchecked (i);
66 
67  // double-check that it's not already been deleted during another object's destructor.
68  {
69  const SpinLock::ScopedLockType sl (deletedAtShutdownLock);
70 
71  if (! getDeletedAtShutdownObjects().contains (deletee))
72  deletee = nullptr;
73  }
74 
75  delete deletee;
76  }
77  JUCE_CATCH_EXCEPTION
78  }
79 
80  // if this fails, then it's likely that some new DeletedAtShutdown objects were
81  // created while executing the destructors of the other ones.
82  jassert (getDeletedAtShutdownObjects().isEmpty());
83 
84  getDeletedAtShutdownObjects().clear(); // just to make sure the array doesn't have any memory still allocated
85 }
86 
87 JUCE_END_IGNORE_WARNINGS_MSVC
88 
89 } // namespace juce
ElementType getUnchecked(int index) const
Definition: juce_Array.h:252
int size() const noexcept
Definition: juce_Array.h:215