views:

96

answers:

1

First of all, the problem is that program fails with double memory freeing ...

The deal is: I have

FooCPlusPlus *obj;

and I pass it to my script. It works fine. Like this:

PyObject *pArgs, *pValue;
pArgs = Py_BuildValue("((O))", obj);
pValue = PyObject_CallObject(pFunc, pArgs);

where pFunc is a python function... So, my script has function, where I use obj.

def main(args)
  ...
  pythonObj = FooPython(args[0])

  ...
  # hardcore calculation of "x" 
  ...

  ...
  pythonObj.doWork(x)

Of course I've defined python class

class FooPython:
  def __init__(self, data):
     self._base = data      

  def doWork(arg):
    import extend_module
    extend_module.bar(self._base, arg)

"Extend_module" is an extension c++ module where I've defined function "bar".

I expected that "bar" function would work fine, but instead of it I got memory errors: "double memory free or corruption".

Here is "bar" function:

static PyObject* bar(PyObject *self, PyObject *args)
{
    PyObject *pyFooObject = 0;
    int arg;
    int ok = PyArg_ParseTuple(args,"Oi",&pyRuleHandler, &arg);
    if(!ok) return 0;

    void * temp = PyCObject_AsVoidPtr(pyFooObject);
    FooCPlusPlus* obj =  static_cast<FooCPlusPlus*>(temp);

    obj->method(arg); // some c++ method  
    return PyCObject_FromVoidPtr((void *) ruleHandler, NULL);
}

It fails at "bar"'s return statement...

A: 

Well, finally I know where the problem was:

we should return from "bar" function input args:

return args;

instead of

return PyCObject_FromVoidPtr((void *) ruleHandler, NULL);
Nelly