0

I am doing a project for school, and I am not looking for answers, more so just an explanation on what this error is and how to correct it.

The scenario of the project does not matter for me to explain (unless you feel otherwise), but the code block the error is reading is this:

void CallProcedure(string pName)
{
    char* procname = new char[pName.length() + 1];
    std::strcpy(procname, pName.c_str());

    Py_Initialize();
    PyObject* my_module = PyImport_ImportModule("PythonCode");
    PyErr_Print();
    PyObject* my_function = PyObject_GetAttrString(my_module, procname);
    PyObject* my_result = PyObject_CallObject(my_function, NULL);
    Py_Finalize();

    delete[] procname;
}

The error occurs on line

PyObject* my_function = PyObject_GetAttrString(my_module, procname);

The error message is

Exception thrown at 0x1E0CB9E7 (python36.dll) in SetUp.exe: 0xC0000005:

Acess violation reading location 0x00000004."

I am very novice and very much a beginner so if I am missing something in regards of information I apologize.

I have tried googling this error and have not found a direct answer. I am merely trying to just run the code before I change anything to see what the output is in regard to what I need to change and where I am with the code provided to us in the project.

MaxPlankton
  • 1,158
  • 14
  • 28
  • 4
    Is `my_module` a valid pointer or `NULL`? – Retired Ninja Dec 06 '22 at 01:45
  • I apologize, I am very new. I am not exactly sure what you mean. – Ihavenoideawhatimdoing Dec 06 '22 at 01:47
  • 2
    He means `PyImport_ImportModule` returns a null pointer when it fails to find and load the module as a warning not to try using the module. In general always inspect the return value from a function before using it. – user4581301 Dec 06 '22 at 01:48
  • Side note: `PyObject* my_function = PyObject_GetAttrString(my_module, pName.data());` and save yourself from the rigamarole with `procname` – user4581301 Dec 06 '22 at 01:53
  • 1
    Recent similar question: [https://stackoverflow.com/questions/74644535/error-0xc0000005-access-violation-reading-location-0x00000004#comment131754919_74644535](https://stackoverflow.com/questions/74644535/error-0xc0000005-access-violation-reading-location-0x00000004#comment131754919_74644535) – drescherjm Dec 06 '22 at 01:55
  • 1
    Related: [https://stackoverflow.com/questions/17532371/pyimport-import-fails-returns-null](https://stackoverflow.com/questions/17532371/pyimport-import-fails-returns-null) – drescherjm Dec 06 '22 at 01:55
  • 1
    Just because this is where the program crashes or reports an error doesn't mean this is where the problem is. C++ does not work this way. The problem can be anywhere in your code, but after the bug occurs the program keeps running for a little bit before it finally crashes here. This is why stackoverflow.com's [help] requires you to show a [mre] that everyone else can cut/paste ***exactly as shown***, then compile, run, and reproduce your problem. See [ask] for more information. Until you do that, it is unlikely that anyone will be able to answer your question. – Sam Varshavchik Dec 06 '22 at 01:58

0 Answers0