Is OwnedArray::addArray() unsafe?

I ran into a simple RAII problem with OwnedArray, where I was trying to move objects from one Array to another, but ended up deleting them twice. This got me thinking about the addArray() method on OwnedArray–isn’t it fundamentally unsafe?

{
    OwnedArray<Component> a, b;

    a.add(new Component());

    b.addArray(a);  // Component* is now owned by both a and b
}
// scope ends, Component* is deleted by a and b. Crash!

Is there a possible use case where this method isn’t unsafe? If not, should it be allowed to compile?

1 Like

std::vector<std::unique_ptr<Component>> is simply a better choice nowadays. it’s explicit and avoids shooting yourself in the foot.

3 Likes