-1

“节目意外结束了。”

我有一堂课正在打电话CMem::Write()。并将迭代显示到屏幕上。有时它会达到 140,其他... 12、3、42,立即掉出...非常随机。
如果我删除对 的调用 CMem::Write(),程序将永远运行。

不知道为什么程序终止?我只能假设某些东西不是写在CMem::Write()方法中的。

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
}
4

2 回答 2

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
}

上面的代码仅在第一个实例中将 m_posRead 和 m_posWrite 初始化为 0。对于所有其他实例,它是未定义的。

其次,在你正在做的构造函数中

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

现在,这使得 m_cMessageCB 只有 1 个字节宽。同时,在 CMem::Write 你正在做

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

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

此处 m_posWrite 已递增。第一次它将写入第 0 个索引。下次您调用 CMem:Write 时,它​​将尝试在第一个索引上写入,即“Array Out Of Bound Write”(因为 m_cMessageCB 只有 1 个字节宽)并且它的行为是未定义的。它可能会在下一次写入或任何其他未来写入时终止。

于 2013-08-13T17:49:03.867 回答
0

这些行似乎是此代码的问题之一:

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

为什么只在第一个实例上初始化索引?其他实例将使这些成员未初始化,因此显然它们会破坏内存。

编辑(关于评论):

好的,您可以将它们设为静态,但这表明您的设计存在严重问题。但这里是题外话。在使它们成为静态之后,另一个问题仍然存在:m_posWrite 变量只会递增,而不会重置/递减 - 您如何期望它不会超出范围?

于 2013-08-13T17:37:58.753 回答