Manage the Frame Lifecycle and Buffer Pooling
mainTo benefit from the performance improvements of GetFrames(), you must strictly follow the frame lifecycle to prevent pool exhaustion:
- Receive: Iterate over the channel returned by
dev.GetFrames(). - Process: Use
frame.Datato perform your work. - Release: You MUST call
frame.Release()when finished with the frame. This returns the buffer to the pool for reuse.
Critical Warnings:
- Do not use
frame.Dataafter callingRelease(): The buffer is immediately invalid and may be overwritten by the next captured frame. - Copy if you need to persist data: If you need to use the frame data outside the capture loop, you must copy it to a new slice before calling
Release(). - Failure to call
Release(): If you forget to release frames, the buffer pool will eventually exhaust, causing the application to stall or crash.
// GOOD: Copy before Release() to use data later
frame := <-dev.GetFrames()
saved := make([]byte, len(frame.Data))
copy(saved, frame.Data)
frame.Release()
processLater(saved) // OK: using copy