|
From: | Laurent Deniau |
Subject: | Re: POC destructors? (was Re: bogus retain via NSEnumerator) |
Date: | Fri, 23 Apr 2004 11:39:54 +0200 |
User-agent: | Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.5) Gecko/20031107 Debian/1.5-3 |
Ben Golding wrote:
In article <4088AA4F.DDE8A533@nospam.idiom.com>, "John C. Randolph" <jcr@nospam.idiom.com> wrote:Of course not. I balance my retains and releases, I use -release when I want to get rid of something immediately, and I use autorelease when something else might want to keep the object in question around. In other words, I follow the published rules for retain/release.This discussion left me wondering how you can be sure when a program using garbage collection can know when an object is finally released.If I have a class that opens a file in it's init method, like: @implementation MyFile - (id)initWithPath:(char *)path { if ((self = [super init]) == nil) return nil; _fp = fopen(path, "r"); return self; } - (void)dealloc { fclose(_fp); [super dealloc]; }@endHow can I know when the -dealloc method is called by the garbage collector? If I create a lot of files using my class, how can I get POC to call the destructor so I don't run out of file descriptors?
You can't (do not rely on finalization) but you should not need to do it... -(void)dealloc { [self close]; [super dealloc]; } -(int)close { int err = 0; if (_fp != NULL) err = fclose(_fp), fp = NULL; return err; }This is a "well-know" design pattern in OO language when you use the paradigm "ressource acquisition is initialization".
I had a look at the POC documentation and I couldn't see any documentation on what conditions a destructor is called, or even if there is a destructor method that's invoked when an object is destroyed.One advantage of the autorelease pool mechanism is that when I send a -release message to an object and there are no other references, I know that its -dealloc method will be called then and there. There are occasions when I want to defer the release of the objects because it could impact the UI experience in which case I use -autorelease. The simple control that gives is quite something.So is there a way to force an object to call a destructor in POC or are you at the dependent on the garbage collector?Ben.
ld.
[Prev in Thread] | Current Thread | [Next in Thread] |