fromarray Memory Leak and proposed patch (Feature #993)


Added by Matthew Baker almost 14 years ago. Updated over 13 years ago.


Status:Done Start date:
Priority:High Due date:
Assignee:James Bowman % Done:

0%

Category:python bindings
Target version:-
Difficulty: Pull request:

Description

Hi there OpenCv developers,

Recently, I encountered some fairly significant memory leaks associated with the 'fromarray' functionality of the Python bindings. After doing a little research it was clear that this problem had also troubled a number of other users. Attached is a python script (fromarray_bug.py) that demonstrates at least two bugs I observed with the functionality, both of which seem to leak memory until finally crashing.

During my research on the problem I had noticed that partial patches had been submitted to address similar concerns with this functionality. These patches, however, did not seem to conform to my understanding of the Python Array Interface as described in the Numpy documentation 'Guide to NumPy' (page 230). Furthermore, in developing an understanding of how the 'fromarray' functionality was intended to work I discovered that the bindings attempted to somehow monitor the foreign memory within the bindings. To me this was a seemingly questionable approach, particularly given the memory being passed into the function is 'foreign' memory and hence not OpenCv's responsibility. Therefore, I wrote my own patch which is also attached here as cv.cpp.

The defining difference about my approach and submitted patches is that I replace the calls to pythonize_foreign_CvMat and pythonize_CvMatND with the following:

m->data = o;
m->offset = 0;

with the inclusion of upping the reference count of the input array object o (I also had to add to the python clean up for the cvmatnd structure i.e. cvmatnd_dealloc).

This patch seems to address the aforementioned memory leak(s) surrounding the 'fromarray' functionality and in such a way that conforms with the Python Array Interface usage (at least as describe in the NumPy documentation).

Kind Regards,

Matthew Baker


fromarray_bug.py (347 Bytes) Matthew Baker, 2011-04-11 01:48 am

cv.cpp (112.2 kB) Matthew Baker, 2011-04-11 01:49 am

cv.cpp_patch_for_ticket_993.diff (3 kB) Matthew Baker, 2011-06-17 12:15 am


Related issues

duplicated by Bug #906: Memory leak when converting from numpy array Done

Associated revisions

Revision 8fa1e756
Added by Roman Donchenko over 11 years ago

Merge pull request #993 from ivan-korolev:fix_videostab_bug_3023

Revision 13cd0a05
Added by Roman Donchenko over 11 years ago

Merge remote-tracking branch 'origin/2.4'

Original pull requests:
#996 from jet47:gpu-nvcuvid-libraries
#995 from jet47:fix-bug-2985
#999 from snosov1:unreliable-results-fix
#1005 from alekcac:doc_fix
#1004 from jet47:fix-bug-3068
#987 from jet47:bug-3085-fix
#969 from pengx17:2.4_binary_cache
#929 from dominikrose:mingw-libdc1394-2-windows
#1000 from ivan-korolev:fix_sift_bug_2892
#1001 from ivan-korolev:fix_stitching_bug_2405
#998 from asmorkalov:android_cmake_mips_fix
#993 from ivan-korolev:fix_videostab_bug_3023
#988 from snosov1:3071-fix
#986 from pengx17:2.4_initiated_context
#982 from pengx17:2.4_fix_two_bugs
#981 from SeninAndrew:ximea_camera_support_fix
#991 from asmorkalov:android_javadoc_fix
#972 from jet47:mog2-params-bug-2168
#980 from SpecLad:include-config
#973 from pengx17:2.4_oclclahe
#903 from aks2:2.4
#968 from asmorkalov:android_na_cproj_fix
#971 from SpecLad:matchers-ctor
#970 from asmorkalov:dshow_valid_check_fix
#965 from apavlenko:fix_java_empty_mats

Conflicts:
cmake/OpenCVModule.cmake
modules/core/src/matmul.cpp
modules/gpu/CMakeLists.txt
modules/ocl/include/opencv2/ocl/ocl.hpp
modules/ocl/perf/perf_imgproc.cpp
modules/ocl/src/imgproc.cpp
modules/ocl/src/initialization.cpp
modules/stitching/src/matchers.cpp
modules/video/src/video_init.cpp
modules/videostab/src/global_motion.cpp

History

Updated by Vadim Pisarevsky over 13 years ago

hello Matthew!

thank you for the bug fixes! could you, please, provide patch against the trunk?

  • Status deleted (Open)

Updated by Matthew Baker over 13 years ago

Hi James,

I patched the official 2.2 release of the source code. I believe the trunk has moved on since then and hence, I am not sure that providing a patch against the current trunk would be sensible. I can and have provided (cv.cpp_patch_for_ticket_993.diff) the diff between the original cv.cpp from the official 2.2 release and the cv.cpp I patched.

Cheers,

MatB

Updated by Vadim Pisarevsky over 13 years ago

thanks a lot! your patch has been integrated in r5564

  • Status set to Done
  • (deleted custom field) set to fixed

Also available in: Atom PDF