Strange Error: terminate called after throwing an ...

terminate called after throwing an instance of ‘char const*’

Hello everyone,

I am not that new to coding; but definitely new to coding in C++ so I have a feeling this may be related to my improper use of C++ and OpenFrameworks. I am kind of stuck as when this error happens the whole app just closes … no beachball, freezing, or anything else - just closes. I am bringing together Zach’s Vector Field code and RUI’s optical Flow ( and later some sound libraries ) to create a music visualizer for a friend doing a cello performance. I’ll list my specs and some of the “testApp.cpp” code below.

Machine: MBP 2.4
IDE: xCode

I noticed this problem only became apparent once I started wrapping some code (originally in update) into a function that scans along a certain part of the screen to look for vector field movement. This is it below. It is called from update and I wonder if maybe the problem may be related to creating a new “vector” array every time it is called … maybe my lack of real understandng of pointers and the like is messing it up?

Thanks in advance for any help! :slight_smile: (I’d add the project folder but it is about 100mb)

  
//now loop through screen to find where motion is occuring  
inline vector <int> application::returnMotionXValues( ofxVec2f topLeftCorner, ofxVec2f bottomRightCorner, float threshold, int beginX, int endX)  
{  
	//first we will set a virtual bounding box proportional to width raindropfall width given then move that across across screen to find where particles should fall  
	magTot = 0;				//total magnitudes of all vectors - this will determine if it should rain there or not  
	int boxWidth = bottomRightCorner.x - topLeftCorner.x;  
	vector <int> xResArr;  
	  
	//set up arr  
	for(int i = 0; i < endX; i++)  
	{  
		xResArr.push_back(0);  
	}  
	  
	//scan across screen  
	for(counterX = beginX; counterX < endX; counterX += boxWidth)  
	{  
		//set corners of bounding box  
		topLeftCorner.set(counterX, topLeftCorner.y);  
		bottomRightCorner.set(counterX + boxWidth, bottomRightCorner.y);  
		  
		magTot = returnVecMagsOfBox(topLeftCorner, bottomRightCorner);  
		  
		if (magTot > threshold)  
		{  
			xResArr = addRandomMotionXBoundary(counterX, boxWidth, xResArr);  
		}  
	}  
	  
	return xResArr;  
}  
  
  
  
  
inline float application::returnVecMagsOfBox(ofxVec2f leftHandCorner, ofxVec2f botRightCorner)  
{  
	float totalMag = 0.0f;  
	ofxVec2f currVec;  
	  
	int arrSize = VF.field.size();  
	for(int i = 0; i < arrSize ; i++)  
	{  
		currVec = returnVecConcenPoint(i);  
		  
		if((currVec.x >= leftHandCorner.x) && (currVec.x <= botRightCorner.x))  
		{  
			if((currVec.y >= leftHandCorner.y) && (currVec.y <= botRightCorner.y))  
			{  
				totalMag += VF.field[i].length(); //add magnitude of actual vector (NOT position. did that once ...)  
			}  
		}  
	}  
	  
	return totalMag;  
}  
  
  
//add motion to the given indices (i.e. x pixels of captured screen)  
inline vector <int> application::addRandomMotionXBoundary(int beginIndex, int numIndices, vector <int> resolutionArr)  
{  
	int endIndex = beginIndex + numIndices;  
	  
	if( (beginIndex + numIndices) <= resolutionArr.size() )  
	{  
		for(int i = beginIndex; i < (endIndex + 1); i++)  
		{  
			resolutionArr[i] = 1;  
		}  
	}  
	else  
	{  
		throw "Vector's given size is not compatible with the indices given";  
	}  
	  
	return resolutionArr;  
}  
  

Sorry this is how I call it in update if this helps as well:

  
  
//set size of scanning bounding box (this so the returnMotionXValues function can scan across screen once per update looking for motion)  
	screenTL.set(0, 0);  
	screenBR.set(50, screenResY);  
	  
	//return a vector of that presents 1's where across the screen x axis there is motion  
	possibleFallxPixelVals = returnMotionXValues( screenTL , screenBR , 500, 0, screenResX);  
	anyMovement = anyNonZeroValues( possibleFallxPixelVals ); //is there any movement (if vector has no 1's in it (all zero's) then no motion this update = return false)  
  

Ah, sorry to spam lol. Hopefully this is not just talking to myself but might help other problem -solve with similar issues.

I just noticed that it only crashes when my motion leaves the right side of the screen suggesting I have some sort of out of bounds error happening somewhere in a loop … I’ll see if I can find it and pot here again :slight_smile:

Funny how you never notice (for hours even ) anything until you start trying to explain to others …

Figured out the problem. Seems I was throwing an error incorrectly :confused: (so it was actually my own throw error cancelling the program! … ). It was an out of bounds problem and this is the new function that should replace the one causing the problem. Thanks.

//add motion to the given indices (i.e. x pixels of captured screen)
inline vector application::addRandomMotionXBoundary(int beginIndex, int numIndices, vector resolutionArr)
{
int endIndex = beginIndex + numIndices;

//have to do a catch in case
if (endIndex > ( resolutionArr.size() - 1 ))
{
endIndex = resolutionArr.size() - 1;
}

for(int i = beginIndex; i < (endIndex + 1); i++)
{
resolutionArr[i] = 1;
}

return resolutionArr;
}