What does "MemoryError: Stack overflow" mean while releasing memory using CFFI?
Asked Answered
C

1

0

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).

Cartwheel answered 24/3, 2019 at 10:14 Comment(2)
I just made a test with python2.7 building cffi with visual compiler aka.ms/vcpython27 and building C++ with Visual 2017, your code run till the end without problems. Which compiler did you used ?Muricate
@Muricate I use cl.exe in Visual Studio 2010 and the last version of Python (3.7.2).Cartwheel
M
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.

Muricate answered 24/3, 2019 at 14:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.