python - 分配一 block 内存并立即在 C 中释放它失败

标签 python c

在我与 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;
}

因此,不知何故,始终且仅针对此特定字符串,分配内存以放入它,然后立即释放该内存会导致错误。字符串的实际文本甚至没有复制到内存中。是什么导致了这种神秘行为?

最佳答案

这里

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);
}

关于python - 分配一 block 内存并立即在 C 中释放它失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14125294/

相关文章:

python 扭曲 : Performing multiple writes at once

python - 我可以在另一台计算机上运行和开发 python 程序吗?

c - (方向键的读法)C程序中的金手指(键盘输入时)

使用 make 编译时,main 的类型与先前的 main 定义发生冲突

c - int 矩阵中的段错误

python - python中子进程命令行中没有反斜杠的引号

python - 是否可以重新加载 python 模块?

c - 如何修复 "unable to open stdio.h in Turbo C"错误?

python - 是否有适用于 Azure Synapse 的 Python SDK?

c - #define 不传播到 C 头文件