[wxPython-users] What should I explicitly Destroy()?

Christopher Barker Chris.Barker at noaa.gov
Mon Jun 18 16:28:00 PDT 2007


Gre7g Luterman wrote:
> According to all the wxPython examples I see, Dialogs
> should be Destroy()ed explicitly when you're done with
> them.  I've got that, but is there anything else we
> should Destroy()?

generally not.


> The reason I ask is that I'm doing my first program
> that requires a wxMemoryDC for off-screen painting.  I
> didn't like the idea of creating a new wxEmptyBitmap
> and wxMemoryDC with every screen update (it seems like
> this would cause memory to thrash a lot more than just
> keeping one of each around). 

correct

> So I just made these members of my Panel.

However, it's not considered good practice to keep a MemoryDC around, so 
you should generally keep the bitmap around, but re-create the DC when 
you need it. After all, if you didn't keep the bitmap around, there 
wouldn't be much point in using it at all!

> This seems to work just fine *IF* I create a new
> bitmap when the window size changes.  Resizing the
> bitmap didn't seem to work.

Correct, you can't re-size a bitmap -- how would you even try?

> I did try doing a Destroy()
> on the old bitmap before creating the new one.  You
> can go ahead and scream "DON'T DO THAT!" now.  That
> crashes things pretty hard.

right -- Python takes care of the memory management for you -- if it 
goes out of scope, it will be destroyed for you. For instance:

self.Buffer = wx.EmptyBitmap(....)

Every time you run this code, the old Bitmap object is deleted, as it no 
longer has anything referencing it.

Now that I think about it, I'm not sure you need to Destroy() Dialogs 
either...

Oh, and for more on off-screen bitmaps:

http://wiki.wxpython.org/DoubleBufferedDrawing
and
http://wiki.wxpython.org/BufferedCanvas

-Chris


-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov




More information about the wxpython-users mailing list