简体   繁体   中英

How to debug - Python CTypes causing *glibc detected*

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM