【问题标题】:Why does this function cause an Access Violation if accessed via a pointer?如果通过指针访问此函数,为什么会导致访问冲突?
【发布时间】:2016-06-29 14:07:19
【问题描述】:

我有以下代码:

void Aurora64::Messaging::SendConsoleMessageToPlayer(int channelId , const char *msg) 
{
    CGameRules *pGameRules = new CGameRules;
    pGameRules->SendTextMessage(eTextMessageConsole, msg, eRMI_ToClientChannel, channelId);
    delete(pGameRules);
}

它通过指针pGameRules访问另一个类中的函数,但是该函数在调用时会导致程序崩溃。

此代码在另一个文件的Aurora64 类中。


将以下代码放入指针的类CGameRules(在其中执行的函数中)时,程序崩溃:

CGameRules *pGameRules = new CGameRules;
pGameRules->SendTextMessage(eTextMessageConsole, "$4test", eRMI_ToClientChannel, channelId);
delete(pGameRules);

这是在试验 Aurora64 类文件中的某些东西是否导致了崩溃(不是)。

但是,当放在同一个类中时,程序可以完美运行而不会崩溃:

SendTextMessage(eTextMessageConsole, "$4test", eRMI_ToClientChannel, channelId);

除了指针之外,代码在功能上是相同的(使用相同的输入值)。

注释掉delete 调用无效。


调用栈是:

Aurora64.dll!IGameObject::GetEntityId() Line 301    C+Aurora64.dll!IGameObject::InvokeRMI_Primitive<CGameRules::MethodInfo_ClTextMessage,
          CGameRules::TextMessageParams>(const CGameRules::MethodInfo_ClTextMessage method, 
          const CGameRules::TextMessageParams
            & params, unsigned int where,
          IRMIListener * pListener, int userId, int channel, unsigned int dependentId)
          Line 281  C++
Aurora64.dll!IGameObject::InvokeRMI<CGameRules::MethodInfo_ClTextMessage,
          CGameRules::TextMessageParams>(const
          CGameRules::MethodInfo_ClTextMessage method, const CGameRules::TextMessageParams 
          & params, unsigned int where, int channel) 
          Line 275  C++
Aurora64.dll!CGameRules::SendTextMessage(ETextMessageType type, const char * msg, unsigned int
          to, int channelId, const char * p0, const char * p1, const char * p2, 
          const char * p3)
          Line 3075 C++
Aurora64.dll!CGameRules::OnClientConnect(int channelId, bool isReset) Line 596  C++

我不明白它为什么会崩溃...从技术上讲,代码应该可以工作,因为调用完全相同的函数,输入完全相同。

我有 source-code on GitHub should it be required(它没有 Aurora64 类,因为它是另一个基于 GitHub 的项目,但源代码没有更改)。调试它也可能需要 Crysis Wars 游戏和专用服务器包,这使得这里的任何人都难以调试。

这个问题可能很明显,我错过了。

我做错了什么?


SendTextMessage 方法:

int CScriptBind_GameRules::SendTextMessage(IFunctionHandler *pH, int type, const char *msg)
{
    CGameRules *pGameRules=GetGameRules(pH);

    if (!pGameRules)
        return pH->EndFunction();

    int to=eRMI_ToAllClients;
    int channelId=-1;

    if (pH->GetParamCount()>2)
        pH->GetParam(3, to);

    if (pH->GetParamCount()>3)
    {
        if (pH->GetParamType(4)==svtPointer)
        {
            ScriptHandle playerId;
            pH->GetParam(4, playerId);

            channelId=pGameRules->GetChannelId((EntityId)playerId.n);
        }
        else if (pH->GetParamType(4)==svtNumber)
            pH->GetParam(4, channelId);
    }

    if (pH->GetParamCount()>4)
    {
        string p[4];
        for (int i=0;i<pH->GetParamCount()-4;i++)
        {
            switch(pH->GetParamType(5+i))
            {
            case svtPointer:
                {
                    ScriptHandle sh;
                    pH->GetParam(5+i, sh);

                    if (IEntity *pEntity=gEnv->pEntitySystem->GetEntity((EntityId)sh.n))
                        p[i]=pEntity->GetName();
                }
                break;
            default:
                {
                    ScriptAnyValue value;
                    pH->GetParamAny(5+i, value);
                    switch(value.GetVarType())
                    {
                    case svtNumber:
                        p[i].Format("%g", value.number);
                        break;
                    case svtString:
                        p[i]=value.str;
                        break;
                    case svtBool:
                        p[i]=value.b?"true":"false";
                        break;
                    default:
                        break;
                    }           
                }
                break;
            }
        }
        pGameRules->SendTextMessage((ETextMessageType)type, msg, to, channelId, 
            p[0].empty()?0:p[0].c_str(),
            p[1].empty()?0:p[1].c_str(),
            p[2].empty()?0:p[2].c_str(),
            p[3].empty()?0:p[3].c_str()
            );
    }
    else
        pGameRules->SendTextMessage((ETextMessageType)type, msg, to, channelId);

    return pH->EndFunction();
}

【问题讨论】:

  • [OT] 你为什么在这里使用指针?你应该可以使用CGameRules pGameRules; pGameRules.SendTextMessage(eTextMessageConsole, msg, eRMI_ToClientChannel, channelId);
  • @NathanOliver 已更新。我会试试的;我的 C++ 有点生疏了(两年多没用了),所以如果它会这么简单,我不会感到惊讶。
  • 在这种情况下您可以取消使用指针,但您应该始终测试 new() 函数的结果以确保动态分配顺利
  • SendTextMessage的内容是什么?
  • @cybermonkey 可能你不能只创建一个CGameRules 实例并开始使用它——它必须正确设置。程序中很可能只有一个,在启动时创建,你应该使用它。

标签: c++ c++11 crash


【解决方案1】:

正如molbdnilo所解释的那样,我忽略了特定项目的对象系统:

可能你不能只创建一个 CGameRules 实例并启动 使用它 - 它必须正确设置。很可能只有一个 在程序中,在启动时创建,你应该使用它。

我在项目中寻找这样一个对象,我找到了它:

CGameRules *pGameRules=g_pGame->GetGameRules();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-30
    相关资源
    最近更新 更多