Amazon.com Widgets confusion about memory management
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 18, 2013, 12:54:19 AM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Help!!!
| | |-+  confusion about memory management
Pages: [1]   Go Down
Print
Author Topic: confusion about memory management  (Read 482 times)
gwithers
Newbie
*
Posts: 1






« on: November 18, 2009, 10:52:12 PM »

Hi,
I've been working my way through the book, and everything has been making sense, but I've just reached Chapter 17 - memory management - and I'm confused. I had some questions about memory management as I worked through the text, and I thought Ch 17 would clarify all, but so far it hasn't.

Here are my questions, hopefully someone can help...

1. In all of the sample programs, we've been creating an NSAutoreleasePool as our first line of code within main(). How do the objects know to use that pool? Is it because it is the only pool that exists? What if we created two pools? Which one would they use?

2. Sometimes the books uses class methods that allocate the memory for you, and other times it doesn't. How do we choose which approach to take? What are the decision criteria? For example, the NSString class method "stringWithString:" allocates the memory for you (?). I think I understand why in program 15.8 we modify the code on page 349 to allocate the memory ourselves, using the "alloc" class method - because we are initializing an instance variable, and we want to 'own' that variable. Is that right? What about in other cases? 

3. In program 15.4, when I look at it now, after reading Ch 17, I seem to think that we are leaking memory all over the place - or maybe not.  I have a bunch of questions about this example...
Code: (Objective-C)
NSString *str1 = @"This is string A"
I think I'm OK with that.

Code: (Objective-C)
NSString *res;

This is creating a pointer to a string, but doing no allocation or initializing, correct?

Code: (Objective-C)
res = [str1 substringToIndex: 3];
I believe that this is creating a new string, and making res point to it. If so, does this mean that the substringToIndex: method is allocating and initializing for us? (because we certainly didn't allocate it ourselves)

Code: (Objective-C)
res = [str1 substringFromIndex: 5];
I believe that this is creating a new string, and re-pointing res to it.
If that is correct, what happens to the string where res was previously pointing? (from the call to substringToIndex:) Is this a memory leak? Or, is NSString class responsible for deallocating its memory?

Answers greatly appreciated.

Thanks,
Gord.

Logged
rgronlie
Global Moderator
Full Member
*****
Posts: 212







« Reply #1 on: November 19, 2009, 03:41:39 AM »

Gord,

Quote
1. In all of the sample programs, we've been creating an NSAutoreleasePool as our first line of code within main(). How do the objects know to use that pool? Is it because it is the only pool that exists? What if we created two pools? Which one would they use?

You can have more than one AutoRelease pool. Autoreleased objects will use the top pool. I suggest you read Apple's documentation for NSAutoReleasePool for more information.

Quote
2. Sometimes the books uses class methods that allocate the memory for you, and other times it doesn't. How do we choose which approach to take? What are the decision criteria? For example, the NSString class method "stringWithString:" allocates the memory for you (?). I think I understand why in program 15.8 we modify the code on page 349 to allocate the memory ourselves, using the "alloc" class method - because we are initializing an instance variable, and we want to 'own' that variable. Is that right? What about in other cases? 

The Rule

Quote
3. In program 15.4, when I look at it now, after reading Ch 17, I seem to think that we are leaking memory all over the place - or maybe not.  I have a bunch of questions about this example...

Quote
Code: (Objective-C)
NSString *res;
This is creating a pointer to a string, but doing no allocation or initializing, correct?
Correct (declaring a pointer to an NSString).

Quote
Code: (Objective-C)
res = [str1 substringToIndex: 3];
I believe that this is creating a new string, and making res point to it. If so, does this mean that the substringToIndex: method is allocating and initializing for us? (because we certainly didn't allocate it ourselves)
Yes, substringToIndex does the alloc/init

Quote
Code: (Objective-C)
res = [str1 substringFromIndex: 5];
 
I believe that this is creating a new string, and re-pointing res to it.
If that is correct, what happens to the string where res was previously pointing? (from the call to substringToIndex:) Is this a memory leak? Or, is NSString class responsible for deallocating its memory?
Correct. Refer back to the rule. The object res was previously pointing to is in the AutoRelease pool... so no leaking.

Ryan
Logged

Sanity: Minds are like parachutes. Just because you've lost yours doesn't mean you can borrow mine.
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.