What does “MemoryError: Stack overflow” mean while releasing memory using CFFI?

淺唱寂寞╮ 提交于 2019-12-13 03:47:39

问题


This question follows this one.

I use CFFI to create a DLL and I call it from a C++ application. I was questioning myself to find how to release memory allocated by the DLL and I follow the idea mention by @metal in its answer.

Here is now my Python code:

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)

And my C++ code:

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

I get this result:

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>:

What does this error mean? I don't have memory problem because I release memory with my new free_char function. By the way, if I remove the call to free_char I can make all the loops (but the memory is not release).


回答1:


From documentation of 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

Then you should either downgrade python or upgrade visual-studio.



来源:https://stackoverflow.com/questions/55322716/what-does-memoryerror-stack-overflow-mean-while-releasing-memory-using-cffi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!