C ++程序崩溃在Python脚本调用上

时间:2014-06-01 18:26:31

标签: python c++ integration

我正在尝试发送一个运行特定python脚本的命令:但是,只要程序到达执行行,就会发生这种情况:

GameServer.exe中0x69bd1f16处的未处理异常:0xC0000005:访问冲突读取位置0x46f520ca。

程序停止共振和崩溃。这是有问题的方法:

void ScriptManager::runScript(std::string scriptName, std::string args[])
{
    std::string py = "python " + scriptName;
    std::cout << py << std::endl;
    for(int i = 0; i < args->length(); i++)
    {
        py += " " + args[i];
        std::cout << py << std::endl;
    }
    std::cout << py << std::endl;

    std::system(py.c_str());

}

这称为上述功能:

void DBFactory::dbRegisterUser(std::string username, std::string password)
{
    ScriptManager script;
    std::string data[] = {username, password};

    script.runScript("Python.py", data);
}

据我所知,该脚本无法运行。我也可以发布脚本,如果它会有所帮助。

1 个答案:

答案 0 :(得分:1)

这是问题所在:

for (int i = 0; i < args->length(); i++)
{
    py += " " + args[i];
    std::cout << py << std::endl;
}

args->length()相当于args[0].length();即你正在获取数组中第一个字符串的长度并将其用作索引。经过两次迭代后,您将访问数组末尾。最好的解决方案是(所有例子都是UNTESTED):

  1. 使用std::array(仅限C ++ 11):

    void DBFactory::dbRegisterUser(std::string username, std::string password)
    {
        ScriptManager script;
    
        script.runScript("Python.py", {username, password});
    }
    
    void ScriptManager::runScript(std::string scriptName, std::array<std::string, 2> args)
    {
        std::string py = "python " + scriptName;
        std::cout << py << std::endl;
        for (std::string s : args)
        {
                py += " " + s;
                std::cout << py << std::endl;
        }
        std::cout << py << std::endl;
    
        std::system(py.c_str());
    }
    
  2. 使用std::vector(该示例使用C ++ 03):

    void DBFactory::dbRegisterUser(std::string username, std::string password)
    {
        ScriptManager script;
        int tmp[2] = {username, password};
    
        script.runScript("Python.py", std::vector<std::string>(&tmp[0], &tmp[0]+2));
    }
    
    void ScriptManager::runScript(std::string scriptName, std::vector<std::string> args)
    {
        std::string py = "python " + scriptName;
        std::cout << py << std::endl;
        for(std::vector<std::string>::iterator it = args.begin(); it != args.end(); it++)
        {
            py += " " + *it;
            std::cout << py << std::endl;
        }
        std::cout << py << std::endl;
    
        std::system(py.c_str());
        }
    
  3. 将数组大小作为参数传递:

    void DBFactory::dbRegisterUser(std::string username, std::string password)
    {
        ScriptManager script;
    
        script.runScript("Python.py", {username, password}, 2);
    }
    
    void ScriptManager::runScript(std::string scriptName, std::string args[], int size)
    {
        std::string py = "python " + scriptName;
        std::cout << py << std::endl;
        for(int i=0; i<size; i++)
        {
            py += " " + args[i];
            std::cout << py << std::endl;
        }
        std::cout << py << std::endl;
    
        std::system(py.c_str());
    }
    
  4. 我个人更喜欢示例1,并会像瘟疫一样避免示例3。示例2效果很好,但可能没有示例1那么快。