Fix-Request on cocos2d::Vector<T>::replace()

Fix-Request on cocos2d::Vector<T>::replace()
0.0 0

#1

Hi there, can you make the line in replace() method in cocos2d::Vector() something like this?

/** Replace value at index with given object. */
void replace(ssize_t index, T object)
{
    CCASSERT(index >= 0 && index < size(), "Invalid index!");
    CCASSERT(object != nullptr, "The object should not be nullptr");
    
    if(_data[index] != nullptr) _data[index]->release();  //<--REQUEST
    //_data[index]->release();  //original line
    _data[index] = object;
    object->retain();
}

My reason is, there will be cases where you need an empty array first then add contents to it in non-sequential order later.

I have one of this case in my current project but I made an alternate solution so it’s ok now. But I think it’s better if to update the line of code that way for future releases.


#2

An easy fix could also be not to use cocos2d::Vector at all. Use std::vector<RefPtr>.

For example:

std::vector<RefPtr<Sprite>> sprites {10, nullptr};
sprites.at(5) = Sprite::create();

#3

Using std::vector fixed this. It would be better for you to create a GitHub issue for these types of issues. Our engineering team can work on the issue easier.


#7

ugh, it’s too late here, I shouldn’t try to answer things when tired, haha (deleted them)

@esuplus I don’t believe cocos2d::Vector allows inserting nullptr, nor calling resize (as that would default construct nullptr values). Can you share the code you used where _data[index] was nullptr?