VideoCapture memory leak when using read function to get frame data inside a loop,opencv3.4.6

asked 2019-07-20 12:17:43 -0500

Tyrone gravatar image

updated 2019-07-20 13:53:45 -0500

Witek gravatar image

When I use opencv3.4.6 to process a big video frame by frame,and everything worked fine until frame 35000 .And after that each frame is empty.As if this function cause memory leaks,but I can't know how to fix this???When I test a smaller video file,nothing happen and my programe worked fine.Can you help me out?

Here is part of my code:

while(1)
            {
                // gettimeofday(&frameStart, NULL);
                cv::Mat frame;
                demo_time = what_time_is_it_now();
                cam >> frame;
                // select_ROI(frame);
                if(frame.data)
                {
                    frame.release();
                }
                else{
                    frame.release();
                }

                c++;
                // std::cout << "c=" << c-1 << " cc=" << cc-1 << std::endl;

                if(c > total_frames) break;
            }

            // video.release();
            cam.release();

Here is my build info: General configuration for OpenCV 3.4.6 ===================================== Version control: unknown

Platform: Timestamp: 2019-07-20T16:51:58Z Host: Linux 4.15.0-29-generic x86_64 CMake: 3.10.2 CMake generator: Unix Makefiles CMake build tool: /usr/bin/make Configuration: RELEASE

CPU/HW features: Baseline: SSE SSE2 SSE3 requested: SSE3 Dispatched code generation: SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX requested: SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX SSE4_1 (14 files): + SSSE3 SSE4_1 SSE4_2 (2 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX AVX (6 files): + SSSE3 SSE4_1 POPCNT SSE4_2 AVX AVX2 (28 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX512_SKX (2 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_SKX

C/C++: Built as dynamic libs?: YES C++ Compiler: /usr/bin/c++ (ver 5.5.0) C++ flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG C++ flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG C Compiler: /usr/bin/cc C flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG -DNDEBUG C flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -g -O0 -DDEBUG -D_DEBUG Linker flags (Release): -Wl,--gc-sections
Linker flags (Debug): -Wl,--gc-sections
ccache: NO Precompiled headers: YES Extra dependencies: dl m pthread rt 3rdparty dependencies:

OpenCV modules: To be built: calib3d core dnn features2d flann highgui imgcodecs imgproc ml objdetect photo python3 shape stitching superres ts video videoio videostab Disabled: world Disabled by dependency: - Unavailable: cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev java js python2 viz Applications: tests perf_tests apps Documentation: NO Non-free ... (more)

edit retag flag offensive close merge delete

Comments

Are you sure your input file has more than 35000 frames?

What is the point of frame.release() in your code? And what is the point of the if function there since regardless of its result you release the frame?

Witek gravatar imageWitek ( 2019-07-20 13:57:24 -0500 )edit
1

Yes,pretty sure!!And my video info is: Total frame=70921, fps=50 frame height = 1536 frame width = 2560 mode = 0

the reason I add frame.release() inside the while loop is that I doubt it could be cause by frame Mat used too many times,so memory leak may happen.However neither adding frame.release in my code worked....

Tyrone gravatar imageTyrone ( 2019-07-20 22:05:12 -0500 )edit

And I had post another QA using image,that should make my question clearer link text

Tyrone gravatar imageTyrone ( 2019-07-20 22:06:28 -0500 )edit