Wednesday, August 31, 2022
HomeGame DevelopmentEXC_BAD_ACCESS Whereas Making an attempt to Get Knowledge from Record or Vector...

EXC_BAD_ACCESS Whereas Making an attempt to Get Knowledge from Record or Vector – C++


The sprite pointer being NULL will not be the issue, however relatively 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 difficulty, you’ll repair it like this:

std::vector<tlEntry> spriteEntries;

// Create sprite
tlEntry entry;
entry.s = Sprite::createXYZ() // No matter creation technique you need 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(). On 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 routinely checked in future updates

At this level the sprite you created has a ref rely = 1. The one means this sprite will be eliminated and reminiscence related to it correctly freed is to name launch() on it, which is what you might be chargeable for.

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

// At any level you need to take away the sprite/sprites from the checklist, you can’t merely name std::vector::clear(). It’s essential to name launch on all the sprite first, then clear the checklist

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

In the event you simply need 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(); // Launch/free the sprite. You may as well name CC_SAFE_RELEASE_NULL(entry.s) or CC_SAFE_RELEASE(entry.s) as an alternative

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments