A quick question about event objects. Let's say I have a program that operates on a webcam feed. I want to keep looping as fast as I can grab frames and process them. The program might be structured like so:

Code :
/*
*  Set up context, memory objects, kernel, etc...
*/
 
// events that commands will generate
cl::Event writeImgGenEvent;
cl::Event kernelGenEvent;
cl::Event readImgGenEvent;
 
// lists of events for commands to wait on
std::vector<cl::Event> writeImgWaitEvents;
std::vector<cl::Event> kernelWaitEvents;
std::vector<cl::Event> readImgWaitEvents;
 
// first we'll write the image, but we won't wait on anything
cmdQueue.enqueueWriteImage(
      /* non-blocking, blah-blah... */,
      webcamDataPtr,
      NULL,
      writeImgGenEvent);
 
// now we'll enqueue the kernel, and we'll wait on the previous command to finish
kernelWaitEvents.push_back(writeImgGenEvent);
cmdQueue.enqueueNDRangeKernel(
      /* non-blocking, blah-blah... */,
      kernelWaitEvents,
      kernelGenEvent);
 
// now we'll read the data back so we can display it, but wait on the kernel to finish
kernelWaitEvents.push_back(kernelGenEvent);
cmdQueue.enqueueReadImage(
      /* non-blocking, blah-blah... */,
      displayImgPtr,
      readImgWaitEvents,
      readImgGenEvent);
 
// NOTE: ONLY NOW CAN WAIT HAVE THE READ_IMAGE EVENT IN WRITE_EVENT'S WAIT LIST
// (it wasn't a valid object until the read completed for the first time)
writeImgWaitEvents.push_back(readImgGenEvent);
 
// now we want to loop forever so we can keep processing webcam frames
while (1)
{
      // write, but wait on the previous read to finish
      cmdQueue.enqueueWriteImage(
            /* non-blocking, blah-blah... */,
            webcamDataPtr,
            writeImgWaitEvents,
            writeImgGenEvent);
 
      cmdQueue.enqueueNDRangeKernel(
            /* non-blocking, blah-blah... */,
            kernelWaitEvents,
            kernelGenEvent);
 
      // now we'll read the data back so we can display it, but wait on the kernel to finish
      cmdQueue.enqueueReadImage(
            /* non-blocking, blah-blah... */,
            displayImgPtr,
            readImgWaitEvents,
            readImgGenEvent);
}

Unfortunately, this doesn't work. It appears that the generated events are no longer valid after a certain period of time, because the program just hangs after one time through the while loop. The only solution I've found is to clear each commands' wait list and reinsert whatever generated events the command depends on, but this just seems cumbersome. Am I missing something?!