This may well be a lack of fully understanding RAII techniques, but I’m hitting a bad access error on shutdown in a small audio playback app in juce_ContainerDeletePolicy.h:
template <typename ObjectType>
struct ContainerDeletePolicy
{
static void destroy (ObjectType* object)
{
// If the line below triggers a compiler error, it means that you are using
// an incomplete type for ObjectType (for example, a type that is declared
// but not defined). This is a problem because then the following delete is
// undefined behaviour. The purpose of the sizeof is to capture this situation.
// If this was caused by a ScopedPointer to a forward-declared type, move the
// implementation of all methods trying to use the ScopedPointer (e.g. the destructor
// of the class owning it) into cpp files where they can see to the definition
// of ObjectType. This should fix the error.
ignoreUnused (sizeof (ObjectType));
delete object; // <---- EXC_BAD_ACCESS here
}
};
If I use only use a class member ScopedPointer<AudioFormatReader> formatReader,
or - if I use a function variable of the same type e.g.
and then assign it to the member with formatReader = reader.release() I get a bad access error on the AudioFormatReader destructor trying to delete input.
You can use raw pointers for those things the new source takes ownership. Or call release inside:
ScopedPointer<AudioFormatReaderSource> newSource = new AudioFormatReaderSource (reader.release(), true);
Otherwise there is a time when two objects claim ownership. And newSource is allowed to delete the reader before you call the release.
The call release at the end is not how it is done. In a perfect world you would never need to call release. If you wanted to delete the objects at the end, simply set the ScopedPointer to nullptr:
readerSource = nullptr; // deletes the readerSource
formatReader = nullptr; // deletes the formatReader
while
readerSopurce.release(); // leaves a dangling pointer
And you even don’t need to set it to nullptr, because when the ScopedPointer on the stack goes out of scope, it deletes the object it points to.