python - 使用CFFI释放内存时 "MemoryError: Stack overflow"是什么意思?

标签 python c++ memory-management python-cffi

这个问题遵循this one .

我使用 CFFI 创建一个 DLL,并从 C++ 应用程序中调用它。我问自己如何释放 DLL 分配的内存,我遵循@metal 在 its answer 中提到的想法。 .

这是我的 Python 代码:

import cffi

ffibuilder = cffi.FFI()

ffibuilder.embedding_api('''
    char* get_string();
    void free_char(char*);
''')

ffibuilder.set_source('my_plugin', '')

ffibuilder.embedding_init_code('''
    from my_plugin import ffi, lib

    @ffi.def_extern()
    def get_string():
        val = "string"
        return lib.strdup(val.encode())

    @ffi.def_extern()
    def free_char(ptr):
        lib.free(ptr)
''')

ffibuilder.cdef('''
    char *strdup(const char *);
    void free(void *ptr);
''')

ffibuilder.compile(target='my-plugin.*', verbose=True)

还有我的 C++ 代码:

#include <iostream>
#include <windows.h>

typedef char* (__stdcall *get_string_t)();
typedef void (__stdcall *free_char_t)(char*);

int main()
{
    HINSTANCE hGetProcIDDLL = LoadLibrary("my-plugin.dll");

    if (!hGetProcIDDLL) {
        std::cout << "could not load the dynamic library" << std::endl;
        return -1;
    }

    get_string_t get_string = (get_string_t)GetProcAddress(hGetProcIDDLL, "get_string");
    if (!get_string) {
        std::cout << "could not locate the function" << std::endl;
        return -1;
    }

    free_char_t free_char = (free_char_t)GetProcAddress(hGetProcIDDLL, "free_char");
    if (!free_char) {
        std::cout << "could not locate the function" << std::endl;
        return -1;
    }

    for(int i = 0; i < 25000000; i++)
    {
        char* val = NULL;
        val = get_string();

        if(i % 10000 == 0)
        {
            std::cout << "Value " << i << " = " << val << std::endl;
        }

        if(val)
            free_char(val);
    }

    std::cout << "End" << std::endl;

    return 0;
}

我得到这个结果:

Value 0 = string
Value 10000 = string
Value 20000 = string
Value 30000 = string
Value 40000 = string
Value 50000 = string
Value 60000 = string
Value 70000 = string
Value 80000 = string
Value 90000 = string
Value 100000 = string
Value 110000 = string
Value 120000 = string
Value 130000 = string
Value 140000 = string
Value 150000 = string
Value 160000 = string
Value 170000 = string
Value 180000 = string
Value 190000 = string
Value 200000 = string
Value 210000 = string
Value 220000 = string
Value 230000 = string
Value 240000 = string
Value 250000 = string
From cffi callback <function get_string at 0x03470810>:
MemoryError: Stack overflow
From cffi callback <function get_string at 0x03470810>:

From cffi callback <function get_string at 0x03470810>:
From cffi callback <function get_string at 0x03470810>:
From cffi callback <function get_string at 0x03470810>:

这个错误是什么意思?我没有内存问题,因为我使用新的 free_char 函数释放了内存。顺便说一句,如果我删除对 free_char 的调用,我可以进行所有循环(但不会释放内存)。

最佳答案

来自 cffi 的文档:

The recommended C compiler compatible with Python 2.7 is this one:
http://www.microsoft.com/en-us/download/details.aspx?id=44266
...
For Python 3.4 and beyond:
https://www.visualstudio.com/en-us/downloads/visual-studio-2015-ctp-vs

那你要么降级python,要么升级visual-studio。

关于python - 使用CFFI释放内存时 "MemoryError: Stack overflow"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55322716/

相关文章:

python - 在 Python 2.7.9 中安装包

c++ - 为什么我在静态库中缺少显式模板特化的符号?

c++ - 使用适用于 Win32 的英特尔 C++ 编译器编译 Qt 时出错,我做错了什么?

c++ - 下载文件的 CMake 检查哈希 (MD5/SHA256)

python - Python 基本类型的内存使用(尤其是 int 和 float)

python - 实现一般反向传播

python 使用argparse获取位置参数的长度

python - setuptools_scm 可用时是否需要bumpversion(或bump2version)?

c - valgrind 中的内存

php - 准备好的语句可能会导致内存泄漏?