What should I explicitly Destroy()?

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()?

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). So I just made these
members of my Panel. Hopefully no one out there in
'net-land is screaming "DON'T DO THAT!" just yet. If
so, please explain why, so that I'll know.

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. No biggie. However, due
an abundance of caution, 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.

I'm fine with not Destroy()ing wxBitmap's, but I want
to know more. Why shouldn't I? What should I
Destroy()? I generally just Destroy() dialogs and let
the rest of it take care of itself. Is that okay?

Thanks,

Gre7g

···

____________________________________________________________________________________
Sucker-punch spam with award-winning protection.
Try the free Yahoo! Mail Beta.
http://advision.webevents.yahoo.com/mailbeta/features_spam.html

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@noaa.gov

Many thanks for the useful tips, Chris.

<snipped>

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

So should we stop doing this? It sure would clean up
my code some if I didn't have try/finally's all over
it to make sure these things are Destroy()ed.

Can anyone put forth a good argument for Destroy()ing
or not Destroy()ing wxDialog's?

Gre7g

···

--- Christopher Barker <Chris.Barker@noaa.gov> wrote:

____________________________________________________________________________________
Need a vacation? Get great deals
to amazing places on Yahoo! Travel.

Many thanks for the useful tips, Chris.

<snipped>

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

So should we stop doing this? It sure would clean up
my code some if I didn't have try/finally's all over
it to make sure these things are Destroy()ed.

Using try/finally to destroy a dialog be a bit of overkill. I don't
think I've seen any code in wxPython that uses it to destroy objects,
though it is necessary for releasing locks.

Can anyone put forth a good argument for Destroy()ing
or not Destroy()ing wxDialog's?

Explicit is better than implicit.

Destroy dialogs because it is more explicit than letting them die by
themselves. Also, you can control when they are destroyed when you do
it manually (especially important if you have some code that steps
through a series of dialogs).

I would get rid of the finally statements though, that's a bit of
overkill.

- Josiah

···

Gre7g Luterman <hafeliel@yahoo.com> wrote:

--- Christopher Barker <Chris.Barker@noaa.gov> wrote:

Gre7g Luterman wrote:

....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). So I just made these
members of my Panel.

I'm not sure if it would apply in your case, but I used similar reasoning a while ago to keep a reference to a wxClientDC. It all worked fine until I ran the code under OS X (with an updated version of wxPython) and started getting assertion errors - apparently the associated wxPaintDC doesn't like to be 'nested' with an already existing DC. Getting rid of the reference cured the problem.

···

--
Regards,

David Hughes