memory leak in opencv python interface (Bug #1177)
Description
When the cv functions are used on objects memory leaks appear. A simple test case:
import sys
import cv
import numpy as np
frame=np.zeros( (640,480)).astype('uint8')
n=0
while True:
n=n+1
if n>500:
break;
print('%d' % n)
frame0=frame.copy()
# the next line creates the leak!
sz=cv.GetSize(frame0)
del frame0
cv.+file+
cv.+version+
Associated revisions
disable implicit numpy array use in old-style Python bindings because of hard-to-fix memory leaks (ticket #1177)
Merge pull request #1177 from janstarzy:refactor
History
Updated by Joe RICHARD over 13 years ago
I tried this code on openCV 2.2, the result is the same memory leak.
But I wonder if eendebakpt's code is resonable?
I know openCV integrate a numpy interface for their image,
but I think reasonable to call the function to convert numpy array to Matrix ( cv.fromarray(anumpyarray) ),
and eventually a cv.GetImage(mymatrix).
So far if you add a line in your exemple the memory leak disappear. (at least on 2.2, I notice I patched my version so the cv.fromarray() doesn't leak, which I think should have been integrated in 2.3)
eendebakpt you can try the following, I let you confirm, and also change the status following your idea.
import sys
import cv
import numpy as np
frame=np.zeros( (640,480)).astype('uint8')
n=0
while True:
n=n+1
if n>500:
break;
print('%d' % n)
frame0=frame.copy()
mat=cv.fromarray(frame0) # if you add this line,
sz=cv.GetSize(mat) # there is no more memory leak
#no need for a del in my opinion.
cheers.
Updated by Pieter Eendebak over 13 years ago
Adding the line suggested by joemanu indeed removes the memory leak (2.3rc). However, if passing numpy arrays to the opencv functions directly is an option, then there should be no memory leaks (or at least functions to clear the memory).
I changed the status to minor, but leave it as a defect.
Updated by Vadim Pisarevsky over 13 years ago
Thanks for exploring the problem. In trunk r6358 implicit use of numpy arrays in C-style Python bindings is not allowed anymore. You can either use cv.fromarray(), as proposed above or use the C++-style bindings (cv2.filter2D() etc.), where numpy arrays are native and the only supported arrays.
- Status changed from Open to Done
- (deleted custom field) set to fixed