0

I am trying to embed a python code as part of my cpp code. So, I impelement the following class to call RunModel in a loop after calling LoadModel:

class PyAdapter {
private:
    PyObject *pModule;
    PyObject *pModel;
    PyObject *pDetectionFunc;
public:
    LoadModel(){
        Py_Initialize();
        ...
        PyObject *pName = PyUnicode_FromString("detection");
        this->pModule = PyImport_Import(pName);
        Py_DECREF(pName);
        ...
        PyObject *pFunc, *pArgs, *pArg;
        pFunc = PyObject_GetAttrString(this->pModule, "model_init");
        pArgs = PyTuple_New(1);
        pArg = PyUnicode_FromString("m1.bin");
        PyTuple_SetItem(pArgs, 0, pArg);
        this->pModel = PyObject_CallObject(pFunc, pArgs);
        Py_DECREF(pArgs);
        Py_DECREF(pFunc);
        ...
        this->pDetectionFunc = PyObject_GetAttrString(this->pModule, "detect_me");
        ...
    }

    RunModel(){
        PyObject *pArgs, *pArg, *pDetection;
        pArgs = PyTuple_New(5);
        PyTuple_SetItem(pArgs, 0, this->pModel);
        ...
        pDetection = PyObject_CallObject(this->pDetectionFunc, pArgs);
        Py_DECREF(pArgs);
        ...
        Py_DECREF(pDetection);
    }

}

after calling the RunModel function for the first time it works properly. However, for the second step it throws:

'XYZ' object has no attribute 'ABC'

to print the pModel object attributes I use the following lines in my Python code:

from pprint import pprint
pprint(vars(MODEL))

for the first step it prints all the expected attributes but for the second step it returns

{}

Please help me understand what's wrong with my code?

EDIT:

I'm not sure which part of the code should be remove or remain. So, i published all the code here: https://pastebin.com/pXhBVbyw and this is test for PyAdapter class.

ma.mehralian
  • 1,274
  • 2
  • 13
  • 29
  • 1
    what makes you believe that the small snippet of code you provided is the problem? i can at least tell that out of context all these calls are valid, also you don't even show where you assign the 'ABC' attribute – AntiMatterDynamite Feb 05 '18 at 15:26
  • @AntiMatterDynamite The `pModel` never changed in cpp side it only stored to pass to the other function in `RunModel` (i.e. `xyxy` function in python) – ma.mehralian Feb 05 '18 at 15:32
  • You should try to produce a [mcve]. Nothing you show here should produce this error message so the issue is clearly elsewhere, in the code that you don't show. – DavidW Feb 05 '18 at 20:59
  • @DavidW I edit the post and share the code details. – ma.mehralian Feb 06 '18 at 07:55
  • @AntiMatterDynamite note edits for details of the code – ma.mehralian Feb 06 '18 at 07:56
  • @ma.mehralian 1) [We typically don't really like the relevant code hosted off-site](https://meta.stackoverflow.com/questions/339450/should-code-from-pastebin-be-edited-into-a-question) 2) this isn't really minimal (you should remove as much code as possible such that you can still verify the problem - this may well help you solve it) and it isn't complete and verifiable (we don't have the `detection` module or the `.bin` file it needs). I don't think anyone here has the information to help. – DavidW Feb 07 '18 at 18:17
  • @DavidW thank you for your comment. I change the code. The problem fortunately resolved and I post the answer. – ma.mehralian Feb 08 '18 at 07:16

1 Answers1

0

The problem related to calling Py_DECREF(pArgs) in RunModel function witch destroy all pArgs items (i.e this->pModel). So for the first run it works properly. However after calling Py_DECREF(pArgs) the this->pModel will destroyed and cannot be used for the next runs.

ma.mehralian
  • 1,274
  • 2
  • 13
  • 29