scipy/scipy — pull request #508 by gustavla (2013-04-13 18:56:47+00:00)
Title: Memory leak in convolve2d

The function `convolve2d` leaked memory allocated for the output array's shape. This is a temporary array that is passed to `PyArray_SimpleNew`, which makes a copy of it, so the original one must be freed. Before this fix, it was freed only when something failed. I fixed this by adding a call to `free` when it succeeds as well.

The amount of leaked memory is very small, but given enough calls to `convolve2d` it will add up. For me, I couldn't run my entire experiment without eventually getting killed by the OOM killer.

This is an easy test for checking this bug:

import numpy as np
import scipy.signal

X = np.ones((5, 5))
kernel = np.ones((4, 4))

while True:
scipy.signal.convolve2d(X, kernel)

And then watch the memory usage steadily increase. I have experimentally confirmed that my proposed solution fixes this problem.