ofImage bad_alloc in memory location (high res images)

Hi everyone,

I am writing a simple program, opening every images contained in a folder and mixing them into one image. It turns out that I have a bad_alloc at memory location problem in my script (at the load of the seventh picture).
I am working with large images (4700x6000) and it seems to me that I need to clean some space at a point, but I don’t where (and maybe I should be using an ofTexture instead of ofImage, but as I want to be able to resize every image, I don’t know if it is useful).

Here is the script :

 ofImage input;
string path = "random";
ofDirectory dir(path);
dir.allowExt("jpg");
dir.listDir();

if (dir.size() <= 0) {
	cout << " The directory is empty." << endl;
	std::exit(1);
}

bool success = input.load(dir[i]);

if (success) {
	w = input.getWidth();
	h = input.getHeight();
	output.allocate(w, h, OF_IMAGE_COLOR);

	int i = 0; int j = 0;
	int y = 0; int x = 0;
	int x_max = 3; int y_max = 3
	
	for (int n = 0; n < dir.size(); ++n) {
		input.load(dir[n]); // load new image	

		if (input.getHeight() > input.getWidth()) { // resize if necessary
			input.resize(input.getWidth()*h/input.getHeight(), h);
		}
		else if (input.getHeight() < input.getWidth()) {
			input.resize(w, w*input.getHeight() / input.getWidth());
		}
		else {
			input.resize(w, h);
		}

		input.crop(0, 0, w, h); // get the same size for every image

		for (x = i * w / x_max; x < (i+1) * w / x_max; ++x) { // write that part into output image
			for (y = j * h / y_max; y < (j+1) * h / y_max; ++y) {
				output.setColor(x, y, input.getColor(x,y));
			}
		}

		++i;
		if (i == x_max && j <= y_max) {
			i = 0;
			++j;
			if (j == y_max) {
				n = dir.size(); // stop the process when it reaches the end
								// if x_max * y_max < dir.size()
			}
		}
	}

	if (x_max * y_max > dir.size()) {
		for (x = i * w / x_max; x < w; ++x) { // write that part into final image
			for (y = j * h / y_max; y < h; ++y) {
				output.setColor(x, y, input.getColor(x, y));
			}
		}
	}


	output.save("output/result.jpg", OF_IMAGE_QUALITY_BEST);
}

thanks in advance,

what version of OF are you using? there used to be a bug like this in 0.9.x but it’s fixed in the latest versions

I was using the 0.9.3 but with the 0.9.8 I have the same error.