copyWithZone memory leak?

copyWithZone memory leak?
0.0 0


While implementing some custom actions I found what may be a memory leak.

Most object have a copyWithZone implementation and it’s done something like this:

CCObject* CCRepeat::copyWithZone(CCZone *pZone)

    CCZone* pNewZone = NULL;
    CCRepeat* pCopy = NULL;
    if(pZone && pZone->m_pCopyObject) 
        //in case of being called at sub class
        pCopy = (CCRepeat*)(pZone->m_pCopyObject);
        pCopy = new CCRepeat();
        pZone = pNewZone = new CCZone(pCopy);


    pCopy->initWithAction((CCFiniteTimeAction*)(m_pInnerAction->copy()->autorelease()), m_uTimes);

    return pCopy;

This is usually called with NULL as the parameter to start, so a new CCRepeat gets created. The issue I’m seeing is at CCActionInterval::copyWithZone(pZone). This is calling the base class copy, which is running the base class init. The problem is that pCopy->initWithAction((CCFiniteTimeAction*)(m_pInnerAction->copy()->autorelease()), m_uTimes) also calls the base class init inside of it (look at the implementation). This means that when copying, the base class init may get called twice. If you are calling retain() in that code, your objects will not get released because the ref count has increased by 2 instead of 1.

Is there something I am missing? This implementation of copyWithZone seems to be complicated for no reason. The original Cocos2d implementation is a simple call to create() with the original parameters.