Search code examples
c++circular-buffer

Circular buffer writing randomly terminates the program?


"The program has unexpectedly finished."

I have a class that is calling CMem::Write(). And it Displays the iteration to the screen. Sometimes it reaches 140, others... 12, 3, 42, falls out right away... very random.
If I remove the call to CMem::Write(), the program will run forever.

Not sure why it the program termination? All I can assume is that something is not write in CMem::Write() method.

CMem::CMem()//sets up the "static" stack memory and the pointers to it; also the "static" positionIndex
{
    m_nBufferLength = sizeof(char); //short int
    static char *cMessageCB = new char[m_nBufferLength];
    static double *dTimeCB = new double[m_nBufferLength];

    m_cMessageCB = cMessageCB;
    m_dTimeCB = dTimeCB;


    ////////////////////////////////////////
    static char *cMessageReadList = new char[m_nBufferLength]; //max size can be the CB
    static double *dTimeReadList = new double[m_nBufferLength]; //max size can be the CB

    m_cMessageReadList = cMessageReadList;
    m_dTimeReadList = dTimeReadList;

    static int firstInstance = 0;

    if(firstInstance == 0){
        m_posRead = 0;//only on first instance
        m_posWrite = 0;//only on first instance

        firstInstance++;//check to see if multiple threads entered at the same time and look at the count
    }
}


void CMem::Write()
{//double dTime, char cMessage
//only one thread can write at a time... so lock... (make other threads with various random delays)

    static bool bUse = false;
    bool bDone = false;


    while(bDone == false){

        if(bUse == false){
            bUse = true;


            m_cMessageCB[m_posWrite] = m_cMessageWrite;
            m_dTimeCB[m_posWrite] = m_dTimeWrite;

            m_posWrite = (unsigned char)(m_posWrite + 1);


            static char cFlag = 0;
            //if writing position == reading position then flag
            if(m_posWrite == m_posRead){
                cFlag = 1;
            }

            bDone = true;
            bUse = false;
        }else if(bUse == true){
            printf("SUSPEND ");
        }
    }
}


void CMem::Read()
{//get the whole block of memory and increment the m_posRead accordingly
    unsigned char j = 0;

    while( (m_posRead + 1) != (m_posWrite + 1) ){
        m_cMessageReadList[j] = m_cMessageCB[m_posRead];//inc m_posRead at the end
        m_dTimeReadList[j] = m_dTimeCB[m_posRead];//inc m_posRead at the end

        m_posRead = (unsigned char)(m_posRead + 1);//circulate around
        j++;// 'j' is not circulating back around
    }

    //write to file
}

Solution

  • With whatever code you have provided, it seems it is a clear case of memory corruption

    First

    if(firstInstance == 0){
        m_posRead = 0;//only on first instance
        m_posWrite = 0;//only on first instance
    
        firstInstance++;//check to see if multiple threads entered at the same time and look at the count
    }
    

    Above code initializes m_posRead and m_posWrite to 0 only for first instance. FOr all other instances, it is undefined.

    Secondly, in the constructor you are doing

    m_nBufferLength = sizeof(char); //short int
    static char *cMessageCB = new char[m_nBufferLength];
    m_cMessageCB = cMessageCB;
    

    Now, this makes m_cMessageCB only 1 byte wide. While, in CMem::Write you are doing

    m_cMessageCB[m_posWrite] = m_cMessageWrite;
    m_dTimeCB[m_posWrite] = m_dTimeWrite;
    
    m_posWrite = (unsigned char)(m_posWrite + 1);
    

    Here m_posWrite has been incremented. First time it will write on 0th index. Next time you call CMem:Write and it will try to write on 1st index which is "Array Out Of Bound Write" (because m_cMessageCB is only 1 byte wide)and it's behaviour is undefined. It may terminate on the next write or any other future writes.