PDA

View Full Version : class initialization methods: release & alloc




mdeh
Feb 1, 2009, 01:13 PM
May I ask 2 quick questions.
Given: NSNumber *myNumber;

myNumber = [ NSNumber numberWithLong: 0xFFAA];

Question 1:

Why no alloc?

Question 2;

there is no release method. Why is this. Thanks in advance.



kainjow
Feb 1, 2009, 01:35 PM
It's an autoreleased object. I'd suggest reading through Apple's Memory Management Programming Guide for Cocoa (http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html).

lee1210
Feb 1, 2009, 01:36 PM
This is considered a "factory" method. It:
Allocates a new object
Initializes the object with the argument given
Calls autorelease on the object

You don't want to call release, because the autorelease pool knows to decrement the retain count the next time it(the pool) is released. If you need to keep the object around after the next time the autorelease pool is released, you can retain it, and run release when you are done.

Also... There's only 2 bytes there, and a long is 8 bytes long... why not pass in your long with something like:
73492233442L
?

-Lee

mdeh
Feb 1, 2009, 01:42 PM
This is considered a "factory" method. It:
Allocates a new object
Initializes the object with the argument given
Calls autorelease on the object

You don't want to call release, because the autorelease pool knows to decrement the retain count the next time it(the pool) is released. If you need to keep the object around after the next time the autorelease pool is released, you can retain it, and run release when you are done.

Also... There's only 2 bytes there, and a long is 8 bytes long... why not pass in your long with something like:
73492233442L
?

-Lee

Lee...thanks as always.
You have put it in perspective for me..which is what I wanted. Kochan is not yet deeply into the autorelease...but I am sure we will get there.
So, in general, can we say that , for all the datatypes and objects for which there are factory methods,


Allocates a new object
Initializes the object with the argument given
Calls autorelease on the object


so, I ( the programmer) **normally** ( with the exception you mentioned above) do not worry about alloc/release.

Thanks.

mdeh
Feb 1, 2009, 01:44 PM
It's an autoreleased object. I'd suggest reading through Apple's Memory Management Programming Guide for Cocoa (http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html).

Thanks for your input. I am slowly getting through Kochan's book, so not quite there yet. Your help is appreciated.

lee1210
Feb 1, 2009, 02:01 PM
Lee...thanks as always.
You have put it in perspective for me..which is what I wanted. Kochan is not yet deeply into the autorelease...but I am sure we will get there.
So, in general, can we say that , for all the datatypes and objects for which there are factory methods,



so, I ( the programmer) **normally** ( with the exception you mentioned above) do not worry about alloc/release.

Thanks.

Not every class will have factory methods, but some certainly do. Generally if something returns an Object pointer, and it doesn't contain the words init or copy, you'll be getting an autoreleased object. I assume this will be covered in greater depth later in the book.

As for other datatypes (non-objects, so primitives or structs), none of this applies(edit). I don't know if there are a lot of cocoa methods that return primitive pointers. Most of the time I would expect a value being returned, and you'll just assign it to the same type of variable in your functions. Most of the time primitives will just be local variables in a method, etc. You can deal with C memory management to malloc space for primitives, but this is normally for large arrays not single values. None of the retain/release/autorelease/alloc/init/etc. things that are normally a part of cocoa memory management come into play.

-Lee

mdeh
Feb 1, 2009, 02:08 PM
Not every class will have factory methods, but some certainly do. Generally if something returns an Object pointer, and it doesn't contain the words init or copy, you'll be getting an autoreleased object. I assume this will be covered in greater depth later in the book.

As for other datatypes (non-objects, so primitives or structs), none of this replies. I don't know if there are a lot of cocoa methods that return primitive pointers. Most of the time I would expect a value being returned, and you'll just assign it to the same type of variable in your functions. Most of the time primitives will just be local variables in a method, etc. You can deal with C memory management to malloc space for primitives, but this is normally for large arrays not single values. None of the retain/release/autorelease/alloc/init/etc. things that are normally a part of cocoa memory management come into play.

-Lee

thank you again.

skochan
Feb 3, 2009, 07:12 PM
Generally if something returns an Object pointer, and it doesn't contain the words init or copy, you'll be getting an autoreleased object. I assume this will be covered in greater depth later in the book.



Yes it is (there's an entire chapter devoted to it). I would add the word "alloc" to your list.

Cheers,

Steve Kochan

Catfish_Man
Feb 3, 2009, 08:54 PM
Also 'new', although not much code uses that these days.