title: Using SWIG to pass C++ object pointers to Python, than back to C++ again
body: <p>I'm using SWIG to wrap 2 C++ objects, and I am embedding the Python interpreter in my application (i.e. calling PyInitialize() etc myself).</p>
  
  <p>The first object is a wrapper for some application data. 
  The second is a "helper" object, also written in C++, which can perform certain operation based on what it finds in the data object.</p>
  
  <p>The python script decides when/how/if to invoke the helper object.</p>
  
  <p>So I pass a pointer to my C++ object to SWIG/Python thus:</p>
  
  <pre><code>swig_type_info *ty = SWIG_MangledTypeQuery("_p_MyDataObject");
  if(ty == NULL)
   {
      Py_Finalize();
       return false;
   }
  
  PyObject *data_obj = SWIG_NewPointerObj(PointerToMyDataObject, ty, 0);
  if(data_obj == NULL)
  {
       Py_Finalize();
       return false;
  }
  
  ty = SWIG_MangledTypeQuery("_p_MyHelperObject");
   if(ty == NULL)
  {
       Py_Finalize();
      return false;
   }
  
  PyObject *helper_obj = SWIG_NewPointerObj(PointerToMyHelperObject, ty, 0);
  if(helper_obj == NULL)
  {
      Py_Finalize();
      return false;
  }
  PyTuple_SetItem(pArgs, 0, data_obj);
  PyTuple_SetItem(pArgs, 1, helper_obj);
  PyObject *pValue = PyObject_CallObject(pFunc, pArgs);
  if(pValue == NULL)
  {
       Py_Finalize();
       return false;
  }
  </code></pre>
  
  <p>In Python, we see something like:</p>
  
  <pre><code>def go(dataobj, helperobj):
      ## if conditions are right....
      helperobj.helpme(dataobj)
  </code></pre>
  
  <p>Now, this largely works except for one thing. In my C++ code when I am preparing my arguments to pass on to the Python script, I observe the pointer value of PointerToMyDataObject.</p>
  
  <p>When I set a breakpoint in the C++ implementation of helperobj.helpme(), I see that the memory address is different, though it seems to be a pointer to a valid instance of MyDataObject.</p>
  
  <p>This is important to me, as "MyDataObject" is in fact a base class for a few possible derived classes. My helper object wants to perform an appropriate (determined by context) dynamic cast on the pointer it receives to point at the appropriate derived class. That's failing for what I think are obvious reasons now.</p>
  
  <p>I've read some things about "shadow" objects in SWIG, which only adds to my confusion (apologies for my tiny brain :-P) </p>
  
  <p>So, is SWIG making a copy of my object for some reason, and then passing a pointer to the copy? If it is, then I can understand why my assumptions about dynamic casts won't work.</p>
  
  <p>I Tried to add this as a comment, but struggled with formatting, so..... more insight follows:
  The problem has to do with pass-by-reference. Notice I have 2 implementations of the <em>virtual</em> method helpMe():</p>
  
  <pre><code>bool MyHelperObject::helpMe(MyDataObject mydata_obj)
  {
      return common_code(&amp;mydata_obj);
  }
  bool MyHelperObject::helpMe(MyDataObject *mydata_obj)
  {
      return common_code(mydata_obj);
  }
  </code></pre>
  
  <p>Although I provided python with a pointer, it is calling the pass-by-reference version. This explains why I'm getting different pointer values. But what can I do to force a call on the version that takes a pointer argument?</p>