Question about CCDictionary code


#1
void CCDictionary::setObject(CCObject* pObject, const std::string& key)
{
    CCAssert(key.length() > 0 && pObject != NULL, "Invalid Argument!");
    if (m_eDictType == kCCDictUnknown)
    {   
        m_eDictType = kCCDictStr;
    }   

    CCAssert(m_eDictType == kCCDictStr, "this dictionary doesn't use string as key.");

    CCDictElement *pElement = NULL;
    HASH_FIND_STR(m_pElements, key.c_str(), pElement);
    if (pElement == NULL)
    {   
        setObjectUnSafe(pObject, key);
    }   
    else if (pElement->m_pObject != pObject)
    {   
        CCObject* pTmpObj = pElement->m_pObject;
        pTmpObj->retain();
        removeObjectForElememt(pElement);
        setObjectUnSafe(pObject, key);
        pTmpObj->release();
    }   
}

I don’t understand why first retain and then release the pTmpObj. Could someone explain for me, thanks.


#2

The first retain is done to ensure the removeObjectForElememt will not remove that object due because it will decrese the retain count by one in that object.
Later on when the setObjectUnSafe is called the retain count is increased by one (retain count 2) so it should decrease by one indicating that the only retain is the dictionary itself.