3

I've compiled a C++ program into a dynamic library, which I'm now calling in python via ctypes. However, I get the following error:

*** glibc detected *** ../../bin/python: free(): invalid pointer: 0x00007fbdf8ae3500 ***

How do I figure out if this is an error in the C++ code, or in how I'm using ctypes?

The C++ library runs succesfully when it is linked with a C program, or if I compile it as a standalone program. When combined with ctypes it always crashes at this C++ line:

// ... A bunch of code
if(tempBuffer) {
    delete[] tempBuffer;
    // More code below that never runs...

Python code that fails:

def test(fileName):
    feedbackLib = cdll.LoadLibrary("./libpitch.so")

    # Set return type to a character pointer
    feedbackLib.getPitchString.restype = c_char_p
    feedbackLib.getPitchString.argtypes = [c_char_p]

    # Crashes Here!
    feedbackStr = feedbackLib.getPitchString(fileName)
    return feedbackStr

For some reason, I can get the python program to work successfully if I change things around like so:

def test(fileName):
    # I've only changed this line
    feedbackLib = CDLL("./libpitch.so")

    # Set return type to a character pointer
    feedbackLib.getPitchString.restype = c_char_p
    feedbackLib.getPitchString.argtypes = [c_char_p]

    # No longer crashes!
    feedbackStr = feedbackLib.getPitchString(fileName)
    return feedbackStr 

However, if I start adding things to the above function (which I need to do) it'll give me other glibc errors.


Here's information on how I compiled the C++ in case it's relevant.

Header of function I'm calling, using extern "C" seems to be how to get C++ code working with CTypes:

extern "C" char* getPitchString(char* filename);

g++ commands:

$(CXX) $(LDFLAGS) -fPIC -c $(SRCS) $(LIBS)
$(CXX) -shared -Wl,-soname,libpitch.so -o libpitch.so $(OBJS) $(LIBS)
2
  • The Python code looks correct. Why is getPitchString calling free? Commented Mar 28, 2012 at 0:33
  • getPitchString calls a bunch of code from within the C++ library. Commented Mar 28, 2012 at 7:07

1 Answer 1

2

Turns out there was an error in the C++ portion of the code. I managed to get that part to segfault independently of Python/CTypes, and after debugging it seems to work.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.