[PyKDE] ways to workaround a qcanvas segfault?

Solly Brown sollyb at cse.unsw.edu.au
Fri Oct 22 04:03:26 BST 2004


Hi all,

I've been using QCanvasSprites and it seems far too easy to create a 
seg-fault. I've worked out that the following code is a no-no:

class Main( QMainWindow ):
 	def __init__( self, parent = None, name = None ):
 		QMainWindow.__init__( self, parent, name )
 		pixmap = [QPixmap("mySpritePicture.png")]
 		spritePix = QCanvasPixmapArray( pixmap )
 		self.mySprite = QCanvasSprite( spritePix, myCanvas )

If you try and call self.mySprite.show() you get a segmentation fault. I 
presume the reason for this is that the python garbage collector gets rid 
of spritePix while self.mySprite still has a reference to it within the 
underlying C++ code. I can fix it easily enough by changing spritePix to 
self.spritePix, which guarantees that it won't be prematurely cleaned up.

I've got a trickier problem though:

I want a separate class for each type of sprite, inheriting from 
QCanvasSprite. Following object-oriented principles, I'd like to store the 
pixmaps for each class of sprite within the class itself -- I can do this 
by passing in a dummy (blank) list of pixmaps for my sprite and then using 
the setSequence() method within the sprite's __init__ method:

class mySprite( QCanvasSprite ):
 	def __init__( self, dummyPix, parentCanvas ):
 		QCanvasSprite.__init__( self, dummyPix, parentCanvas )
 		pixmap = [QPixmap("spritePicture.png")]
 		self.spritePix = QCanvasPixmapArray( pixmap )
 		self.setSequence( self.spritePix )

This works fine, until it comes to closing the main window -- it produces 
a seg fault. In this case I can't work out how to stop python cleaning 
things up before C++ trys to delete everything (which again, I assume is 
what is producing the seg fault).

Does anyone know a work-around? Can I store my sprite's pixmaps within the 
class itself without getting a seg-fault upon exit, or do I always have to 
pass them in from outside?

Thanks for your ideas......

Cheers, Solly




More information about the PyQt mailing list