Home Game Development EXC_BAD_ACCESS While Trying to Get Data from List or Vector – C++

EXC_BAD_ACCESS While Trying to Get Data from List or Vector – C++

0
EXC_BAD_ACCESS While Trying to Get Data from List or Vector – C++

[ad_1]

The sprite pointer being NULL isn’t the issue, however moderately that it’s pointing to invalid reminiscence, as a result of the sprite has already been launched. It’s an auto-release object, so if nothing calls retain on it to extend the reference rely by 1 (see class Cocos2d::Ref), then on the subsequent replace loop it will likely be deleted by the PoolManager.

Assuming that’s the problem, you’d repair it like this:

std::vector<tlEntry> spriteEntries;

// Create sprite
tlEntry entry;
entry.s = Sprite::createXYZ() // Whatever creation technique you wish to use. Reference rely = 1 right here, and it's added to the auto-release pool (by way of a name to autorelease() within the create technique)
entry.s->retain(); // retain the sprite, so will increase ref rely by 1, so now ref rely = 2
spriteEntries.push_back(entry);

Now say you’ve reached the tip of this, and we’re on the subsequent replace loop.
Director::mainLoop() calls PoolManager::getInstance()->getCurrentPool()->clear(); to launch all objects within the pool
The PoolManager checks the auto-release object assortment, and for every merchandise, it decrements the reference rely by 1.
So, for this sprite, ref rely = 2 – 1 = 1
The PoolManager then checks if ref rely == 0, and whether it is, it calls object->delete(). In this case, the sprite nonetheless has a ref rely of 1.
All objects within the auto-release pool assortment are eliminated, so they’ll now not mechanically checked in future updates

At this level the sprite you created has a ref rely = 1. The solely manner this sprite may be eliminated and reminiscence related to it correctly freed is to name launch() on it, which is what you’re liable for.

So, whenever you now not want the sprite within the checklist, you do that:
addChild(entries.entrance().s); // sprite added to a guardian, which calls retain() on it, so now ref rely = 2.

// At any level you wish to take away the sprite/sprites from the checklist, you can’t merely name std::vector::clear(). You have to name launch on the entire sprite first, then clear the checklist

for (auto&& entry : spriteEntries)
{
    entry.s->launch();
}
spriteEntries.clear();

If you simply wish to take away 1 sprite from the checklist, as an example, the entry on the finish, then do that:

auto entry = spriteEntries.pop_back(); // Removes the entry from the checklist
entry.s->launch(); // Release/free the sprite. You may name CC_SAFE_RELEASE_NULL(entry.s) or CC_SAFE_RELEASE(entry.s) as an alternative

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here