[PyQt] [SIP] Python crash with QString

Jens Thoms Toerring jt at toerring.de
Fri Sep 9 22:02:39 BST 2011


Hi,

   I've got a SIP wrapper for a C++ library and have some issue
with running under Win32 after compiling with Visual Studio 10.
Within the library I have a number of functions that return
QString objects or (mostly const) references. Under Linux every-
thing works fine, e.g. if I do in a Python script

  print yaba.Version.text( )

I get the version string (which is a QString returned by the
wrapped library) output to the screen. Under Windows Python
crashes after outputting the string with an access violation
(C0000005). The crash seems to happen whenever I use a QString
object (or reference) returned by the library wrapped with SIP,
e.g. also

  print yaba.Version.text( ).length( )

crashes. The same happens with all other methods of the
QString class I tried.

I can avoid the crash in this case by assigning the QString
object to another Python variable first, i.e. with

   dummy = yaba.Version.text( )
   print dummy

it seems to work.

To confirm that this is a problem with QString I then wrote
an extremely simple library, containing just a single class
with a single (static) method that returns a QString, plus an
equally simple SIP wrapper for it. Again, everything works
fine under Linux but crashes under Windows the first time the
QString (or PyQt4.QtCore.QString) object is used. And with
this much simplified library even the "trick" of copying
the QString to a local variable first doesn't work anymore.
The error is again an access violation and as far as I can
tell it seems to be an attempt to write to address 0x0.

The SIP version I'm using under Windows is 4.12.2 (under
Linux it's 4.10.1). The Windows version is 7 Professional
32-bit and the compiler Visual Studio 10. Has anyone an idea
what could be behind this? Since I have nearly no experience
with Windows and, after having struggled for two weeks to get
the whole stuff to built, I'm not too keen on spending what's
probably's going to be another month to learn how to obtain a
debug built under this environment, thus I can at the moment
only report my observations and no more details...

To check for possible memory problems with the simple library
(though I can't see where any could come from) I also run the
Linux version under valgrind and just got the usual complaints
about stuff from what looks like somewhere deep in the inter-
nals Python, but nothing that would let me suspect that there's
anything broken in the library.

I append the relevant code of the simple library and SIP wrap-
per below (with qs_lib.hpp and qs_lib.cpp being the code for
the library to be SIP-wrapped, qs_sip.sip the SIP wrapper and
qs.py the script that crashes Python) in the hope that it is
of any help.
                           Best regards, Jens

----- qs_lib.hpp ----------------------------------------------

#ifndef QS_HPP_
#define QS_HPP_

#if ! defined _MSC_VER      // this isn't MS Visual Studio
#    define DLL_STUFF
#else
#    if defined BUILDING_LIB
#        define DLL_STUFF __declspec( dllexport )
#    else
#        define DLL_STUFF __declspec( dllimport )
#    endif
#endif

#include <QString>

class DLL_STUFF QS {
  public:
    static QString text( );
};

#endif

----- qs_lib.cpp ----------------------------------------------

#include "qs_lib.hpp"

QString
QS::text( ) {
    return QString( "Hello world" );
}

----- qs_sip.sip ----------------------------------------------

%Module qs_sip 0

%Import QtCore/QtCoremod.sip
%Import QtGui/QtGuimod.sip

class QS
{
%TypeHeaderCode
#include "qs_lib.hpp"
%End
  public:
    static QString text( );
};

----- qs.py ----------------------------------------------

import qs_sip as qs
print qs.QS.text( )

-- 
  \   Jens Thoms Toerring  ________      jt at toerring.de
   \_______________________________      http://toerring.de


More information about the PyQt mailing list