Understanding when to call hold on an object?

When should I use retain ? I understand that it increases the number of references to objects, so basically the next release on this object will not call dealloc on it. OK, so what? I read around that this is a kind of agreement that if you care about the object, you retain it. Are there any examples of this? It's true?

EDIT:

I am not looking for when the debugger tells you to do this or that. So, I looked through this one .

To put it in words, here is an example of using retain

  • in your code, do you ever call a method with a method that returns an object that does not belong
  • you are working with this object
  • then you want to release it => you cannot , because you are not the owner
  • Your solution should either use copy or retain . If the user is retain , then you will receive ownership of this object.
  • Then, to free this object, you either do 2 release (since the number of links is 1 + 1 when you save), or directly use dealloc on it

This is true? I don’t think so, because an object can have several owners. Therefore, for the last paragraph, a call to dealloc really “kill” the object; but with 2 release , you won’t be the owner, but the program that created it will still be there, so the objects are alive somewhere somewhere (leak? zombies?)

Please, I'm confused.

+6
memory-management iphone cocoa-touch
source share
4 answers

All your answers are in the "Memory Management Guide."

EDIT

After your editing, here is a little more detail:

in your code, somewhere you call a method with a method that returns an object that you do not have

Since you do not have this, you have no control over his life. It can be released while you still rely on the fact that it is a valid object.

you are working with this object

Not sure if it will exist.

then you want to free it => you cannot, because you do not own it

But why do you need to release it? You do not own the object, so you are not responsible for its memory management.

It sounds like you want to call release because you think you are managing memory that way and that retain is what you can call.

Here's how it should work:

  • You are calling a method that returns an object. If you did not receive this object by calling alloc , new , copy or mutableCopy , and then following the Memory Management Guide, you are not the owner of the object, so you are not responsible for managing this memory.
  • In most cases, you can assume that an object with auto-implementation was passed to you. This means that you do not control it all your life. To make sure that it is not freed before you finish with it, you call retain on the object. Now you are the owner of this object and are responsible for calling release on it in the future. Beginner’s mistake is now to worry about saving the account at the facility. Do not be. All that matters is that you are responsible for invoking release on it.
  • You are using an object, bearing in mind the general paradigm of memory management. For example, if you add this object to an NSArray , it will be saved in an array.
  • Once you have done what you need to do with the object, you call release on it. Again. Do not worry about saving the account of the object, or what other objects use this object. All that matters is that you balance your calls to retain with an equal number of calls to release .
+6
source share

Usually I just use hold explicitly when necessary, and the Xcode debugger tells me what just happened when such a situation arises. Usually for some reason (improper management of a part of the developer, or if some kind of release happens behind the scenes), you do something on the released object, you will crash. Just read the log in the console, see the Xcode debugger when debugging, and you usually find out which object is causing the problem.

+1
source share

Marcel, memory management is an important moment for developing applications for iOS. You should consider reading Apple's documentation on this, as suggested by the other guys.

I can add some information here to try to help you with your needs.

The memory management process with Obj-C is the basis of the count. This means that at any time when you “save” an object, the system improves the counter for this object. For example, if you create a button and save it, it will have the value 1. If you save it again, it will have the value 2. To completely destroy this object, you will need to release it twice. Each time an object is set to 0, it will be destroyed.

My personal opinion: if you want to have good control over the memory management of your application, it is good to explicitly save and destroy your objects, avoiding autoadvertising when possible. Again, this is my personal opinion. I like to know about the memory process inside my applications, so I prefer to take care of it exactly.

Confident that this is not the best way, each developer will prefer to use a different approach that manages memory.

Again: take the time to read the documentation that Abizem offered. Of course, you will have a better understanding.

0
source share

Check out the Objective-C Beginner's Guide for Basic Use

-one
source share

All Articles