简体   繁体   中英

Python Objects in C++ DLL

I coded a C++ DLL to use in MQL4 . I have to use some python modules.

DLL Code:

#define WIN32_LEAN_AND_MEAN  // Exclude rarely-used stuff from Windows headers
//#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <windows.h>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include "Python.h"
#include "numpy/arrayobject.h"
//----
#define MT4_EXPFUNC __declspec(dllexport)

PyObject *Amanda;
PyObject *AmandaZones;


#pragma pack(push,1)

#pragma pack(pop)


BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
  {
//----
   switch(ul_reason_for_call)
     {
      case DLL_PROCESS_ATTACH:
          Py_Initialize();
          import_array1(-1);
          PyObject *pName;
          pName = PyString_FromString("Amanda");
          Amanda = PyImport_Import(pName);
          Py_DECREF(pName);
          if (Amanda != NULL){
              AmandaZones = PyObject_GetAttrString(Amanda, "CalculateZones");
          }
          break;
      case DLL_THREAD_ATTACH:
          break;
      case DLL_THREAD_DETACH:


          break;
      case DLL_PROCESS_DETACH:
              Py_DECREF(Amanda);
              Py_DECREF(AmandaZones);
              Py_Finalize();
          break;
     }
//----
   return(TRUE);
  }

MT4_EXPFUNC int __stdcall CalculateZones(double *data, double quantile,const int arraySize)
  {
    if (arraySize < 1 || data == NULL) return 2;
    if (AmandaZones == NULL) return 0;
   return 1;
  }

It works well until I removed it from the Metatrader and attach it again. It returns 0 which simply means AmandaZones is NULL .

Simply, If the DLL file attached for second time, PyObjects wouldn't be loaded and return NULL Pointers. What's the problem with code? I think it's a memory leak problem, because If I remove if (AmandaZones == NULL) return 0; , It throws Access Violation Read error .

Thank you

numpy should not be initialize twice when using in C++. Py_finilize() not works correctly and does not remove numpy module objects. I solve it from here https://github.com/numpy/numpy/issues/8097

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