简体   繁体   English

C++ 未处理异常:无效参数被传递给认为无效参数致命的函数

[英]C++ Unhandled exception: An invalid parameter was passed to a function that considers invalid parameters fatal

I'm doing a group project at school, we're creating a 2d game, and I'm getting an exception I have no idea what to do with.我在学校做一个小组项目,我们正在创建一个 2d 游戏,但我遇到了一个异常,我不知道该怎么办。

A side note: all of this is done in debug mode.旁注:所有这些都是在调试模式下完成的。

We have a physicsengine class that calculates collision and adds vector forces to entities, the code:我们有一个物理引擎类,用于计算碰撞并向实体添加矢量力,代码:

void PhysicsEngine::doPhysicsTick()
{
    vector<vector<unordered_set<Entity*>>> *grid = this->grid.getGrid();
    unordered_set<Entity*>* updatable = new unordered_set<Entity*>();
    unordered_set<Entity*>* collidable = new unordered_set<Entity*>();

    const int &r = CollisionGrid::C_GRID_RADIUS;

    for (int row = 0; row < grid->size(); row++)
    for (int col = 0; col < grid->at(row).size(); col++)
    {
        collidable->clear();

        // put all surrounding grid blocks in a entity list to
        // check against collision with the current block

        int rw(row - r > 0 ? row - r : 0);
        int cl(col - r > 0 ? col - r : 0);

        for (int rrow = rw; rrow <= row + r && rrow < grid->size(); rrow++)
        for (int rcol = cl; rcol <= col + r && rcol < grid->at(rrow).size(); rcol++)
            for (Entity *e : grid->at(rrow).at(rcol)) // It crashes here
                collidable->insert(e);

        for (Entity *e : grid->at(row).at(col))
        {
            if (e->isFixed())
                continue;

            e->speed += gravity;

            eBounds.reBox(e->location, e->getSize() + e->speed);

            for (Entity *c : *collidable)
                if (isOverlapping(eBounds, Box(c, c->getSize())) && e != c)
                    doCollision(e, c);

            updatable->insert(e);
        }
    }

We have a separate collisiongrid class that manages the grid where we store our entities for collision check (so we don't have to check everything with everything)我们有一个单独的碰撞网格类来管理我们存储实体以进行碰撞检查的网格(因此我们不必检查所有内容)

the collisiongrid and its underlying vector are created when the constructor of the physicsengine is called PhysicsEngine::PhysicsEngine(World *world) : grid(world) .当物理引擎的构造函数称为PhysicsEngine::PhysicsEngine(World *world) : grid(world)时,将创建碰撞PhysicsEngine::PhysicsEngine(World *world) : grid(world)及其底层vector But for some reason on only the first tick I see the vector grid points to something nonexistant (size being ridiculously large and such) inside of the loop that fills up collidable.但是出于某种原因,仅在第一个刻度上,我看到矢量网格指向填充可碰撞的循环内部不存在的东西(尺寸大得离谱等)。

The error it throws is in the title.它抛出的错误在标题中。 If I put a catch block around it, it'll just crash someplace else in some c++ library kind of randomly (different file every time)如果我在它周围放一个 catch 块,它只会在某些 C++ 库中的其他地方随机崩溃(每次都不同的文件)

and for some reason it crashes in our gameloop class' (the one calling tick of physicsengine) destructor if I don't comment the thing I put in there.并且由于某种原因,如果我不评论我放在那里的东西,它会在我们的游戏循环类(物理引擎的一个调用滴答声)析构函数中崩溃。

Gameloop::~Gameloop()
{
    //if( t ) // t = #include <thread>
    //  delete t;
}

We are not allowed to use any libraries outside of the Win32 API and some default c++ libraries我们不允许使用 Win32 API 之外的任何库和一些默认的 c++ 库


Edit: I added some pictures (and undid the dynamic allocation of the unordered_sets)编辑:我添加了一些图片(并取消了 unordered_sets 的动态分配)

what it should show:它应该显示什么:

正确的网格

what it sometimes shows on the first tick: (notice how the pointers are the same, first two shot where made during the same run)它有时会在第一个刻度上显示:(注意指针是如何相同的,在同一次运行中进行的前两次射击)

在此处输入图片说明

what it shows other times on the first tick:它在第一个刻度上显示的其他时间:

在此处输入图片说明

Does your program handle tasks?你的程序处理任务吗? Because this may solve your problem in that case (those kind of exceptions never crash the program at the right time!)因为在这种情况下可能会解决您的问题(这些异常永远不会在正确的时间使程序崩溃!)

If not, then you need to debug the program some more.如果没有,那么您需要更多地调试程序。 Like printing rrow , rrcol , grid->size() , grid->at(rrow)->size everytime before the line where it crashes.像打印rrowrrcolgrid->size() grid->at(rrow)->size ,其中它崩溃前行每次。

But my money is on the concurrency / memory management side of the code.但我的钱是在代码的并发/内存管理方面。 The fact that it crashes in the destructor in the delete part, makes me think maybe you are handling items that were already deleted elsewhere or concurrently handling items without the appropriate measures.它在删除部分的析构函数中崩溃这一事实让我觉得您可能正在处理已在其他地方删除的项目,或者在没有采取适当措施的情况下同时处理项目。 And in your screenshot you have a bunch of size=???在你的截图中你有一堆size=??? which maybe means that your item/instance was deleted and your pointer points to free memory, causing the crash when accessing it in your loop.这可能意味着您的项目/实例已被删除并且您的指针指向可用内存,导致在循环中访问它时崩溃。 It's a tough error to solve.这是一个很难解决的错误。

Additionnally, you should be able to access parameters of your exception in the debugger, if you can, maybe you can post all of its contents?此外,您应该能够在调试器中访问异常的参数,如果可以,也许您可​​以发布其所有内容?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C++ 一个无效参数被传递给一个认为无效参数致命的 function - C++ An invalid parameter was passed to a function that considers invalid parameters fatal 未处理的异常:一个无效的参数被传递给 function,它认为无效参数在 UWP 应用程序中是致命的 - Unhandled exception: An invalid parameter was passed to a function that considers invalid parameters fatal in UWP application 将无效参数传递给认为无效参数致命的 function? - An invalid parameter was passed to a function that considers invalid parameters fatal? C ++:无效的参数传递给C运行时函数 - C++: Invalid parameter passed to C runtime function 通过QTest将无效的参数传递给C运行时函数 - Invalid parameter passed to C runtime function with QTest C++ 中的自定义异常用于函数的无效输入 - Custom exception in C++ for invalid input to a function 如何调试“传递给 C 运行时函数的参数无效”? - how to debug “Invalid parameter passed to C runtime function”? C ++类函数的未处理异常 - Unhandled exception with C++ class function 必须在将QWidget和Invalid参数传递给C运行时函数之前构造QApplication - Must construct a QApplication before a QWidget & Invalid parameter passed to C runtime function C ++中未处理的异常 - Unhandled exception at C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM