array data clearing when using out of bounds condition in iteration

I’m working at this module randLoops8

During tests I noticed that when Rack autosaves the patch one of the outputs stops to work, so I tried to deeply debug it and found that, when autosave occurs, one not involved array is set to zero.

this is the dataToJson():

json_t* dataToJson() override {
		json_t* rootJ = json_object();
		json_object_set_new(rootJ, "InitStart", json_boolean(initStart));
		json_object_set_new(rootJ, "dontAdvanceSetting", json_boolean(dontAdvanceSetting));
		json_object_set_new(rootJ, "polyChans", json_integer(polyChans));
		json_object_set_new(rootJ, "bitResolution", json_integer(bitResolution));
		json_object_set_new(rootJ, "progression", json_integer(progression));
		json_object_set_new(rootJ, "outType", json_integer(outType));

		for (int t = 0; t < 8; t++)
			sequence_to_saveRegister(t); // THIS COMMENTED LET bitResTable DONT GO TO ZERO 

		for (int t = 0; t < 8; t++) {
			json_t *track_json_array = json_array();
			for (int tempStep = 0; tempStep < 16; tempStep++) {
				json_array_append_new(track_json_array, json_integer(saveRegister[t][tempStep]));
			}
			json_object_set_new(rootJ, ("sr"+to_string(t)).c_str(), track_json_array);	
		}

		return rootJ;
	}

this bitResTable is the array that goes to zero:

int bitResTable[2] = {8, 16};
int tempRegister[16] =     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int tempSaveRegister[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

this happens also if I declare it as const.

if I comment sequence_to_saveRegister() call it doesn’t happen.
This is the code:

	void inline sequence_to_saveRegister(int t) {

		int cursor = startingStep[t];
		int wSteps = int(params[LENGTH_PARAM+t].getValue());
		
		for (int i = 0; i <= wSteps; i++) {
			tempSaveRegister[i] = shiftRegister[t][cursor];
			cursor++;
			if (cursor >= 16)
				cursor = 0;
		}

		int fillCursor = 0;
		for (int i = wSteps; i < 16; i++) {
			tempSaveRegister[i] = tempSaveRegister[fillCursor];
			fillCursor++;
			if (fillCursor >= wSteps)
				fillCursor = 0;
		}

		for (int i = 0; i < 16; i++)
			saveRegister[t][i] = tempSaveRegister[i];

	}

but if I replace that tempSaveRegister with tempRegister everything works fine. But I’m using tempRegister elsewhere, so I imagine it’s not thread-safe.

Any ideas?

Does “goes to zero” mean {0, 0} ?

How do you know that the array bitResTable goes to zero?

This sounds like an array out of bounds issue.

Should this be < wSteps rather than <= wSteps?

2 Likes

@Ahornberg yes, it means {0, 0}, I used

DEBUG("%i %i", bitResTable[0], bitResTable[1]);

@CountModula You’re right! Shame on me! … so using another array instead of tempSaveRegister must have erased something else, thank you.

2 Likes

Style tip: you don’t need all the '0’s to zero initialise an array, use = {}; or just simply {};

int tempRegister[16] =     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int tempSaveRegister[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
2 Likes

Without diminishing @Steve_Russell’s style suggestion in any way, here’s a fun (?) complement:

One of many things Herb Sutter and collaborators are trying to clean up in cppfront

1 Like

Where have you placed the DEBUG() in your code? I searched RandLoops8.cpp on yout github repo but I couldn’t find it.

Yes, sorry, you can’t find it because those were the test I made locally. I placed it before and after the sequence_to_saveRegister() call.

I downloaded your dev branch and added the DEBUG() messages before and after both calls to sequence_to_saveRegister() and bitResTable hasn’t changed its values until now.

How can I reproduce your bug?

Yes, because it’s fixed now. To reproduce the bug you can uncomment line 240 and comment line 241

1 Like