Dustin Oprea
Dustin Oprea

Reputation: 10236

Freeing memory allocated via ctypes in Python

I have a ctypes-based library that wraps a C-library that sometimes allocates memory that needs to be freed. Under Linux this is easy because common glibc functionality is been linked into that C library and I can simply call free() on it. However, it looks like the same functionality isn't available when I access that third-party-compiled library under Windows.

ctypes.util.find_msvcrt() seemed promising, but it returns None even though I'm running under Windows. I'm not sure if this means that the source DLL was built with something other than the Microsoft toolchain.

So, I'm left with memory to free and no apparent, simple way to do it. Does anyone have any ideas?

Upvotes: 1

Views: 462

Answers (1)

Mark Nunberg
Mark Nunberg

Reputation: 3691

When using the MSVCRT you need to ensure that you are using the same free/ malloc pairs from the same CRT (it's possible to have your program running with multiple CRTs). The library you are dealing with seems broken - the library should have a free wrapper which invokes the correct version of free.

In other words, even if you did manage to find some kind of MSVCRT free, there's no guarantee that it's the one you want.

See also http://siomsystems.com/mixing-visual-studio-versions/

Upvotes: 5

Related Questions