[PyKDE] Lambda function call in connect statement

Phil Thompson phil at river-bank.demon.co.uk
Sat Mar 3 11:45:35 GMT 2001


"Aaron J. Ginn" wrote:
> 
> Phil Thompson wrote:
> 
> > However, I don't like segfaults - send me a small but complete script
> > that demonstrates the problem and I'll take a look.
> >
> > Phil
> 
> I've attached a script that displays the core dump.  I also included the
> non-lambda version of the connect statement that shows how the pop-up is
> created prior to the construction of the main window.

Sorry - I should have noticed this right at the start...

The problem is that you are not keeping a reference to the function
object that lambda returns - so it is getting garbage collected
immediately. The signal is them emitted to an object that no longer
exists, hence the segfault. Change the code to be something like...

        self.lam = lambda s=self,x=num:s.changeTopStruct(x)
        self.connect( self.top_struct_button, SIGNAL( "clicked()" ),
                      self.lam )

QObject.connect does not keep a reference to the slot object to avoid
(potential) hidden circurlar references - but it does mean that you have
to manage them yourself.

The weakreferences module in Python 2.1 looks interesting - it might
offer a more programmer friendly solution.

Phil




More information about the PyQt mailing list