Home > Enterprise >  C6011 in C and Python project
C6011 in C and Python project

Time:08-10

I am in my third CS class online and I have done ok until now but I am really struggling with this. My code runs fine through the menu and input validation just fine but then as soon as I call a function from the python file I get the dereferencing null pointer message as follows " EXCEPTION UNHANDLED: Unhandled exception at 0x1DD09F27 (python36.dll) in moduleSixCppAndPython.exe: 0xC0000005: Access violation reading location 0x00000004. occurred".

I'm going to try to include my c code so forgive me if i mess this it up.. this is my first time using stackoverflow. the sections underlined in my IDE are the line as follows:

pFunc = PyDict_GetItemString(pDict, procname); // this one get like a red X next to it ... Py_DECREF(pValue); // this line is underlined

all issues are coming from the "int callIntFunc(string proc, int param)" funtion

main is not really finished yet so i'm not super concerned with that unless that's where my problem is coming from...

any guidance at all would be very greatly appreciated!

#include <Python.h>
#include <iostream>
#include <Windows.h>
#include <cmath>
#include <string>
#include <conio.h>

using namespace std;

bool CONTINUE_RUN = true;

int displayMenu() {
    int userInput = 0;
    while (true) {
        
        cout << "1: Display a Multiplication Table" << endl;
        cout << "2: Double a Value" << endl;
        cout << "3: Exit" << endl;
        cout << "Enter your selection as a number 1, 2, or 3." << endl;

        while (!(cin >> userInput)) {
            system("cls");
            cout << "ERROR: Please enter 1, 2, or 3" << endl;
            cout << "1: Display a Multiplication Table" << endl;
            cout << "2: Double a Value" << endl;
            cout << "3: Exit" << endl;
            cout << "Enter your selection as a number 1, 2, or 3." << endl;
            
            cin.clear();
            cin.ignore(123, '\n');
        }
        if (userInput == 1) {
            break;
        }
        if (userInput == 2) {
            break;
        }
        if (userInput == 3) {
            CONTINUE_RUN = false;
            break;
        }
        else {
            system("cls");
            cout << "ERROR: Please enter 1, 2, or 3" << endl;
            continue;
        }
    }
    return userInput;
}

int userData() {
    int pickNum;
    system("cls");
    cout << "Please enter an integer: " << endl;
    
    while (!(cin >> pickNum)) {
        system("cls");
        cout << "ERROR: Please enter an INTEGER:";
        cin.clear();
        cin.ignore(123, '\n');
    }

    return pickNum;
}

int callIntFunc(string proc, int param)
{
    char* procname = new char[proc.length()   1];
    std::strcpy(procname, proc.c_str());

    PyObject* pName, * pModule, * pDict, * pFunc, * pValue = nullptr, * presult = nullptr;
    // Initialize the Python Interpreter
    Py_Initialize();
    // Build the name object
    pName = PyUnicode_FromString((char*)"PythonCode");
    // Load the module object
    pModule = PyImport_Import(pName);
    // pDict is a borrowed reference 
    pDict = PyModule_GetDict(pModule);
    // pFunc is also a borrowed reference 
    pFunc = PyDict_GetItemString(pDict, procname);
    if (PyCallable_Check(pFunc))
    {
        pValue = Py_BuildValue("(i)", param);
        PyErr_Print();
        presult = PyObject_CallObject(pFunc, pValue);
        PyErr_Print();
    }
    else
    {
        PyErr_Print();
    }
    //printf("Result is %d\n", _PyLong_AsInt(presult));
    Py_DECREF(pValue);
    // Clean up
    Py_DECREF(pModule);
    Py_DECREF(pName);
    // Finish the Python Interpreter
    Py_Finalize();

    // clean 
    delete[] procname;

    return _PyLong_AsInt(presult);
}

int main(){
        
    while (CONTINUE_RUN == true) {
            int userNumber = 0;
            int menuValue = displayMenu();
            if (menuValue == 1) {
                userNumber = userData();
                system("cls");
                int token = callIntFunc("MultiplicationTable", userNumber);
                cout << "Press any key to continue" << endl;
                _getch();

            }
            if (menuValue == 2) {
                userNumber = userData();
                system("cls");
                cout << callIntFunc("DoubleValue", userNumber);
                cout << "Press any key to continue" << endl;
                _getch();

            }
        }
        cout << "GOODBYE" << endl;
}

CodePudding user response:

OK so a big THANK YOU to @PaulMcKenzie in the comments for pointing me in the right direction... turns out the issue was not in my .cpp file but in fact in the .py file that it was reading. I had used the Python syntax: print(namedVariable "someString" (evaluatedCalculation) now while this was technically correct for some instances it was creating unpredictable results when passed from my .py file back to my .cpp file... the error was flagging in my .cpp file so the real error that was made was that I had tunnel vision in trying to find the error solely in the .cpp file and not anywhere else. PLEASE forgive a rookie of his rookie mistake here! To fix this, I altered the syntax in my .py file to: print(namedVariable, "someString", (evaluatedCalculation) the true error here was not just in the logic I applied in writing my Python code... but in the logic I applied in finding the source of my error. I learned much more in finding the flaw in my thinking than I did in finding the flaw in this code. But hey.. live and learn right? anyway, happy coding! Love and Respect!

  • Related