The following code apparently causes a memory leak:
stim::image cropped(size_x, size_y, channels); cropped = I.crop(j*size_x, j*size_x+size_x, k*size_y, k*size_y+size_y);