How to allocate and delegate; or how to keep GC and the static analyzer happy

Glen Low <[email protected]>
Newsgroups gmane.comp.macosx.devel
Message-ID <[email protected]>
Hi All

I have several cases of the following pattern in my code:

- (void)start
{
	[[Something alloc] initWithDelegate:self];
}

- (void)finishWithSomething:(Something*)something
{
	[something release];
}

The intent of course is that the Something object calls back the  
caller with finishWithSomething: and that does the cleanup. Several UI  
classes can work this way e.g. -[UIActionSheet  
initWithTitle:delegate:...].

However as I understand it, there are several problems with this  
pattern:

1.	The code is not GC friendly as between the end of start and the  
beginning of finishWithSomething, there are no references to the  
object, so it may be collected.
2.	The static analyzer in Xcode 3.2 doesn't like the construction,  
thinking that the object is leaking from the start method.

Of course I could keep a reference to the something object in a field  
of the calling object, but I was hoping to avoid that -- the calling  
object may invoke start multiple times before each finishWithSomething:.

Any suggestions?






Cheers, Glen Low


---
pixelglow software | simply brilliant stuff
www.pixelglow.com
aim: pixglen
twitter: pixelglow
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.