My module crashes Rack. Why?

To speed up some calculations I decided to precalculate these values and put them in two arrays. That makes Rack crash. Why?

#include "plugin.hpp"
#include "grid.hpp"
#include <vector>

struct Travelers : Module {
	//snip enums

	const std::vector<Cell> bases = {
		Cell(240, LEFT), Cell(239, RIGHT), Cell(208, LEFT), Cell(207, RIGHT), 
		Cell(176, LEFT), Cell(175, RIGHT), Cell(144, LEFT), Cell(143, RIGHT), 
		Cell(112, LEFT), Cell(111, RIGHT), Cell( 80, LEFT), Cell( 79, RIGHT), 
		Cell( 48, LEFT), Cell( 47, RIGHT), Cell( 16, LEFT), Cell( 15, RIGHT)
	};
	
	Grid grid256 = {16, 16, bases};
	std::vector<Traveler> traveler;

	Travelers() {
		config(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS);
		for(int i = 0; i < NUM_PARAMS; i++){
			configParam(PMZ_PARAM + i, 0.f, 31.f, 22.f, "");
		}

		grid256.initLUT();  //without this no crash

		for(int i = 0; i < NUM_OUTPUTS; i++){
			traveler.push_back({grid256, Cell(grid256.getBaseCell(i))});
			traveler[i].base2current();
		}
	}

	void process(const ProcessArgs& args) override {
		//snip
	}
};

in grid.hpp where NUMCELLS = 256, NUMROWS = 16:

void initLUT(){
  for (int i = 0; i < NUMCELLS; i++){
    gridRow[i] = i % NUMROWS;
    gridCol[i] = floor(i / NUMROWS);
  }
}

from log.txt:

[8.237 fatal src/main.cpp:45] Fatal signal 11. Stack trace:
31: ZN4rack10appDestroyEv 0x456435
30: gai_strerrorW 0x739800
29: _C_specific_handler 0x84917fb0
28: _chkstk 0x84ea10e0
27: RtlRaiseException 0x84e69ef0
26: KiUserExceptionDispatcher 0x84e9fe40
25: ZN9TravelersC1Ev 0x2e28770
24: ZZN4rack11createModelI9Travelers15TravelersWidgetEEPNS_6plugin5ModelERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEN6TModel18createModuleWidgetEv 0x2e2a320
23: ZN4rack3app11AudioWidget12setAudioPortEPNS_5audio4PortE 0x49839c
22: ZN4rack3app8ModelBox8onButtonERKNS_5event6ButtonE 0x75cbc0
21: ZN4rack6widget6Widget8onButtonERKNS_5event6ButtonE 0x7726f0
20: ZN4rack6widget6Widget8onButtonERKNS_5event6ButtonE 0x7726f0
19: ZN4rack6widget6Widget8onButtonERKNS_5event6ButtonE 0x7726f0
18: ZN4rack2ui12ScrollWidget8onButtonERKNS_5event6ButtonE 0x4a3ed4
17: ZN4rack6widget12OpaqueWidget8onButtonERKNS_5event6ButtonE 0x771cd0
16: ZN4rack3app14BrowserOverlay8onButtonERKNS_5event6ButtonE 0x7530d0
15: ZN4rack6widget12OpaqueWidget8onButtonERKNS_5event6ButtonE 0x771cd0
14: ZN4rack5event5State12handleButtonENS_4math3VecEiii 0x4576c8
13: ZN4rack7updater17isUpdateAvailableEv 0x45914e
12: glfwPlatformUnlockMutex 0x4d78ae
11: CallWindowProcW 0x82ff6030
10: CallWindowProcW 0x82ff6030
9: glPushClientAttrib 0x6c473770
8: CallWindowProcW 0x82ff6030
7: DispatchMessageW 0x82ff5bf0
6: glfwPlatformPollEvents 0x4daf66
5: ZN4rack6Window3runEv 0x4594ea
4: main 0x781840
3: main 0x781840
2: main 0x781840
1: BaseThreadInitThunk 0x83357bc0
0: RtlUserThreadStart 0x84e6ceb0

I don’t see a declaration for gridRow and gridCol?

that’s in grid.hpp

class Grid {

  private:

    typedef void (Grid::*Directions) (int dir);
    const  int NUMROWS;
    const  int NUMCOLS;
    const  int NUMCELLS = NUMROWS * NUMCOLS;
    static const int COL = 1;
    static const int ROW = 1;
    static constexpr int NUMSTATES = 32;
    int direction = 0;
    int newId = 0; 
    std::vector<Cell> baseCells;
    std::vector<int> gridCol;
    std::vector<int> gridRow;
//snip functions
  public:
    Grid(int numrows, int numcols, std::vector<Cell> baseCells) 
      : NUMROWS(numrows), NUMCOLS(numcols), baseCells(baseCells){}

    void initLUT(){
      for (int i = 0; i < NUMCELLS; i++){
        gridRow[i] = i % NUMROWS;
        gridCol[i] = floor(i / NUMROWS);
      }
    }
//snip

I had the initLUT in the constructor, that also crashes Rack.

It crashes because the std::vector size is not set

Yes, that might be right, use push_back instead or size accordingly.

You can use resize() to set an initial size to the vector, then using [] operator will work

Ahh, that’s it. Used push_back and it works!

Thanks!