Mi sto chiedendo qui perché finora non ho ricevuto aiuto dagli sviluppatori OpenCV. Ho ridotto il problema a un caso di test molto semplice quindi probabilmente chiunque con uno sfondo con CPython potrebbe aiutare qui.OpenCV: perdita di memoria con l'interfaccia Python ma non nella versione C
Questo codice C non ci siano perdite:
int main() {
while(true) {
int hist_size[] = {40};
float range[] = {0.0f,255.0f};
float* ranges[] = {range};
CvHistogram* hist = cvCreateHist(1, hist_size, CV_HIST_ARRAY, ranges, 1);
cvReleaseHist(&hist);
}
}
Questo codice Python fa di perdite:
while True: cv.CreateHist([40], cv.CV_HIST_ARRAY, [[0,255]], 1)
ho cercato attraverso il codice CPython (di OpenCVs corrente SVN codice tronco) e abbiamo trovato questo:
struct cvhistogram_t {
PyObject_HEAD
CvHistogram h;
PyObject *bins;
};
...
/* cvhistogram */
static void cvhistogram_dealloc(PyObject *self)
{
cvhistogram_t *cvh = (cvhistogram_t*)self;
Py_DECREF(cvh->bins);
PyObject_Del(self);
}
static PyTypeObject cvhistogram_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0, /*size*/
MODULESTR".cvhistogram", /*name*/
sizeof(cvhistogram_t), /*basicsize*/
};
static PyObject *cvhistogram_getbins(cvhistogram_t *cvh)
{
Py_INCREF(cvh->bins);
return cvh->bins;
}
static PyGetSetDef cvhistogram_getseters[] = {
{(char*)"bins", (getter)cvhistogram_getbins, (setter)NULL, (char*)"bins", NULL},
{NULL} /* Sentinel */
};
static void cvhistogram_specials(void)
{
cvhistogram_Type.tp_dealloc = cvhistogram_dealloc;
cvhistogram_Type.tp_getset = cvhistogram_getseters;
}
...
static PyObject *pycvCreateHist(PyObject *self, PyObject *args, PyObject *kw)
{
const char *keywords[] = { "dims", "type", "ranges", "uniform", NULL };
PyObject *dims;
int type;
float **ranges = NULL;
int uniform = 1;
if (!PyArg_ParseTupleAndKeywords(args, kw, "Oi|O&i", (char**)keywords, &dims, &type, convert_to_floatPTRPTR, (void*)&ranges, &uniform)) {
return NULL;
}
cvhistogram_t *h = PyObject_NEW(cvhistogram_t, &cvhistogram_Type);
args = Py_BuildValue("Oi", dims, CV_32FC1);
h->bins = pycvCreateMatND(self, args);
Py_DECREF(args);
if (h->bins == NULL) {
return NULL;
}
h->h.type = CV_HIST_MAGIC_VAL;
if (!convert_to_CvArr(h->bins, &(h->h.bins), "bins"))
return NULL;
ERRWRAP(cvSetHistBinRanges(&(h->h), ranges, uniform));
return (PyObject*)h;
}
E dalle intestazioni OpenCV C:
typedef struct CvHistogram
{
int type;
CvArr* bins;
float thresh[CV_MAX_DIM][2]; /* For uniform histograms. */
float** thresh2; /* For non-uniform histograms. */
CvMatND mat; /* Embedded matrix header for array histograms. */
}
CvHistogram;
Non so esattamente capisco tutto perché non ho mai lavorato con il C-interfaccia per Python prima. Ma probabilmente l'errore che sto cercando è da qualche parte in questo codice.
Ho ragione? O dove dovrei cercare il bug? Come lo risolverei?
(Nota per le persone che hanno visto una versione precedente di questa domanda: ho guardato il codice sbagliato.La loro interfaccia SWIG era deprecata e non più utilizzata (ma il codice era ancora lì in SVN, questo è il motivo per cui l'ho confuso . Quindi non guardare in interfaces/swig
, questo codice è vecchio e non usato. Il codice attuale vive a modules/python
.)
Upstream bug report: memleak in OpenCV Python CreateHist
'CreateHist' restituisce un oggetto. Poiché non è assegnato a nessuna variabile, dovrebbe essere liberato in futuro (dovrebbe entrare direttamente nel GC).Come lo stesso Python fa bene, OpenCV deve aver mantenuto un altro riferimento a qualcosa. – Albert
Inoltre, 'cv = None' non è realmente correlato al problema stesso. 'cv' è un modulo qui e viene liberato correttamente alla chiusura di Python. – Albert