videoio(test): avoid raw ptr in ffmpeg tests

causes leaks if FFmpeg is disabled through OPENCV_VIDEOIO_PRIORITY_FFMPEG=0
pull/14834/head
Alexander Alekhin 6 years ago
parent 681e0323f2
commit ab24325df3
  1. 31
      modules/videoio/test/test_ffmpeg.cpp

@ -227,7 +227,7 @@ public:
const static Size FrameSize; const static Size FrameSize;
static std::string TmpDirectory; static std::string TmpDirectory;
CreateVideoWriterInvoker(std::vector<VideoWriter*>& _writers, std::vector<std::string>& _files) : CreateVideoWriterInvoker(std::vector< cv::Ptr<VideoWriter> >& _writers, std::vector<std::string>& _files) :
writers(_writers), files(_files) writers(_writers), files(_files)
{ {
} }
@ -241,14 +241,14 @@ public:
std::string fileName = tempfile(stream.str().c_str()); std::string fileName = tempfile(stream.str().c_str());
files[i] = fileName; files[i] = fileName;
writers[i] = new VideoWriter(fileName, CAP_FFMPEG, VideoWriter::fourcc('X','V','I','D'), 25.0f, FrameSize); writers[i] = makePtr<VideoWriter>(fileName, CAP_FFMPEG, VideoWriter::fourcc('X','V','I','D'), 25.0f, FrameSize);
CV_Assert(writers[i]->isOpened()); CV_Assert(writers[i]->isOpened());
} }
} }
private: private:
std::vector<VideoWriter*>& writers; std::vector< cv::Ptr<VideoWriter> >& writers;
std::vector<std::string>& files; std::vector<std::string>& files;
}; };
@ -264,7 +264,7 @@ public:
static const Scalar ObjectColor; static const Scalar ObjectColor;
static const Point Center; static const Point Center;
WriteVideo_Invoker(const std::vector<VideoWriter*>& _writers) : WriteVideo_Invoker(const std::vector< cv::Ptr<VideoWriter> >& _writers) :
ParallelLoopBody(), writers(&_writers) ParallelLoopBody(), writers(&_writers)
{ {
} }
@ -304,7 +304,7 @@ protected:
} }
private: private:
const std::vector<VideoWriter*>* writers; const std::vector< cv::Ptr<VideoWriter> >* writers;
}; };
const Scalar WriteVideo_Invoker::ObjectColor(Scalar::all(0)); const Scalar WriteVideo_Invoker::ObjectColor(Scalar::all(0));
@ -315,7 +315,7 @@ class CreateVideoCaptureInvoker :
public ParallelLoopBody public ParallelLoopBody
{ {
public: public:
CreateVideoCaptureInvoker(std::vector<VideoCapture*>& _readers, const std::vector<std::string>& _files) : CreateVideoCaptureInvoker(std::vector< cv::Ptr<VideoCapture> >& _readers, const std::vector<std::string>& _files) :
ParallelLoopBody(), readers(&_readers), files(&_files) ParallelLoopBody(), readers(&_readers), files(&_files)
{ {
} }
@ -324,12 +324,12 @@ public:
{ {
for (int i = range.start; i != range.end; ++i) for (int i = range.start; i != range.end; ++i)
{ {
readers->operator[](i) = new VideoCapture(files->operator[](i), CAP_FFMPEG); readers->operator[](i) = makePtr<VideoCapture>(files->operator[](i), CAP_FFMPEG);
CV_Assert(readers->operator[](i)->isOpened()); CV_Assert(readers->operator[](i)->isOpened());
} }
} }
private: private:
std::vector<VideoCapture*>* readers; std::vector< cv::Ptr<VideoCapture> >* readers;
const std::vector<std::string>* files; const std::vector<std::string>* files;
}; };
@ -337,7 +337,7 @@ class ReadImageAndTest :
public ParallelLoopBody public ParallelLoopBody
{ {
public: public:
ReadImageAndTest(const std::vector<VideoCapture*>& _readers, cvtest::TS* _ts) : ReadImageAndTest(const std::vector< cv::Ptr<VideoCapture> >& _readers, cvtest::TS* _ts) :
ParallelLoopBody(), readers(&_readers), ts(_ts) ParallelLoopBody(), readers(&_readers), ts(_ts)
{ {
} }
@ -346,7 +346,7 @@ public:
{ {
for (int j = range.start; j < range.end; ++j) for (int j = range.start; j < range.end; ++j)
{ {
VideoCapture* capture = readers->operator[](j); VideoCapture* capture = readers->operator[](j).get();
CV_Assert(capture != NULL); CV_Assert(capture != NULL);
CV_Assert(capture->isOpened()); CV_Assert(capture->isOpened());
@ -394,7 +394,7 @@ public:
static bool next; static bool next;
private: private:
const std::vector<VideoCapture*>* readers; const std::vector< cv::Ptr<VideoCapture> >* readers;
cvtest::TS* ts; cvtest::TS* ts;
}; };
@ -406,7 +406,7 @@ TEST(Videoio_Video_parallel_writers_and_readers, accuracy)
cvtest::TS* ts = cvtest::TS::ptr(); cvtest::TS* ts = cvtest::TS::ptr();
// creating VideoWriters // creating VideoWriters
std::vector<VideoWriter*> writers(threadsCount); std::vector< cv::Ptr<VideoWriter> > writers(threadsCount);
Range range(0, threadsCount); Range range(0, threadsCount);
std::vector<std::string> files(threadsCount); std::vector<std::string> files(threadsCount);
CreateVideoWriterInvoker invoker1(writers, files); CreateVideoWriterInvoker invoker1(writers, files);
@ -416,11 +416,9 @@ TEST(Videoio_Video_parallel_writers_and_readers, accuracy)
parallel_for_(range, WriteVideo_Invoker(writers)); parallel_for_(range, WriteVideo_Invoker(writers));
// deleting the writers // deleting the writers
for (std::vector<VideoWriter*>::iterator i = writers.begin(), end = writers.end(); i != end; ++i)
delete *i;
writers.clear(); writers.clear();
std::vector<VideoCapture*> readers(threadsCount); std::vector<cv::Ptr<VideoCapture> > readers(threadsCount);
CreateVideoCaptureInvoker invoker2(readers, files); CreateVideoCaptureInvoker invoker2(readers, files);
parallel_for_(range, invoker2); parallel_for_(range, invoker2);
@ -437,8 +435,7 @@ TEST(Videoio_Video_parallel_writers_and_readers, accuracy)
} }
// delete the readers // delete the readers
for (std::vector<VideoCapture *>::iterator i = readers.begin(), end = readers.end(); i != end; ++i) readers.clear();
delete *i;
} }
#endif #endif

Loading…
Cancel
Save