views:

84

answers:

4

1) What is the reason for the use of retain?

For example, in a setter method:

- (void) setCount: (int) input {
    [intCount autorelease];
    intCount = [input retain];
}

2) the autorelease-Method: Is it deleting an old object or preparing the new one?

3) Why the retain-method is called at the input-object?

Would

intCount = input;

be wrong? And why?

A: 

To answer ur question specifically:

1). the use of retain is to declare the ownership of an object. In this case, intCount retains the ownership of input, in case the input got released somewhere else, u can still use the intCount.

2). the autorelease of intCount is to relinquish the ownership of the old value. That avoid the memory leak of the old value. If u don't release the old value, and u assign a new value to this pointer, the old object will always be there and never got released, which will cause the memory leak.

3). if u don't retain the input, and the parameter of input got released somewhere else. then if nowhere else retain this object, it will get freed. So u can't use intCount as well. That's why u need to retain it or copy it.

But i think if u do intCount = input; it should be fine. Because int is not an object, it's just a type. So I think the whole method is okay to be written like this:

- (void) setCount: (int) input {
    intCount = input;
}

But if its a pointer, u should not assign the new value to the old one directly.

JohnnySun
OK!Thank you!I should have asked the 'retain'-issue with an referenced object. Right?
Bambam2174
+4  A: 
  1. Retain is used to increment the retainCount of an object. NSObjects have a property called retainCount which maintains a count on the number of references that are currently held on an object. When the retainCount of an object reaches 0, the object can be released from memory. Effectively this prevents an object from being released from memory if it's still in use elsewhere.

  2. The autorelease method does not delete an old object and does not prepare the new object. It is effectively a pre-emptive call to release the object (autorelease is much more complicated than that and you should read up on it in the Memory Management Guide.)

  3. In your case intCount = input wouldn't be wrong because you're working with a primative. However if input were an object then you'd need to be calling retain on it. In fact you don't even need to be writing your own getters/setters for primatives (or objects), but instead should be using Declared Properties. In fact you're almost always better off using Declared Properties and if you do want to roll your own, get to know the pitfalls of doing so first.

Gavin Miller
@Jacob Thanks for catching the syntax error!
Gavin Miller
Bambam2174
Bambam2174
I come from a C# background as well before I started picking up Objective-C. The biggest help for me was reading the documentation and reading/using apple's example code (you can view these on a PC.) Apple's docs are *so* much better than .NET! I personally wouldn't waste my time learning C-programming if you're really wanting to learn objective-C. The two languages (and their concepts) are different enough that you won't gain a lot.
Gavin Miller
+2  A: 

The answers to your questions have been answered fairly well so let me just add that if you can use garbage collection you should. It makes everything so much easier. It isn't a panacea and you still should learn to use the retain/release mechanism but unless you are dealing in some high volume memory calls, creating and deleting lots of objects, then just use garbage collection.

It can be found under Project | Edit Project Settings | Build

Then just search for "garbage" and you'll see it.

If you are doing iOS development and cannot use garbage collection I apologize for giving unhelpful information but it still stands for non-iOS development.

FannyPack
I'm coding for iOS, but I think it's better to learn to do the memory management manually!TNX
Bambam2174
Absolutely. You should learn how to do all the things that can be automated for you like property accessors and memory management but once you understand them if there is no performance hit you should use the automated methods. It's the Agile approach, code smart not hard. iOS is scheduled to have garbage collection added but that doesn't help with older devices where users refuse to upgrade because of decreased performance. Sorry I wasn't more helpful.
FannyPack