OpenShot Library | OpenShotAudio 0.2.2
juce_DeletedAtShutdown.cpp
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2017 - ROLI Ltd.
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
23namespace juce
24{
25
26static SpinLock deletedAtShutdownLock; // use a spin lock because it can be statically initialised
27
28static 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#if JUCE_MSVC
47 // Disable unreachable code warning, in case the compiler manages to figure out that
48 // you have no classes of DeletedAtShutdown that could throw an exception in their destructor.
49 #pragma warning (push)
50 #pragma warning (disable: 4702)
51#endif
52
54{
55 // make a local copy of the array, so it can't get into a loop if something
56 // creates another DeletedAtShutdown object during its destructor.
58
59 {
60 const SpinLock::ScopedLockType sl (deletedAtShutdownLock);
61 localCopy = getDeletedAtShutdownObjects();
62 }
63
64 for (int i = localCopy.size(); --i >= 0;)
65 {
66 JUCE_TRY
67 {
68 auto* deletee = localCopy.getUnchecked(i);
69
70 // double-check that it's not already been deleted during another object's destructor.
71 {
72 const SpinLock::ScopedLockType sl (deletedAtShutdownLock);
73
74 if (! getDeletedAtShutdownObjects().contains (deletee))
75 deletee = nullptr;
76 }
77
78 delete deletee;
79 }
80 JUCE_CATCH_EXCEPTION
81 }
82
83 // if this fails, then it's likely that some new DeletedAtShutdown objects were
84 // created while executing the destructors of the other ones.
85 jassert (getDeletedAtShutdownObjects().isEmpty());
86
87 getDeletedAtShutdownObjects().clear(); // just to make sure the array doesn't have any memory still allocated
88}
89
90#if JUCE_MSVC
91 #pragma warning (pop)
92#endif
93
94} // namespace juce
Holds a resizable array of primitive or copy-by-value objects.
Definition: juce_Array.h:60
ElementType getUnchecked(int index) const
Returns one of the elements in the array, without checking the index passed in.
Definition: juce_Array.h:256
int size() const noexcept
Returns the current number of elements in the array.
Definition: juce_Array.h:219
static void deleteAll()
Deletes all extant objects.
virtual ~DeletedAtShutdown()
Destructor.
DeletedAtShutdown()
Creates a DeletedAtShutdown object.
Automatically locks and unlocks a mutex object.