4

I created a VCL Application in c++, borland. In my project there is a file where I have implemented embedded python in the methods defined in the same(my application contains a button which calls the method in which embedded python is implemented). when I compile, my build is successful. but when I run my application, and click on the button it shows the run time error : "Access violation at address 1E091375 in module 'PYTHON25.DLL'. Read of address 00000004" . please help. I have never used Python before. my program:

#pragma hdrstop

#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>


#include "Python.h"

#include "Unit1.h"
#include "Unit2.h"
#pragma link "python25_bcpp.lib"

//---------------------------------------------------------------------------

#pragma package(smart_init)

bool callHelloWorld(int intVal)
{
    char fName[] = "Hello"; //file name
    char cFunc[] = "hello"; //method name

    char *pfName, *pcFunc;

    PyObject *pName, *pModule, *pDict, *pFunc ;

    pfName = fName;
    pcFunc = cFunc;

    Py_Initialize();

    pName = PyString_FromString(pfName);

    pModule = PyImport_Import(pName);

    pDict = PyModule_GetDict(pModule);

    pFunc = PyDict_GetItemString(pDict, pcFunc);

    if (PyCallable_Check(pFunc))
    {
        PyObject_CallObject(pFunc, NULL);
    } else
    {
        PyErr_Print();
    }


    // Py_DECREF(pModule);
    // Py_DECREF(pName);

    Py_Finalize();

    return 0;
}
8
  • 3
    I seem to remember that Borland C++ uses a different calling convention (fastcall?). Is your python.dll compiled using the same compiler? Commented Oct 9, 2011 at 18:43
  • the python25.dll was in C:\Windows\SysWOW64. I didnt compile it. Commented Oct 9, 2011 at 18:55
  • I dont know if this would help or not, but I think you are missing some of the python files. I remember getting the same error when trying to compile my program on a different machine. But in my case I was using an MFC program and I was accessing my MFC DLL using the python script. Probably try reinstalling python. Commented Oct 9, 2011 at 19:18
  • I have placed all the files like .h ,lib and .dll in the source folder. I think the issue is with loading the module Hello.py. Can you please suggest how should i load the module successfully. Commented Oct 9, 2011 at 19:28
  • 1
    It won't be calling convention since python.h will specify cdecl. To help us can you tell us which line fails. Commented Oct 9, 2011 at 19:59

2 Answers 2

1

Check the return values of PyImport_Import (is the module in the search path?) and PyDict_GetItemString.

If that doesn't help put some trace messages in your app to see where it crashes.

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

Comments

1

This works for me:

Just delete Py_Finalize()

I read in another site that Py_Finalize has some problems in specific cases such as threading.

1 Comment

Same issue and same solution after all those years. Use Py_FinalizeEx for Python 3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.