Looping from a loop

Hey all. I think I am missing something very basic. I am trying to create a dynamic amount of loops from a loop. If I try to pass anything higher than 1 to func2 my app crashes. Is this where I need to start threading things? Thanks for any help.

void ofApp::func1(){

	//loop thru arr of objects
	for(int i=0; i < arr.size(); i++) {

		//test for touch
		if(touched){

			//PROBLEM HERE
			func2(1);

		}

	}

}

//----------------

void ofApp::func2(int loops){
		
	for(int i = 0; i < loops; i++){
		//add new objects to array
	}

}

it’s hard to say, are you altering arr.size() while in the for loop? This generally a bad idea (typically you can get into trouble deleting elements while in a for loop) – can you possibly add objects to a second array and then when you are out of the for loop, copy them to the first?

hey Zach… actually thats exactly what I am doing. My array objects are box2d rects.

void ofApp::func1(){

	//loop thru arr of objects
	for(int i=0; i < arr.size(); i++) {

		//test for touch
		if(touched){

	        //destroy
            arr[i]->destroy();
            arr.erase(arr.begin() + i);

			//call
			func2(23);

		}

	}

}

//----------------

void ofApp::func2(int loops){
		
	for(int i = 0; i < loops; i++){
		//add new objects to array
		arr.push_back(shared_ptr<objectname>(new objectname));
        arr.back().get()->setup(box2d.getWorld(), bx, by, bloksizes[0], bloksizes[0]);
	}

}

I guess that’s why I can only add one. I deleted one before adding the new one, meaning the arr.size() isn’t changing. So you suggest creating an array in func2() and then copy its items to the main array? Is that right?

Actually Zach. I got it thank you. It was a box2d issue. I was building many rects at the same location… I am thinking that box2d cant handle making object at the same point at the same time well. Thanks for your help.