2

I need to call a function in a C library from python, which would free() the parameter. So I tried create_string_buffer(), but it seems like that this buffer would be freed by Python later, and this would make the buffer be freed twice.

I read on the web that Python would refcount the buffers, and free them when there is no reference. So how can I create a buffer which python would not care about it afterwards? Thanks.

example: I load the dll with: lib = cdll.LoadLibrary("libxxx.so") and then call the function with: path = create_string_buffer(topdir) and lib.load(path). However, the load function in the libxxx.so would free its argument. And later "path" would be freed by Python, so it is freed twice

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Henry Hu
  • 524
  • 4
  • 12
  • Post your example code and what's wrong with it. The question is unclear. – Daenyth Jun 27 '11 at 18:04
  • I load the dll with: `lib = cdll.LoadLibrary("libxxx.so")` and then call the function with: `path = create_string_buffer(topdir)` and `lib.load(path)`. However, the load function in the libxxx.so would free its argument. And later "path" would be freed by Python, so it is freed twice. – Henry Hu Jun 27 '11 at 18:09
  • So that people can answer you more easily, the question should contain all the needed info -- edit the question to add it rather than putting it in a comment that people will miss. – Daenyth Jun 27 '11 at 18:11

1 Answers1

3

Try the following in the given order:

  1. Try by all means to manage your memory in Python, for example using create_string_buffer(). If you can control the behaviour of the C function, modify it to not free() the buffer.

  2. If the library function you call frees the buffer after using it, there must be some library function that allocates the buffer (or the library is broken).

  3. Of course you could call malloc() via ctypes, but this would break all good practices on memory management. Use it as a last resort. Almost certainly, this will introduce hard to find bugs at some later time.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • Thanks, I am trying not to modify the library. As this library only have three functions: `load`, `unload` and `request`, and the `load` function is called first, I think this is a design problem... Maybe I'll try to keep the reference in a dictionary, as this seems better than calling `malloc()`.... – Henry Hu Jun 27 '11 at 18:15