OwnedArray - ContainerDeletePolicy error

Hello,
for sometime I was producing code and made big mess with OwnedArray objects. I copied them to other objects and made many other things which are probably stupid.
And every time I finish debugging just by click “stop” in my compiler (xCode) so there was no problem.

But last time I’ve just closed my application normally by clicking on OSX the command-Q. And suddenly I’ve found out I get some error. And almost all day I was trying to fix it but with no success.

There is comment in Juce code that tries to explain the reason of error and help to solve it. But unfortunately my English is probably not good enough and I am not sure what to do.

The JUCE comment is here:

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 OwnedArray of a forward-declared type, move the
        // implementation of all methods trying to use the OwnedArray (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;
    }
};

I thought the problem is implementation (definition) of my classes methods in header file. So I moved all definitions to cpp files, and in header I left only declarations of methods. But it doesn’t help.

Now I wonder to use std::vector<myClasses*> to hold my pointers instead of OwnedArray, but I feel it’s not good way.

So now I think to use myOwnedArray.clear(false) in all my destructors. But I am also not sure if it’s good idea, because finally I want to delete all objects in OwnedArray so I am afraid OwnedArray::clear(false) will make even bigger mess in my code.

Does anyone know how to solve such problem easily and fast?

For any help great thanks in advance.

Best Regards

Good luck with that! :sweat_smile:

If that’s the error you’re actually getting, it means you’re making an array of a class whose definition is not visible there. Like

// this file
class YourClass; // forward declaration, the class is incomplete
juce::OwnedArray<YourClass> array;
// some other file
class YourClass
{
    // etc.
}

OK, great thanks for your answer.
But is there any way to check which class exactly makes that problem?

I’m not on OSX, but if you’re catching the error in Xcode you should be able to see the type of this, which should include that.

Ok let’s stop here though. The code you quoted should trigger a compiler error, not a runtime one. Your program is running, so this is not the error you’re getting. Find out what is really the error you’re getting in the first place.

You need to have it in the debugger and go the call stack outwards. This is triggered from the OwnedArray destructor, that is triggered from the class that owns the OwnedArray. Now you should be able to see what kind the wrapped type in the OwnedArray is and so on.

Hello kamedin
Yes it definitely happens during runtime and the the error happens exactly in that place:

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 OwnedArray of a forward-declared type, move the
        // implementation of all methods trying to use the OwnedArray (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; //I GET ERROR HERE, AND ERROR SAYS: JUCE Message Thread (1): signal SIGABRT
    }
}

I think @kamedin and myself were caught by a red herring. The problem we thought only happens at compile time.

It crashes on delete object;, which means the object was already deleted somewhere else.
You need to be careful not to add the same object in two OwnedArrays, not in a std::unique_ptr and for sure don’t delete it manually.
It must be owned in exactly one place.

Or some AudioSources can own the source they pull from, they also must not be kept in an OwnedArray.

Hope that helps