0

I am using a smart pointer in my code. When I try compile my program, Visual Studio pops up this notification in the vector class:

Unhandled exception at location 0x00007FFD725A9179 in game of life.exe: Microsoft C ++ exception: std :: length_error at memory location 0x00000004A491E9B0.

Here is my code:

template <class cellDerived>
class gameOfLife : public std::enable_shared_from_this<gameOfLife<cellDerived>>
{
    int sizeX;
    int sizeY;
    std::vector<std::vector<cellDerived>> rows;
    void init(int x, int y)
    {
        rows.resize(sizeX);
        for (int i = 0; i < sizeX; i++)
        {
            for (int j = 0; j < sizeY; j++)
            {
                cellDerived c(i, j);
                rows.at(i).push_back(cellDerived(i, j));
            }
        }
    }
public:
    std::shared_ptr<gameOfLife<cellDerived>> getptr() {
        return shared_from_this();
    }
    cellDerived &get(int x, int y)
    {
        x %= sizeX;
        y %= sizeY;
        if (x < 0) x += sizeX;
        if (y < 0) y += sizeY;
        return rows.at(x).at(y);
    }
    void step()
    {
        for (std::vector<cellDerived> &row : rows)
        {
            for (cellDerived &c : row)
            {

                    c.calculateNextState(shared_from_this());
            }
        }
        for (std::vector<cellDerived> &row : rows)
        {
            for (cellDerived &c : row)
            {
                c.update();
            }
        }
    }

this is screen from debugger enter image description here

sharkerrro
  • 11
  • 2
  • 1
    Visual Studio lets you view the line of your code that made that call to `vector` and examine the values of the variables you have to figure out what's wrong. That information should be provided as part of the question after doing some debugging work. – chris Mar 25 '20 at 22:34
  • 4
    Neither `sizeX` nor `sizeY` are assigned a value in the code you've shown. – 1201ProgramAlarm Mar 25 '20 at 22:37
  • when i compile without debugger i get dialog alert abort() has been called – sharkerrro Mar 25 '20 at 22:46
  • x and y are taken from the file in main – sharkerrro Mar 25 '20 at 22:47
  • Run with the debugger and press retry. It will show you where the error happened and you can look at the call stack. – eesiraed Mar 25 '20 at 22:52
  • next step takes me to __declspec(noreturn) void __CRTDECL __scrt_throw_std_bad_array_new_length() { throw std::bad_array_new_length{}; } – sharkerrro Mar 25 '20 at 22:56
  • If you're not comfortable with a debugger, can't you just add some debug prints in your functions to see how far you've come and what the different variables holds at that point? I'd also suggest `this->shared_from_this()` wherever you use `shared_from_this()` above. Reason: https://stackoverflow.com/a/17853405/7582247 – Ted Lyngmo Mar 25 '20 at 23:47
  • your solution has neutralized one error, but the problem still exists, I was able to locate where: game = std::make_shared>(gameOfLife(i,i)); , the error that pops up is std :: length_error after repetition, debugging redirects to the next error: td :: bad_array_new_length {} – sharkerrro Mar 26 '20 at 11:13
  • `gameOfLife(i,i)` creates a temporary `gameOfLife` object on the stack. But the class assumes its instances are always allocated on the heap, and managed by a `shared_ptr`. You probably meant `std::make_shared>(i, i)` – Igor Tandetnik Mar 28 '20 at 17:26

0 Answers0