我有一个循环,该循环在开始时声明一个向量,并通过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
}
答案 0 :(得分:-1)
删除delete[] * it;
前面的两条虚线即可解决问题。