[PyQt] A gobal variable problem in sip

Phil Thompson phil at riverbankcomputing.com
Wed Oct 24 18:01:01 BST 2012


On Wed, 24 Oct 2012 06:11:13 +0800, "D.Y Feng" <yyfeng88625 at gmail.com>
wrote:
> I meet a problem in my code:
> 
> hello.h:
> 
> #include<QtCore>
>>
>> class Hello : public QObject{
>>     Q_OBJECT
>> public:
>>     Hello(QObject *parent = 0);
>>     static int test(){
>>         static int i=0;
>>         qDebug()<<i++;
>>         return 0;
>>     }
>> };
>>
> 
> hello.cpp:
> 
>> #include "hello.h"
>>
>> const int i=Hello::test();
>>
>> Hello::Hello (QObject *parent)
>> {
>> }
>>
> 
> hello.sip
> 
> %Module hello
>>
>> %Import QtCore/QtCoremod.sip
>>
>>
>> class Hello : public QObject {
>>
>> %TypeHeaderCode
>> #include "hello.h"
>> %End
>>
>> public:
>>      Hello(QObject *parent = 0);
>>      static int test();
>> };
>>
> 
> I use the* const int i=Hello::test();* to invoke test() before
> *main*function.It's uesful in factory
> pattern when you want to auto  register driver.
> 
> the python test code
> 
>> import hello
>> hello.Hello.test1()
>>
> 
> It seems *static int i*  in *test() *changed after *import hello*
> 
> Breakpoint 1, Hello::test1 () at hello.h:8
>> 8           static int test1(){
>> (gdb) p i
>> $1 = 0
>> (gdb) p &i
>> $2 = (const int *) 0x7ffff60300d0
>> (gdb) c
>> Continuing.
>> 0
>>
>> Breakpoint 1, Hello::test1 () at main.h:8
>> 8           static int test1(){
>> (gdb) p &i
>> $3 = (const int *) 0x7ffff60300d0
>> (gdb) n
>> 10              qDebug()<<i++;
>> (gdb) p &i
>> $4 = (int *) 0x7ffff62355c0       <---What happen here?
>>
> 
> 
> Thx for reading.

If I have understood correctly you have C++ code that is linked against a
.so and embeds Python which dlopens a sip generated extension module linked
against the same .so. I don't know enough about Linux dynamic loading to
say what happens to static initialisers in those circumstances - but I
don't think it is a sip specific problem.

Phil


More information about the PyQt mailing list