Python 在C中分配一块内存并立即释放它失败了

Python 在C中分配一块内存并立即释放它失败了,python,c,Python,C,在我与Python集成的C模块的部分代码中,我有一个char**(字符串数组),它被重复分配,用分配的字符串填充,然后释放并再次分配。一般的模式是,当调用某个函数(从Python)提供数组的新内容(作为列表)时,它会遍历字符串数组,释放每个字符串,然后释放数组本身。然后再次分配数组以保存新Python列表的内容,然后为每个要保存的字符串分配内存 所有这些都表明我在尝试释放列表中的一个字符串时出错。这种错误是确定的;在程序的同一点上,它总是来自同一个单词列表中的同一个单词,但是这个单词或单词列表没

在我与Python集成的C模块的部分代码中,我有一个char**(字符串数组),它被重复分配,用分配的字符串填充,然后释放并再次分配。一般的模式是,当调用某个函数(从Python)提供数组的新内容(作为列表)时,它会遍历字符串数组,释放每个字符串,然后释放数组本身。然后再次分配数组以保存新Python列表的内容,然后为每个要保存的字符串分配内存

所有这些都表明我在尝试释放列表中的一个字符串时出错。这种错误是确定的;在程序的同一点上,它总是来自同一个单词列表中的同一个单词,但是这个单词或单词列表没有什么特别之处。(它只是[“CCellEnv”,“18”,“34”],这是一种与许多其他格式类似的格式)我尝试向分配字符串的循环添加一些调试代码;以下是产生错误的函数:

static PyObject* py_set_static_line(PyObject* self, PyObject* args)
{
    int i;
    //Free the old values of the allocated variables, if there are any
    if (numStaticWords > 0)
    {
        for (i = 0; i < numStaticWords; i++)
        {
            printf("Freeing word %d = '%s'\n", i, staticWords[i]);
            free(staticWords[i]);
        }
        free(staticWords);
        free(staticWordMatches);
    }

    //Parse arguments
    PyObject* wordList;
    unsigned short numWords;
    PyObject* wordMatchesList;
    if (!PyArg_ParseTuple(args, "O!HO!", &PyList_Type, &wordList, &numWords, &PyList_Type, &wordMatchesList))
        return NULL;

    numStaticWords = numWords;
    if (numStaticWords > 0)
    {
        staticWords = malloc(sizeof(char*) * numStaticWords);
        staticWordMatches = malloc(sizeof(int) * numStaticWords);

        PyObject* wordObj;
        PyObject* matchObj;
        char* word;
        for (i = 0; i < numStaticWords; i++)
        {
            //wordList is the list of strings passed from Python
            wordObj = PyList_GetItem(wordList, i);
            word = PyString_AsString(wordObj); //word is "18" in the failing case

            //staticWords is the char** array of strings, which has already been malloc'd
            staticWords[i] = malloc(sizeof(char) * strlen(word));

            //Test freeing the word to see if it crashes
            free(staticWords[i]); //Crashes for one specific word

            staticWords[i] = malloc(sizeof(char) * strlen(word));
            strcpy(staticWords[i], word);

            matchObj = PyList_GetItem(wordMatchesList, i);
            if (matchObj == Py_None)
            {
                staticWordMatches[i] = -1;
            }
            else
            {
                staticWordMatches[i] = PyInt_AsLong(matchObj);
            }
        }
    }
    Py_RETURN_NONE;
}
static PyObject*py\u set\u static\u行(PyObject*self,PyObject*args)
{
int i;
//释放已分配变量的旧值(如果有)
如果(numStaticWords>0)
{
对于(i=0;i0)
{
staticWords=malloc(sizeof(char*)*numStaticWords);
staticWordMatches=malloc(sizeof(int)*numStaticWords);
PyObject*wordObj;
PyObject*matchObj;
字符*字;
对于(i=0;i
因此,不知何故,总是并且只针对这个特定的字符串,分配内存将其放入,然后立即释放内存会导致错误。字符串的实际文本甚至不会复制到内存中。是什么导致了这种神秘的行为

staticWords[i] = malloc(sizeof(char) * strlen(word));
strcpy(staticWords[i], word);
您缺少为“字符串”分配
0
-终止。因此,对这些字符串数组的任何操作都很可能导致未定义的行为

这样做:

{
  int isNull = !word;

  staticWords[i] = calloc(sizeof(*staticWords[i]), (isNull ?0 :strlen(word)) + 1);
  strcpy(staticWords[i], isNull ?"" :word);
}

函数的作用是:释放ptr指向的内存分配。如果ptr是一个空指针,则不执行任何操作。我刚刚意识到这一点,并准备删除此问题。