gc question

V

vpalexander

I keep seeing destructor calls in wx for ad hoc dialogs and wonder if
this is required, and if so, why would normal gc flow not be good?

def GetDir(self,Caption,DefaultDir):
dlg = wx.DirDialog(None,Caption,style = 1,defaultPath =
DefaultDir,pos = (10,10))
res = dlg.ShowModal()
pck = dialog.GetPath()
dlg.Destroy()

if res == wx.ID_OK:
return pck
else:
return ''


I'd like to write it as:

def GetDir(self,Caption,DefaultDir):
dlg = wx.DirDialog(None,Caption,style = 1,defaultPath =
DefaultDir,pos = (10,10))

if dlg.ShowModal() == wx.ID_OK:
return dialog.GetPath()
else:
return '' # probably implied; del 2 more lines?

....and gc takes care of things once scope is exited? Or is wx more
tricksome than that?
 
M

Marc 'BlackJack' Rintsch

I keep seeing destructor calls in wx for ad hoc dialogs and wonder if
this is required, and if so, why would normal gc flow not be good?

Because there is no guarantee when `__del__()` is called or if it is
called *at all*.

Ciao,
Marc 'BlackJack' Rintsch
 
V

Vince

Because there is no guarantee when `__del__()` is called or if it is
called *at all*.

Ciao,
Marc 'BlackJack' Rintsch

Well, that suits me. The most unnatural thing about Python was
adapting to the idea of just letting unreleased resources go jogging
off wherever. :)

"Where's the f.close?"
"You don't need it!"

Hmmm!
 
I

I V

Well, that suits me. The most unnatural thing about Python was adapting
to the idea of just letting unreleased resources go jogging off
wherever. :)

Yes, that's a bad habit that garbage collection can encourage. GC is good
for managing memory, but not good for managing other resources, so if an
object holds some other resource, it's important to make sure the
resource is released in a timely fashion rather than relying on the
finalizer (the __del__ function).

CPython uses reference counting, so it usually destroys objects fairly
soon after the stop being used. But JPython and IronPython don't do
reference counting, so forgetting to clean up resources can be a
significant problem.
"Where's the f.close?"
"You don't need it!"

Although, you still don't need f.close, with the new with statement:

with open('myfile') as f:
string = f.readline()
# f.close() gets called automatically here, without waiting for
# garbage collection.

If you want to use this in 2.5, you have to write:

from __future__ import with_statement

The big advantage here is that f.close will get called if the block exits
normally, or if there is an exception. For more, see
http://effbot.org/pyref/with.htm .
 
G

Gabriel Genellina

Yes, that's a bad habit that garbage collection can encourage. GC is good
for managing memory, but not good for managing other resources, so if an
object holds some other resource, it's important to make sure the
resource is released in a timely fashion rather than relying on the
finalizer (the __del__ function).

Although, you still don't need f.close, with the new with statement:

with open('myfile') as f:
        string = f.readline()
# f.close() gets called automatically here, without waiting for        
# garbage collection.

Just for completeness, on earlier Python versions, the way to ensure
that resources are always released is using try/finally:

f = open(...)
try:
...work with file...
finally:
f.close()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top