3

我有一个用于模糊字符串搜索的自定义 python 模块,实现 Levenshtein 距离计算,它包含一个名为 levtree 的 python 类型,它有两个成员,一个指向执行所有计算的 wlevtree C 类型(称为树)的指针和一个指向的 PyObject* python 字符串的 python 列表,称为 wordlist。这是我需要的:

- 当我创建一个 levtree 的新实例时,我使用一个构造函数,它将字符串元组作为其唯一输入(并且它是实例将在其中执行所有搜索的字典),这个构造函数将必须创建一个新的实例wordlist 到 levtree 的新实例中,并将输入元组的内容复制到 wordlist 的新实例中。这是我的第一个代码片段和我的第一个问题:

static int
wlevtree_python_init(wlevtree_wlevtree_obj *self, PyObject *args, PyObject *kwds)
{
    int numLines;       /* how many lines we passed for parsing */
    wchar_t** carg;        /* argument to pass to the C function*/
    unsigned i;

    PyObject * strObj;  /* one string in the list */
    PyObject* intuple;

    /* the O! parses for a Python object (listObj) checked
      to be of type PyList_Type */
    if (!(PyArg_ParseTuple(args, "O!", &PyTuple_Type, &intuple)))
    {
        return -1;
    }

    /* get the number of lines passed to us */
    numLines = PyTuple_Size(intuple);
    carg = malloc(sizeof(char*)*numLines);

    /* should raise an error here. */
    if (numLines < 0)
    {
        return -1; /* Not a list */
    }

    self->wordlist = PyList_New(numLines);
    Py_IncRef(self->wordlist);
    for(i=0; i<numLines; i++)
    {

        strObj = PyTuple_GetItem(intuple, i);
        //PyList_Append(self->wordlist, string);
        PyList_SetItem(self->wordlist, i, strObj);
        Py_IncRef(strObj);
    }

    /* iterate over items of the list, grabbing strings, and parsing
       for numbers */
    for (i=0; i<numLines; i++)
    {

        /* grab the string object from the next element of the list */
        strObj = PyList_GetItem(self->wordlist, i); /* Can't fail */

        /* make it a string */


        if(PyUnicode_Check(strObj))
        {
            carg[i] = PyUnicode_AsUnicode( strObj );
            if(PyErr_Occurred())
            {
                return -1;
            }
        }
        else
        {
            strObj = PyUnicode_FromEncodedObject(strObj,NULL,NULL);
            if(PyErr_Occurred())
            {
                return -1;
            }
            carg[i] = PyUnicode_AsUnicode( strObj );
        }
    }
    self->tree = (wlevtree*) malloc(sizeof(wlevtree));
    wlevtree_init(self->tree,carg,numLines);
    free(carg);
    return 0;
}

我是否必须调用 Py_IncRef(self->wordlist); 在 self->wordlist = PyList_New(numLines); 或者它是多余的,因为引用已经在 PyList_new 中增加了?然后我对 PyList_SetItem(self->wordlist, i, strObj); 有同样的疑问 和 Py_IncRef(strObj);..

-当我销毁 levtree 的实例时,我想调用释放树占用的空间的 C 函数,销毁 wordlist 并减少 wordlist 中包含的所有字符串的所有引用计数。这是我的 tp_dealloc:

static void
wlevtree_dealloc(wlevtree_wlevtree_obj* self)
{
    //wlevtree_clear(self);
    if(self->tree!=NULL)
    {
        wlevtree_free(self->tree);
    }
    free(self->tree);
    PyObject *tmp, *strObj;
    unsigned i;
    int size = PyList_Size(self->wordlist);
    for(i=0; i<size; i++)
    {
        strObj = PyList_GetItem(self->wordlist, i);
        Py_CLEAR(strObj);
    }
    Py_CLEAR(self->wordlist);
    Py_TYPE(self)->tp_free((PyObject *)self);
}

在这里进行所有解除分配是否正确?目前我没有 tp_clear 和 tp_free,我需要它们吗?我的代码目前适用于分配,但不适用于解除分配,因为即使我可以多次在同一个 python 变量上调用init ,在每个 python 脚本(正常工作)结束时,我都会得到一个“分段错误”,这让我认为释放过程中出现问题..

4

1 回答 1

6

tp_clear仅当您实现循环垃圾收集时才需要。似乎不需要这样做,因为您只维护对 Python unicode 对象的引用。

tp_dealloc当对象的引用计数降至零时调用。这是您销毁对象及其成员的地方。然后它应该通过调用来释放对象占用的内存tp_free

tp_free是释放对象内存的地方。仅当您自己实施时才实施tp_alloc

tp_dealloc和之间分开的原因tp_free是,如果你的类型是子类的,那么只有子类知道内存是如何分配的以及如何正确释放内存。

如果您的类型是现有类型的子类,您tp_dealloc可能需要调用tp_dealloc派生类的,但这取决于案例的详细信息。

总而言之,您似乎正确地处理了对象破坏(除了carg在退出函数时出现错误)。

于 2014-07-21T10:47:04.597 回答