PDA

View Full Version : Why doesn't this object not need memory allocated?




chrono1081
Jul 25, 2009, 03:24 AM
EDIT: Oops! This thread should read: "Why doesn't this object need memory allocated?" :D


Hi guys!

Not sure where my other thread went but I can't find it so I'm reposting the same question:

I'm coming from C++ and learning Objective-C and was working through some exercises and this one has me stumped on why its working.


#import <Foundation/Foundation.h>
#import "Complex.h"

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

Complex *c1 = [[Complex alloc] init];
Complex *c2 = [[Complex alloc] init];
Complex *compResult; //<---Why doesnt ths need initialized like the above two objects?

[c1 setReal: 18.0 andImaginary: 2.5];
[c2 setReal: -5.0 andImaginary: 3.2];

//Add and print two complex numbers

[c1 print]; NSLog(@" +"); [c2 print];
NSLog(@"------");
compResult = [c1 add: c2];
[compResult print];
NSLog(@"\n");

[c1 release];
[c2 release];
[compResult release];



[pool drain];
return 0;
}


How come the last object does not need initialized the same as the others? I can initialize it and it works but if I do not initialize one of the first two objects (c1 and c2) the program wont work. I don't understand how this works seeing as they are all objects of the same type.

Could anyone explain this to me please?



ChOas
Jul 25, 2009, 04:06 AM
EDIT: Oops! This thread should read: "Why
How come the last object does not need initialized the same as the others? I can initialize it and it works but if I do not initialize one of the first two objects (c1 and c2) the program wont work. I don't understand how this works seeing as they are all objects of the same type.

Could anyone explain this to me please?

compResult is just a pointer. The reason you do not alloc memory to it is that you just point it to the place where the memory for it was allocated:

compResult = [c1 add: c2];

add: allocs and inits the memory and then you point compResult to that space.

Thats also why you release compResult later, it releases the memory allocated by add:.

If you alloc and init it like you describe you will have a leak:


compResult points to compResultInitedMemory.
[stuff]
compResult = [c1 add: c2];
compResult now points to the memory alloced by add:


... And now nobody knows where the original compResultInitedMemory is because nothing is pointing to it anymore. So it can't be released anymore.

chrono1081
Jul 25, 2009, 04:49 AM
Dolp! Totally missed the fact it was just a pointer haha. Thank you for the help :)

larkost
Jul 25, 2009, 09:30 PM
The other thing that you are missing is that when the [Complex add:] method returns the new Complex object, it has auto-allocated the memory for you, and dropped it into the default NSAutoreleasePool for you. So when that pool gets drained the new Complex objects gets released, and since it only has a count of 1, it gets wiped away and the memory for it released.

mdeh
Jul 25, 2009, 10:31 PM
The other thing that you are missing is that when the [Complex add:] method returns the new Complex object, it has auto-allocated the memory for you, and dropped it into the default NSAutoreleasePool for you. So when that pool gets drained the new Complex objects gets released, and since it only has a count of 1, it gets wiped away and the memory for it released.

Hi larkost,
I think this is one of the examples ( or could be ) from Steve Kochan's book. IIRC, the reason that "compResult" is released, is that the return value is *not* autoreleased, as I do not think this has been covered. Which may have been the source of confusion to the OP.
But, I could be totally incorrect.

chrono1081
Jul 26, 2009, 04:53 AM
Yep its from Steve Kochans book.

This book is a little confusing because it does use things without explaining them first, only to explain them later. Coming from C++ I can figure out pretty much anything but there are some differences that end up catching up with me. Memory management is definitely one of them.

mdeh
Jul 26, 2009, 06:08 AM
Yep its from Steve Kochans book.

This book is a little confusing because it does use things without explaining them first, only to explain them later. Coming from C++ I can figure out pretty much anything but there are some differences that end up catching up with me. Memory management is definitely one of them.

Hi chrono,
Steve Kochan actually has a web site that supports the book. I think the **single** commonest question is about this issue :)

jared_kipe
Jul 26, 2009, 12:20 PM
This is what people mean when they say convenience methods.
The method returns a new object, HOPEFULLY autoreleased so you can retain or copy it if you want to keep it. And it will go away on its own in the future if you do not.

One of my favorites is...
int i=25;
NSString *newString = [NSString stringWithFormat: @"my i = %d", i ];

Though in this context you'd have to retain it if you wanted to keep it.

Usually its particularly useful if you have instance variables that are using @property (retain)

chrono1081
Jul 26, 2009, 12:54 PM
Hi chrono,
Steve Kochan actually has a web site that supports the book. I think the **single** commonest question is about this issue :)

I saw it but my work proxy thinks its "Provocative Attire" and I live where I work so I can't access it : /

Thank you for the suggestion though :)