pointer or not?

Moderator
Posts: 3,570
Joined: 2003.06
Post: #16
Chron Wrote:theObject * anObject = [[theObject alloc] init];
creates a pointer called "anObject", that points to "theObject", which is contained in it's .m and .h files.
theObject * anObject = [[theObject alloc] init];
creates an object and puts a pointer to it in the pointer variable, anObject.

Quote:Doing:
theObject anObject = [[etc.]] would copy the entire "theObject" (or doesn't this work at all)?
I'm not sure I understand the question, but you will hardly ever see an object being copied anywhere since everything just passes around its pointer after initial creation. That's the whole point of pointers -- not having to make copies of large data constructs.

backslash Wrote:object1 = [[NSObject init] alloc]; //this creates a NSObject and puts its address in object1
object2 = [[NSObject init] alloc]; //this does the same
[object2 release]; //it is a good idea to do this before the line below to avoid a memory leak
object2 = object1; //this makes the variable object2 point to the same object as object1
[object2 retain]; //it is a good idea to do this after copying pointers to objects
[/CODE]

The retain/release thing is a whole 'nuther story. It actually should be:
Code:
object1 = [[NSObject alloc] init]; //this creates a NSObject and puts its address in object1 *and* retains it
object2 = [[NSObject alloc] init]; //this does the same
[object2 release]; //it is a *must* to do this before the line below to avoid a memory leak
object2 = object1; //this makes the variable object2 point to the same object as object1
[object2 retain]; // this is not needed because alloc already retained it for you above, but if you do retain it here, remember that you'll need to release it twice to get rid of it

// further:
object3 = [NSObject objectWithBlah:blah]; // creates an NSObject, puts its address in object3 and is autoreleased, so you need to retain it yourself
[object3 retain]; // it's safe now
OR
[myArray addObject:object3]; // myArray will retain object3 so you can let it go without retaining
OR
[myDictionary setObject:object3 ForKey:@"blah"]; // will also retain object3 and you can let it go without retaining
The pattern you see with object3's creation is important to recognize as a `convenience' creator. You can tell that it's a convenience creator because there's no alloc init, which is your big hint that this object is given to you as autoreleased, which means it'll be released soon so you need to retain it if you want to use it yourself.

It is also important to remember that other objects like NSArrays and NSDictionaries retain objects, so if you know they're retaining it then you don't have to retain it yourself (although you can if you want to, as long as you remember to release when you're done).

There's more to it than this of course, but these are just quick tips to remember while we're on the subject. You'll be home free once you grasp the whole memory address thing (pointers) and retain/release.
Quote this message in a reply
Apprentice
Posts: 6
Joined: 2007.08
Post: #17
A few more things:
Quote:object3 = [NSObject objectWithBlah:blah]; // creates an NSObject, puts its address in object3 and is autoreleased
What is the objectWithBlah for? It's a method I guess, with argument blah, but can't you just do
Quote:object 3 = [NSObject]
?
And as for the memory leak, I guess this threat is still there with the use of an Array or Dictionary?

These are my final questions for the time being, I greatly appreciate the overwhelming amount of help from such a relatively small community, but I don't have the feeling that I'm at a level to fully understand what you tell me.
For that reason, please don't take the endeavour to explain new things, I'm currently reading "Becoming an XCoder" and "Learning Cocoa with Objective-C", which I think are really good, and I'll post as soon as I feel there is something I can't learn "from the books".
Thanks again for your help!
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #18
Chrono Wrote:What is the objectWithBlah for? It's a method I guess, with argument blah, but can't you just do ?
object 3 = [NSObject]
No. The convenience creators I was trying to describe do the newObject = [[Object alloc] init] for you, but then go the extra step and do [newObject autorelease] before returning it to you so it sticks around long enough for you to do something with it -- either retain it yourself or pass it off to another object which you know will retain it for you.

Quote:And as for the memory leak, I guess this threat is still there with the use of an Array or Dictionary?
Not if you passed it off to them. You are still responsible for retaining the array or dictionary though. You can retain the objects you send to dictionaries and arrays and other objects too, but a lot of times it is convenient to say you are simply passing ownership of those objects off to the dictionary or array (or some other class for that matter). You still have ownership of the dictionary or array so it won't release any of those objects until you release it first. The book you are reading should explain that pretty well.

The only thing I was trying to point out is how to recognize when you need to retain an object and when it's already been retained for you. Remember the pattern:

NSObject *object = [[NSObject alloc] init];

means that a new NSObject is created and it's address is copied into the *object pointer, and is already retained for you. Remember that it was created with alloc and init, which is how you know it's already been retained.

Whereas this pattern:

NSDictionary *dictionary = [NSDictionary dictionaryWithDictionary:otherDictionary];

means that dictionary was created by NSDictionary and is autoreleased (not going to be around for long), so you need to remember to retain it or pass it off to another object which will retain it for you. Note the distinct lack of the alloc and init when calling NSDictionary dictionaryWithDictionary.

I'm pointing this out specifically because I haven't seen a book yet which YELLS IT OUT. If you can remember this tip, it will save you much pain and agony as you go along.

Quote:These are my final questions for the time being, I greatly appreciate the overwhelming amount of help from such a relatively small community, but I don't have the feeling that I'm at a level to fully understand what you tell me. For that reason, please don't take the endeavour to explain new things...
Hehehe...

Don't worry, you'll get it. The truth is that this isn't easy to learn, and it isn't always easy to explain either.Wink
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  memory pointer tricks? Toontingy 2 4,010 Mar 31, 2009 02:35 AM
Last Post: Ingemar
  pointer cleanup questions kendric 7 4,264 Mar 29, 2009 07:48 PM
Last Post: kendric
  My AnimationManager didn't like being a pointer. milkfilk 6 3,714 Mar 24, 2007 10:03 PM
Last Post: unknown
  Pointer-related woes (C/C++) sealfin 2 2,520 Jan 18, 2006 01:22 PM
Last Post: sealfin
  Hide Mouse Pointer JonTrainer 9 6,974 Nov 10, 2005 10:46 AM
Last Post: Jordan