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 > Help with returning instance variables and memory management

Help with returning instance variables and memory management
Thread Tools
Professional Poster
Join Date: Oct 2001
Status: Offline
Reply With Quote
Feb 23, 2002, 05:39 PM
 
Hello again, in my further study's of Objective-C, I have delved deep into memory management, and I am stuck on a few minor details that I would like to have cleared up. First of all, look at this code:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier> #import <font color = red>"Controller.h"</font>

@implementation Controller

- (IBAction)goButton id)sender
{
NSString *tmpString;
tmpString = [self returnString];
[textField setStringValue:tmpString];
}

- (NSString *)returnString
{
NSString *poo = [[NSString alloc] initWithString:<font color = orange>@"hello mada"</font>];
[poo autorelease]; <font color = brown>// where will poo be autoreleased? why would you even want</font>
<font color = brown>// to allocate memory to it? why not make it a temporary</font>
<font color = brown>// string?</font>
return poo;
}

@end
</font>[/code]

I have read up on a few examples, and this one seemed to be the most widely used. However, in returnString, why would anyone ever want to have a allocated string that they are just going to deallocate soon anyways? Why not just make it a temporary string (not sending it an alloc message) and return that? Anyways, so we autorelease it, poo is gone at the end of the method, right? And then we return it, would it be OK to do this?:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier> - (NSString *)returnString
{
NSString *poo = [[NSString alloc] initWithString:<font color = orange>@"hello mada"</font>];
return poo;
[poo release];
} </font>[/code]

finally, why isn't that you can't just send an object a dealloc message as soon as your done with it? And not have to worry about releaseing it and such.

Oh yes, sorry for the lengthy post, but one last thing please. Again, I am having trouble with understanding pointers.

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier> -(NSCalendarDate *)entryDate
{
return entryDate; <font color = brown>//entryDate was declared in the header file.</font>
} </font>[/code]

Alright, so here we're returning an NSCalendarDate, and what's going to accept that is probably going to be another NSCalendarDate instance. But that would be a pointer, so, we would have a pointer to a pointer. Kind of like this or something:

entryDate2 = *entryDate;

OK, heh, thanks in advance, and I am reading other materials, but none of them really explain things too well.

[ 02-23-2002: Message edited by: itistoday ]
     
Grizzled Veteran
Join Date: Apr 2001
Status: Offline
Reply With Quote
Feb 23, 2002, 11:10 PM
 
NSString *poo = [[NSString alloc] initWithString:@"hello mada"]; [poo autorelease]; // where will poo be autoreleased? why would you even want // to allocate memory to it? why not make it a temporary // string? return poo
You autorelease poo because it has to hang around long enough for the calling function to do something with it.

Poo will be released the next time the autorelease pool is walked. But, it will hang around long enough for the calling function to either make a copy of it, or retain it.

You can't make it a temporary string for the same reason you don't return local variables in C - they won't exist when the function returns.

finally, why isn't that you can't just send an object a dealloc message as soon as your done with it? And not have to worry about releaseing it and such.
If you haven't passed the object to any OS routine, and you're sure nothing is referencing it, you could just dealloc.

However, usually it's difficult to know whether something else has retained the object. Therefore, you just send it a release message and let it be dealloc'ed whenever there are no more references.

-(NSCalendarDate *)entryDate{ return entryDate; //entryDate was declared in the header file.}


Alright, so here we're returning an NSCalendarDate, and what's going to accept that is probably going to be another NSCalendarDate instance. But that would be a pointer, so, we would have a pointer to a pointer.
No, what you have is a second pointer being assigned the address of entryDate.

Wade

[ 02-24-2002: Message edited by: wadesworld ]

[ 02-24-2002: Message edited by: wadesworld ]
     
Mac Enthusiast
Join Date: Sep 2001
Location: McKinney, TX
Status: Offline
Reply With Quote
Feb 24, 2002, 01:49 PM
 
Wade summed it up pretty well. I thought I'd point out another misunderstanding you have regarding the following snippet:

- (NSString *)returnString
{
NSString *poo = [[NSString alloc] initWithString:@"hello mada"];
return poo;
[poo release];
}
The "[poo release]" statement would never be executed because of the "return poo" statement. A return statement causes execution of the current function to stop and control to be returned immediately to the caller. Any statements after the return are just wasted and possibly optimized out of the code when compiled (depending on the compiler options).

[ 02-24-2002: Message edited by: TheBum ]
     
Professional Poster
Join Date: Oct 2001
Status: Offline
Reply With Quote
Feb 25, 2002, 07:11 PM
 
Originally posted by TheBum:
<STRONG>Wade summed it up pretty well. I thought I'd point out another misunderstanding you have regarding the following snippet:



The "[poo release]" statement would never be executed because of the "return poo" statement. A return statement causes execution of the current function to stop and control to be returned immediately to the caller. Any statements after the return are just wasted and possibly optimized out of the code when compiled (depending on the compiler options).

[ 02-24-2002: Message edited by: TheBum ]</STRONG>
Doh! *wacks himself in the head* Forgot that about the return statement!
     
   
Thread Tools
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
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:09 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2