Error using callback in Python

2020-04-02 09:03发布

问题:

I am developing a dll that should be used in Python. I have a callback function to send my parameters (defined in a separate header):

typedef int(*call_nBest)(char **OutList, float* confList, int nB);

So, I'm using this callback in this way:

#define TEXT_BUFFER_MAX_SIZE 50
call_nBest nBestList;
void Xfunction(const char* aLineThatWillBeConvertedInAList){
    char **results;
    float *confidences;
    confidences=new float[nBest];
    results=new char*[nBest];
    for(int i=0; i<nBest; i++) results[i]=new char[TEXT_BUFFER_MAX_SIZE];

    MakeLine2List(aLineThatWillBeConvertedInAList,results,confidences); 

    /*At this function I am having the error :(*/
    nBestList(results,confidences,nBest); // Passing the values to my callback

    for(int i=0; i<nBest; i++) delete [] results[i];
    delete [] confidences;
    delete [] results;

}

And I'm exporting it in this way:

__declspec(dllexport) int ResultCallback(call_nBest theList){
    nBestList = theList;
    return(0);
}

I tested my callback first in another C++ application in this way:

int MyCallback(char **OutLi, float* confLi, int nB){
    printf("\n The nB results: %d \n",nB);
    for(int n=0; n<nB; n++){
        std::cout << *(confLi+n) << "\t" << OutLi[n] << "\n";
    }
    return(0);
}

In main() I give the callback in this way:

ResultCallback(MyCallback);

and it works pretty well. But I don't have any idea how to adapt this to Python. I have tried this:

Note: I have changed the last way, because I resolved some mistakes, but I'm still getting an error. This is the current way of how I am loading myDLL

from ctypes import *
def callbackU(OutList,ConList,nB):
    for i in range(nB):
        print(OutList[i][0:50]) #I don't know how to print the values
return 0

myDLL = cdll.LoadLibrary("MyLibrary.dll")

calling = CFUNCTYPE(c_int,POINTER(POINTER(c_char)),POINTER(c_float),c_int)
theCall= calling(callbackU)
myDLL.ResultCallback(theCall)

myDLL.StartProcess(); #In this process the given callback will be invoqued

ERROR

And now I have this error:

Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. at Xfunction(SByte* aLineThatWillBeConvertedInAList)

Problem signature:

Problem Event Name: APPCRASH
Application Name: python.exe
Application Version: 0.0.0.0
Application Timestamp: 54f9ed12
Fault Module Name: MSVCR100.dll
Fault Module Version: 10.0.40219.325
Fault Module Timestamp: 10.0.40219.325
Exception Code: c0000005
Exception Offset: 00001ed7
OS Version: 6.3.9600.2.0.0.256.4
Locale ID: 1033
Additional Information 1: 5861
Additional Information 2: 5861822e1919d7c014bbb064c64908b2
Additional Information 3: a10f
Additional Information 4: a10ff7d2bb2516fdc753f9c34fc3b069

Things that I've done and are almost what I want:

First I changed the callback Python function for this one:

def callbackU(OutList,ConList,nB):
    for i in range(nB):
        print(i)
return 0

All works with no error and I can see this in the Console (in this case nB was 10):

0
1
...
9

Second, I changed the function as this one:

def callbackU(OutList,ConList,nB):
    for i in range(nB):
        print (cast(OutList,c_char_p))
return 0

and, oh surprise this prints only the first word of the list (nB times)

回答1:

Do you want something like this?

def callbackU(OutList, ConList, nB):
    for i in range(nB):
        print("{}\t{}".format(ConList[i], cast(OutList[i], c_char_p)))
    return 0

From what I understand you're just trying to match the output of your Python callbackU function with your C++ MyCallback function.

Python has a variety of string formatting functionality that can be confusing at first, but pays homage to printf string formatting.

Since OutList has type LP_LP_c_char (pointer to pointer of c_char, vs "NULL terminated char *" c_char_p), we'd best turn it into a native Python data type like so:

def callbackU(OutList, ConList, nB):
    for i in range(nB):
        out_list_item = cast(OutList[i], c_char_p).value
        print("{}\t{}".format(ConList[i], out_list_item))
    return 0