[wxPython] C++ Resources/memory management

Are there any pointers to general information regarding how wxWindows
objects/resources are managed by the wxPython wrapper? In particular, I'm
trying to determine when such resources are freed in the context of some
applications of mine that seem to be leaking memory within fairly simple
code. My environment at the moment is Windows NT 4.0 with wxPython 2.1.16
(I was holding off changing to 2.2.0 while waiting for 2.2.1).

For example, I had an application that had a simple window with a wxTimer
that would generate events to update a dynamic drawing in the window. In
the event handler, I obtained a client DC, made the drawing and then
returned. Whether I simply exit from the method, or explicitly 'del' the
DC, I get a nice, slow, but constant memory leak. In my application the
timer was firing about 10x a second and my memory footprint was growing 10s
of Ks per minute at least. To resolve this I grabbed a single client DC
during window creation and use it during operation, but I'd rather not have
to hold onto it like that.

In the same application, I made use of wxPen constructors to directly set
the pen for a paint DC (supplied automatically in my paint handler) - with
code such as:

    dc.SetPen(wxPen(wxWHITE,2))
    (do drawing operation)
    dc.SetPen(wxPen(wxBLACK,2))
    (do other drawing operation)

and I'd also be leaking memory. Just removing the use of the wxPen calls
would change the behavior.

I suppose what I'm curious about is how the underlying C object references
are managed. In my first case, I had assumed that when the Python object
(the client DC) was destroyed, so would be the C object. But I guess that
can't be a general rule, since in the latter case I don't keep a Python
reference to the Pen but still expect that the DC can reference it. So it
sort of has to be destroyed when I reset the pen the next time, but I don't
know that there is code to handle that. Should I be explicitly trying to
manage the destruction of the underlying C++ objects in such cases?

I should note that the pen case is strange, because a glance at the wxPython
wrapper seemed to be using wxPenList->FindorCreatePen() which I would have
thought would have just kept reusing my two pens since I just oscillated
back and forth between them.

Are there any general guidelines as to how to manage such objects
(particularly all the little GUI elements such as pens, brushes, DCs,
etc...) so that you don't leak memory down in the C++ layer even though the
Python layer is handling its memory properly? Many of my applications are
utilities that run for long periods of time and even a slow memory growth
can be catastrophic over a long interval.

Thanks.

-- David

/-----------------------------------------------------------------------\
\ David Bolen \ E-mail: db3l@fitlinxx.com /
  > FitLinxx, Inc. \ Phone: (203) 708-5192 |
/ 860 Canal Street, Stamford, CT 06902 \ Fax: (203) 316-5150 \
\-----------------------------------------------------------------------/