devolution

Author Topic: cRival.cpp linklist code  (Read 1203 times)

0 Members and 1 Guest are viewing this topic.

Offline chimera

  • Newbie
  • *
  • Posts: 2
cRival.cpp linklist code
« on: January 01, 2011, 04:24:40 PM »
the game was crashing a lot for me thanks just going to drop my fix here

Code: [Select]
void cRivalManager::AddRival(cRival* rival)
    {
       
        //if rival exists
        if(m_Last!=NULL){
            m_Last->m_Next = rival;
            rival->m_Prev = m_Last;
            m_Last = rival;
            rival->m_Next = NULL;
        }
        else{
            rival->m_Next=rival->m_Prev=NULL;
            m_Rivals = m_Last = rival;
        }
        m_NumRivals++;
    }

    void cRivalManager::RemoveRival(cRival* rival)
    {
        //only 1 rival in list must be this
        if(m_Rivals==m_Last){
            m_Rivals=m_Last=NULL;
        }else{
            //not the first or last node
            if(rival != m_Rivals && rival != m_Last){
                rival->m_Next->m_Prev = rival->m_Prev;
                rival->m_Prev->m_Next = rival->m_Next;
                //is firstnode
            }else if(rival == m_Rivals){
                m_Rivals = rival->m_Next;
                m_Rivals->m_Prev =NULL;
                //is last node
            }else if(rival == m_Last){
                m_Last = rival->m_Prev;
                m_Last->m_Next = NULL;
            }
        }
        delete rival;
        rival = NULL;
        m_NumRivals--;
    }

note:something else crashed it before i could test it properly so I'm going to go fix that first

also... wtf?
~cRival(){if(m_Next)delete m_Next;m_Next=0;m_Prev=0;}
wont that recursively delete all rivals in the list after that rival?
~cRival(){m_Next=NULL;m_Prev=NULL;}

an ugly fox for the crash that was happening erlier
Code: [Select]
sGang* cGangManager::GetGangOnMission(u_int missID)
{
    // first find the gang with some men
    sGang* currentGang = m_GangStart;
    int count = 0;
    while(currentGang)
    {
        if(currentGang->m_MissionID == missID && currentGang->m_Num > 0)
            break;
        count++;
        if(currentGang->m_Next)
            currentGang = currentGang->m_Next;
        else
            break;

    }
    return currentGang;
}
if there's no gang assigned to guarding it  returns a dangling pointer so I set it to return the last in the list.
« Last Edit: January 01, 2011, 06:25:18 PM by chimera »