Assertion failed - com->getOwner() == nullptr

So I have a class that inherits Sprite (I call it Brick). In my main layer, I declare and initialize 46 of those objects and push them into a std::vector.

The class that inherits the sprite looks like this:

#include "Brick.h"
#include "LevelOne.h"

Brick::~Brick()
{
	CC_SAFE_RELEASE(appearAnimate);
	CC_SAFE_RELEASE(disintegrateAnimate);
}


Brick * Brick::create(int x, int y)
{
	Brick * brick = new Brick();
	if (brick && brick->initWithFile("brick1.png"))
	{
		brick->autorelease();
		brick->initBrick(x,y);
		return brick;
	}

	CC_SAFE_DELETE(brick);
	return NULL;
}

void Brick::initBrick(int x, int y)
{
	char str[100] = { 0 };
	Vector<SpriteFrame*> appearFrames(5);
	for (int i = 5; i <= 1; i--) //Iterate for the number of images you have
	{
		sprintf(str, "brick%i.png", i);
		auto frame = SpriteFrame::create(str, Rect(0, 0, 32, 32)); //The size of the images in an action should be the same
		appearFrames.pushBack(frame);
	}

	auto appearAnimation = Animation::createWithSpriteFrames(appearFrames, 0.20f);
	appearAnimate = Animate::create(appearAnimation);
	appearAnimate->retain(); //Retain to use it later

	Vector<SpriteFrame*> disintegrateFrames(5);
	for (int i = 1; i <= 5; i++)
	{
		sprintf(str, "brick%i.png", i);
		auto frame = SpriteFrame::create(str, Rect(0, 0, 32, 32));
		disintegrateFrames.pushBack(frame);
	}


	auto disintegrateAnimation = Animation::createWithSpriteFrames(disintegrateFrames, 0.12f);
	disintegrateAnimate = Animate::create(disintegrateAnimation);
	disintegrateAnimate->retain();

	brickbox = Sprite::create("brick1.png");
	this->setAnchorPoint(Point(0, 0));
	this->setPosition(Point(brickbox->getBoundingBox().size.width * x, brickbox->getBoundingBox().size.height * y));
	brickbody = PhysicsBody::createBox(Size(32,32), PhysicsMaterial(0, 0, 0));
	brickbody->setCollisionBitmask(4);
	brickbody->setContactTestBitmask(true);
	brickbody->setDynamic(false);
	this->setPhysicsBody(brickbody);
}

void Brick::appear()
{
	this->runAction(appearAnimate);
}
void Brick::deletion()
{
	this->setOpacity(0);
	this->brickbody->removeFromWorld();
}
void Brick::disintegrate()
{
	this->runAction(disintegrateAnimate);
}
void Brick::creation()
{
	this->setOpacity(255);
	this->setPhysicsBody(brickbody);
}
void Brick::drill()
{
	CCDelayTime *delay1 = CCDelayTime::create(1.5f);
	CCDelayTime *delay2 = CCDelayTime::create(3.0f);
	CallFunc *disintegrate = CallFunc::create([this]()
	{
		this->disintegrate();
	});

	CallFunc *deletion = CallFunc::create([this]()
	{
		this->deletion();
	});

	CallFunc *appear = CallFunc::create([this]()
	{
		this->appear();
	});

	CallFunc *brickcreate = CallFunc::create([this]()
	{
		this->creation();
	});

	runAction(Sequence::create(disintegrate, delay1, deletion, delay2, Spawn::create(appear, brickcreate), NULL));
}

When I call the method drill like this: bricks.at(brickpos)->drill();

where brickpos is the position of the object in the vector and the bricks is the vector itself, I get the error:

Sometimes the compiler either points me that he couldn’t setphysicsbody or to getOwner() method.

I see following errors:
This loop will not work:

 for (int i = 5; i <= 1; i--)

You create vectors with 5 elements (nullptr) but later call pushBack which will append new elements at end:

 Vector<SpriteFrame*> appearFrames(5);
 Vector<SpriteFrame*> disintegrateFrames(5);

I fail to see how that’s the problem. I have numerous pushback loops like this one for other sprites’ animations and they work perfectly. Besides, the disintegrateFrames animation plays properly, then I receive the error.

Vector(n) reserves n positions in terms of the capacity of the vector, but it doesn’t actually resize the vector. There is a difference.

For instance, if you do this:
Vector<SpriteFrame*> appearFrames(5);

appearFrames->empty() will return true
appearFrames->size() will return 0

Internal allocated capacity is 5, so that means that it won’t resize every time you add at item to the list as long as it’s up to 5 items. It’s purely for efficiency, since you are avoiding constant memory allocations/copies. For more info, check this out:

C++ vector, resizes:

image

But cocos Vector is different, it just reserves:

    /** 
     * Constructor with a capacity. 
     * @param capacity Capacity of the Vector.
     */
    explicit Vector<T>(ssize_t capacity)
    : _data()
    {
        static_assert(std::is_convertible<T, Ref*>::value, "Invalid Type for cocos2d::Vector<T>!");
        CCLOGINFO("In the default constructor with capacity of Vector.");
        reserve(capacity);
    }

Yes, that is correct, and perhaps my link to the stackoverflow confused the issue a bit (it was only meant to show the benefits of reserve). You can see that Celestialray’s code is utilizing cocos2d::Vector, not std::vector, so 5 spots are not being allocated, and the code is still correct in using push_back().

The example you showed is using std::vector, so it is allocating 5 positions, and push_back() would cause problems, but that is not the case in Celestialray’s code.

Yes but still problem with loop.

You’re right, I used to have a problem at playing the animation, but then I modified the declaration of the vector and now I don’t have that problem anymore.