OpenShot Audio Library | OpenShotAudio: juce_DeletedAtShutdown.cpp Source File

OpenShot Audio Library | OpenShotAudio 0.3.1

Loading...
Searching...
No Matches
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
34DeletedAtShutdown::DeletedAtShutdown()
35{
36 const SpinLock::ScopedLockType sl (deletedAtShutdownLock);
37 getDeletedAtShutdownObjects().add (this);
38}
39
40DeletedAtShutdown::~DeletedAtShutdown()
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
53 void DeletedAtShutdown::deleteAll()
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.
57 Array<DeletedAtShutdown*> localCopy;
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
juce::Array
Definition: juce_Array.h:56
juce::Array::getUnchecked
ElementType getUnchecked(int index) const
Definition: juce_Array.h:252
juce::Array::size
int size() const noexcept
Definition: juce_Array.h:215
juce::DeletedAtShutdown::deleteAll
static void deleteAll()
Definition: juce_DeletedAtShutdown.cpp:53
juce::DeletedAtShutdown::~DeletedAtShutdown
virtual ~DeletedAtShutdown()
Definition: juce_DeletedAtShutdown.cpp:40
juce::DeletedAtShutdown::DeletedAtShutdown
DeletedAtShutdown()
Definition: juce_DeletedAtShutdown.cpp:34
juce::GenericScopedLock
Definition: juce_ScopedLock.h:55