我有一个循环,在开始时声明一个向量,通过push_back()向它写入数据。在循环开始时,我将向量重新声明为大小0.代码在循环的几次迭代后给我“杀死”。任何人都可以告诉我重用这个载体的正确方法吗?
.
.
.
//forever loop
while (1) {
std::vector < unsigned char * > image_vector(0);
std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
// initialize variables
steady_clock::time_point start_second = now;
steady_clock::time_point start_next_second = now;
steady_clock::time_point end_time = now + std::chrono::milliseconds(dT2 * 1000); // time to end capture sequence
while (1) {
// if start of current second past end_time exit loop
if (start_next_second >= end_time) {
cout << "Done capturing..." << endl;
break;
}
// incriment by 1s for start of next second
start_next_second += std::chrono::milliseconds(1000); //start of next second
for (int y = 0; y < dT1; y++) {
unsigned char * data = new unsigned char[Camera.getImageBufferSize()];
Camera.grab();
Camera.retrieve(data);
image_vector.push_back(data); /**********Writing data to vector********/
}
}
/* Save all images in vector to disk */
int index = 1;
for (std::vector < unsigned char * > ::iterator it = image_vector.begin(); it != image_vector.end(); ++it) {
std::stringstream fn;
fn << "images/[Pi]image";
fn << index;
fn << getExtension(Camera);
saveImage(fn.str(), * it, Camera);
cerr << "Saving " << fn.str() << endl;
//delete[] * it;
index++;
}
cout<< "Capture done, sleeping 5 seconds before next capture routine." << endl;
usleep(5000000); //sleep 5 sec before next capture routine
}
删除delete[] * it;
前面的两条虚线解决了这个问题。