0

我试图在我的游戏中允许暂停。目前,我有一个运行良好的更改状态。对于暂停,我有:

void PushState(int newState)
{
    pauseID = stateID; ///save state number

    Gstates.push_back(currentState); ///Set Current state to vector

    nextState =  newState; ///acquire new state

    switch( nextState )
    {
    case STATE_INTRO:
        currentState = new CIntroState(); ///create new state
        break;
    }

    //Change the current state ID
    stateID = nextState;

    //NULL the next state ID
    nextState = STATE_NULL;
}

上述部分似乎运作良好。

这是我的简历部分

void Resuming()
{
    nextState = pauseID;

    if( nextState != STATE_EXIT )
    {
         delete currentState; ///deletes current state
    }

    switch( nextState )
    {
    case STATE_INTRO:
        currentState = Gstates.back(); ///sets current state to the saved state
        Gstates.pop_back(); ///delets saved state 
        break;
    }

    //Change the current state ID
    stateID = nextState;

    //NULL the next state ID
    nextState = STATE_NULL;
}

我收到一些奇怪的多线程错误。大约 50% 的时间它按预期工作大声笑,但其余时间它崩溃了。

该错误基本上是说,“很可能这是一个多线程客户端,并且没有调用 XInitThreads。

客户端不是多线程的;)..无论如何,有没有人知道发生了什么?

4

1 回答 1

0

你那里有一些危险的代码。这:

if( nextState != STATE_EXIT )
{
     delete currentState; ///deletes current state
}

将使您的“currentState”指向不存在的对象,因为它仅在 nextState 为 STATE_INTRO 时才会更新:

switch( nextState )
{
case STATE_INTRO:
    currentState = Gstates.back(); ///sets current state to the saved state
    Gstates.pop_back(); ///delets saved state 
    break;
}

这很可能是崩溃的原因。

于 2013-03-01T18:20:50.340 回答