Hey all,
I’m trying to make a good particle/object pool manager for myself, and it works good - until I restart the Scene and try to use it again… I’m a beginner at c++ so maybe it’s something easy I’m missing. Anyways, here are some more details…
Usage:
fxMgr = Fx::create();
fxMgr->retain();
greenStarPoolIdx = fxMgr->addPool(fxNode, Fx::FxType::GREEN_STAR, 2);
later
auto star = fxMgr->getGreenStar(greenStarPoolIdx);
(do something with the star)
I recently made it so that the Fx class inherits from Ref. I used to have it as a unique_ptr to avoind having to deal w/ memory management, when changed it to inherit Ref to let cocos handle it instead.
Since I called retain, I have this in my scene’s destructor (can’t use addChild on it since it doesn’t inherit Node):
fxMgr->release();
Inside the Fx class; (except for methods for getting particles and adding object pools):
virtual ~Fx();
Fx();
Vector<ParticlePool*> _pools; <- used have it as std::vector<ParticlePool*>
ParticlePool.h (also inherits from Ref now to use cocos's own reference counting)
int particleIdx;
Vector<Node*> _pool;
virtual ~ParticlePool();
ParticlePool();
public:
Node* nextItem();
void add(Node* item);
static ParticlePool* create();
particle pool destructor
ParticlePool::~ParticlePool()
{
CCLOG("pool dtor");
for (size_t i = 0; i < _pool.size(); i++)
{
_pool.at(i)->release();
}
}
Now, to the problem
When going to another scene, using breakpts, I can see the destructors for the Fx- and ParticlePool class being called, but not my GreenStar class (which the pool is holding).
Edit: updated the constructor to call release on all the items, now the destructor is called for those classes too. But I thought cocos Vectors would do this automatically? I don’t need to delete _pool though, right?
I can use the fxManager once, then when I restart or exit and re-enter the scene and try to use it again, it’s "0x00000000 " when I’m about to use it (after a key press).
I just can’t understand. Even if it was a memory leak I was at least expecting it to work since I’m making a new object? I need help.