Segfault getting object address in 2.5.3 on Debian

Daniel Calvelo wrote:

Hi all,

Sorry for the keywordish subject line, but it's a bit long to put simply.

I'm trying to port the Thuban geographic data viewer (thuban.intevation.de)
from 2.4 to 2.5. The wxPython part is not a major hassle, even without using
wxversion.

Now, there is a piece of the code where C++ is used for speed. It must
interoperate very closely with a Python-side created wxDC. Specifically, a
wxMemoryDC is created in Python, and it is passed to a C++ function to be
drawn on.

In 2.4, the following works:

- take the 'this' property of the PyObject*,

- extract the hex part after the _, before the _p

- convert that to an address

- cast it to the appropriate type

And there you have the C++ object corresponding to the Python one.

That is definitly *not* the way to do it. Instead:

#include <wx/wxPython/wxPython.h>

  wxMemoryDC* dc;
  if (! wxPyConvertSwigPtr(pyobject,
                                  (void**)&dc, wxT("wxMemoryDC")) {
    // handle the error...
  }
  // use the DC...

Here are the docs and prototype for the function:

// Extract a pointer to the wrapped C++ object from a Python proxy object.
// Ensures that the proxy object is of the specified (or derived) type. If
// not able to perform the conversion then a Python exception is set and the
// error should be handled properly in the caller. Returns True on success.
bool wxPyConvertSwigPtr(PyObject* obj, void **ptr,
                         const wxChar* className);

ยทยทยท

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Robin Dunn wrote:

Daniel Calvelo wrote:

[...]

> In 2.4, the following works:
>
> - take the 'this' property of the PyObject*,
>
> - extract the hex part after the _, before the _p
>
> - convert that to an address
>
> - cast it to the appropriate type
>
> And there you have the C++ object corresponding to the Python one.

That is definitly *not* the way to do it. Instead:

#include <wx/wxPython/wxPython.h>

  wxMemoryDC* dc;
  if (! wxPyConvertSwigPtr(pyobject,
                                  (void**)&dc, wxT("wxMemoryDC")) {
    // handle the error...
  }
  // use the DC...

Here are the docs and prototype for the function:

Thank you Robin. That worked OK. Just a minor note: it isn't a function but a
macro, so you do have to #include wxPython.h, it's not enough declaring a
prototype, lest you get undefined symbols because of C++ name mangling.

If the Debian package maintainer is on this list, just a note: wxPython.h and
the rest of headers needed for this to work are not in the wxPython .deb.

Thanks again.

Daniel.