Commit 189d2a1da2a1299cd22b2417d1f10a316621b6e7

Authored by Charles Otto
1 parent 4e956a47

Avoid a mysterious crash related to closing VideoCaptures

Don't explicitly release the VideoCapture when it runs out of frames, further
investigation is needed, but this seems to cause an overrun or something

Additionally, make TemplateDataSource behave consistently with VideoDataSource,
breaking only after getNext has been called when no further frames are
available.
Showing 1 changed file with 19 additions and 14 deletions
openbr/plugins/stream.cpp
@@ -11,8 +11,6 @@ @@ -11,8 +11,6 @@
11 #include "openbr/core/opencvutils.h" 11 #include "openbr/core/opencvutils.h"
12 #include "openbr/core/qtutils.h" 12 #include "openbr/core/qtutils.h"
13 13
14 -#include <iostream>  
15 -  
16 using namespace cv; 14 using namespace cv;
17 15
18 namespace br 16 namespace br
@@ -258,12 +256,16 @@ public: @@ -258,12 +256,16 @@ public:
258 next_idx = 0; 256 next_idx = 0;
259 basis = input; 257 basis = input;
260 video.open(input.file.name.toStdString()); 258 video.open(input.file.name.toStdString());
261 - return video.isOpened(); 259 + video_ok = video.isOpened();
  260 + return video_ok;
262 } 261 }
  262 + bool video_ok;
263 263
264 - bool isOpen() { return video.isOpened(); } 264 + bool isOpen() { return video_ok; }
265 265
266 - void close() { video.release(); } 266 + void close() {
  267 + video.release();
  268 + }
267 269
268 private: 270 private:
269 bool getNext(FrameData & output) 271 bool getNext(FrameData & output)
@@ -279,8 +281,8 @@ private: @@ -279,8 +281,8 @@ private:
279 281
280 bool res = video.read(output.data.last().last()); 282 bool res = video.read(output.data.last().last());
281 if (!res) { 283 if (!res) {
282 - video.release();  
283 - return false; 284 + video_ok = false;
  285 + return video_ok;
284 } 286 }
285 output.data.last().file.set("FrameNumber", output.sequenceNumber); 287 output.data.last().file.set("FrameNumber", output.sequenceNumber);
286 return true; 288 return true;
@@ -299,7 +301,9 @@ public: @@ -299,7 +301,9 @@ public:
299 TemplateDataSource(int maxFrames) : DataSource(maxFrames) 301 TemplateDataSource(int maxFrames) : DataSource(maxFrames)
300 { 302 {
301 current_idx = INT_MAX; 303 current_idx = INT_MAX;
  304 + data_ok = false;
302 } 305 }
  306 + bool data_ok;
303 307
304 bool open(Template &input) 308 bool open(Template &input)
305 { 309 {
@@ -309,10 +313,13 @@ public: @@ -309,10 +313,13 @@ public:
309 final_frame = -1; 313 final_frame = -1;
310 last_issued = -2; 314 last_issued = -2;
311 315
312 - return isOpen(); 316 + data_ok = current_idx < basis.size();
  317 + return data_ok;
313 } 318 }
314 319
315 - bool isOpen() { return current_idx < basis.size() ; } 320 + bool isOpen() {
  321 + return data_ok;
  322 + }
316 323
317 void close() 324 void close()
318 { 325 {
@@ -323,7 +330,8 @@ public: @@ -323,7 +330,8 @@ public:
323 private: 330 private:
324 bool getNext(FrameData & output) 331 bool getNext(FrameData & output)
325 { 332 {
326 - if (!isOpen()) 333 + data_ok = current_idx < basis.size();
  334 + if (!data_ok)
327 return false; 335 return false;
328 336
329 output.data.append(basis[current_idx]); 337 output.data.append(basis[current_idx]);
@@ -332,6 +340,7 @@ private: @@ -332,6 +340,7 @@ private:
332 output.sequenceNumber = next_sequence; 340 output.sequenceNumber = next_sequence;
333 next_sequence++; 341 next_sequence++;
334 342
  343 + output.data.last().file.set("FrameNumber", output.sequenceNumber);
335 return true; 344 return true;
336 } 345 }
337 346
@@ -595,10 +604,6 @@ public: @@ -595,10 +604,6 @@ public:
595 604
596 FrameData * run(FrameData * input, bool & should_continue) 605 FrameData * run(FrameData * input, bool & should_continue)
597 { 606 {
598 - if (input != NULL) {  
599 - dataSource.returnFrame(input);  
600 - }  
601 -  
602 // Is there anything on our input buffer? If so we should start a thread with that. 607 // Is there anything on our input buffer? If so we should start a thread with that.
603 QWriteLocker lock(&statusLock); 608 QWriteLocker lock(&statusLock);
604 input = dataSource.tryGetFrame(); 609 input = dataSource.tryGetFrame();