Python ctypes: how to free memory? Getting invalid pointer error

后端 未结 2 1570
旧巷少年郎
旧巷少年郎 2020-12-08 01:39

I want to get some string from a C/C++ library with ctypes into python. My code looks like this:

Code in lib:

const char* get(struct something *x) 
{         


        
2条回答
  •  南方客
    南方客 (楼主)
    2020-12-08 02:01

    a cheap hack around freeing the memory is to allocate it statically in C and never free it. only works if you know the maximum amount you need, of course, and if you know that you're done with before a second call to your routine.

    static char _externalStringBuffer[MAX_BUFFER_SIZE];
    char *get() {
        // put characters in _externalStringBuffer
        return _externalStringBuffer;
    }
    

提交回复
热议问题