You need to [auto]release it eventually if you don't want to leak that memory. However, as you noticed, autorelease gets freed at the end of the current event -- much too soon for you. You need to keep a reference to this window around somehow, and release it when you know you're done with it.
One option would be to have an ivar in the class you're using there, and use it as a "shared" object. i.e.:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
- (void)dealloc
{
...
[referenceWindow release];
[super dealloc];
}
-(IBAction)doubleOnSearchResults
id)sender {
NSArray *sortedReferenceDictValues;
NSArray *referenceDictValues;
referenceDictValues=[NSArray arrayWithArray:[[currentSearch valueForKey:<font color = orange>@"referenceDictionary"</font>] allValues]];
sortedReferenceDictValues=[referenceDictValues sortedArrayUsingSelector:@selector(compareByYear
];
if (referenceWindow == nil) {
referenceWindow=[[ReferenceController alloc] init];
}
[referenceWindow setReference:[sortedReferenceDictValues objectAtIndex:[searchResults clickedRow]]];
[referenceWindow showWindow:self];
}
</font>[/code]
Obviously, you can only have one of these reference windows visible at a time with this method.
If you want multiple windows, you could add them to an NSArray you keep as an ivar, and remove it from the array when you get the windowWillClose: delegate method from the corresponding window (you probably want to make sure it's autoreleased instead of released in this case to make sure the object survives to the end of the current event).
If the class in question is an NSDocument and ReferenceController is a subclass of NSWindowController, you could simply add it to the windowControllers array of the document (addWindowController
and the releasing details will be taken care of for you.