Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > No need to release a newly allocated window?

No need to release a newly allocated window?
Thread Tools
kman42
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
May 10, 2002, 10:57 AM
 
My understanding of retain/release is that one should try to balance allocations/retains with releases in any block of code. Is this true for new windows as well? I'm having some trouble with the following bit of code:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
-(IBAction)doubleOnSearchResults id)sender {
NSArray *sortedReferenceDictValues;
NSArray *referenceDictValues;
ReferenceController *referenceWindow;
referenceDictValues=[NSArray arrayWithArray:[[currentSearch valueForKey:<font color = orange>@"referenceDictionary"</font>] allValues]];
sortedReferenceDictValues=[referenceDictValues sortedArrayUsingSelector:@selector(compareByYear ];

referenceWindow=[[ReferenceController alloc] initWithReference:[sortedReferenceDictValues objectAtIndex:[searchResults clickedRow]]];
NSLog(<font color = orange>@"A"</font>);

[referenceWindow showWindow:self];
NSLog(<font color = orange>@"b"</font>);

NSLog(<font color = orange>@"d"</font>);
<font color = brown>//[referenceWindow autorelease];</font>
}
</font>[/code]

That last commented line is what is giving me trouble. If I run the app the way it is written above it works fine. The new window opens and stays open. However, if I uncomment that last line (with release or autorelease), the window opens for a flash and then closes. But since I allocated the window in this block of code, don't I want to release it? Should I autorelease it in this code, but add a retain in the init of the new referenceWindow? Would this keep it around and let me balance out my retains/releases?

thanks,
kman
     
Ghoser777
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
May 10, 2002, 12:18 PM
 
Watch this:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>

-(IBAction)object
{
[[[GenericaObject alloc] init] autorelease];
}

</font>[/code]

That's essentially what you've got so far. So, you've got an Object with an initiral retain count of one. This object has it's retain count decremented by one (aka to 0) when we reach the close curly. If you were returning the object, it's good programming form to autorelease it before returning it. In this case, you're just creating an object and dislaying it to the screen, so you shouldn't autorelease it.

HTH,
F-bacher
     
lindberg
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status: Offline
Reply With Quote
May 11, 2002, 05:32 AM
 
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.
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 12:31 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,