1 year ago

#386246

test-img

dongrixinyu

How to deallocate memory of Python List Object in C using ctypes?

I am using ctypes to import a piece of C code into my Python code. the code is as follows:

  • test.h
#include "Python.h"
#include <stdio.h>

PyObject *getPosNodeFeature();

  • test.c
#include "test.h"

#ifdef _WIN32
#define API __declspec(dllexport)
#else
#define API
#endif

API PyObject *getPosNodeFeature(){
    const wchar_t *beforeWordTmp = L"bl";
    wchar_t *beforeWordPartRight = malloc(2 * sizeof(wchar_t));
    wcsncpy(beforeWordPartRight, beforeWordTmp, 2);

    PyObject *beforeWordPartRightTmp = PyUnicode_FromWideChar(beforeWordPartRight, i);
    PyObject *beforeRightWordsList = PyList_New(0);

    ret = PyList_Append(beforeRightWordsList, beforeWordPartRightTmp);
    
    Py_DECREF(beforeWordPartRightTmp);
    free(beforeWordPartRight);
    beforeWordPartRight = NULL;

    Py_DECREF(beforeRightWordsList);
    
    PyObject *featureList = PyList_New(0);

    return featureList;
}
  • in the above code, I introduced a temp PyListObject -> beforeRightWordsList. This object is processed and deallocated.

  • then I compiled it and included into the python code.

  • test.py

import os
import ctypes

dir_path = '/path/to/libtest.so'
feature_extractor = ctypes.PyDLL(os.path.join(dir_path, 'libtest.so'))
get_pos_node_feature_c = feature_extractor.getPosNodeFeature
get_pos_node_feature_c.argtypes = []
get_pos_node_feature_c.restype = ctypes.py_object

for i in range(10000):
    res = get_pos_node_feature_c()

  • When I run the code above, I find the memory of the python process increasing in comply with the for loop.

  • So, how to release the memory of the PyListObject? Simply de-count-referencing it is not enough?

python

c

memory-leaks

ctypes

0 Answers

Your Answer

Accepted video resources