In ObjC, how to describe the balance between alloc / copy / retain and auto- / release, in terms of location

As you know, calls to allocate / copy / save in Objective-C imply ownership and must be counterbalanced by an autorelease / release call. How do you briefly describe where this should happen? The word "short" is key. I can usually use intuition to guide me, but would like to see an explicit principle in case intuition fails and that can be used in discussions.

Properties -dealloc

keep things simple (the auto / release rule happens in setters too), but sometimes properties are not a viable option (for example, not everyone uses ObjC 2.0).

Sometimes a release has to be in one block. In other cases, the alloc / copy / function is stored in a single method that has a corresponding method in which the release should occur (for example, -init

and -dealloc

). It's this kind of method pairing (where a method can be coupled to itself) that seems to be the key, but how can this be put in words? Also, what cases does the concept of a method miss? It doesn't seem to cover where you drop properties as setters are self-paired and -dealloc

deallocate objects that are not allocated / copied / saved to -init

.

It looks like the object model is related to my difficulty. There seems to be no model element that I can attach pairing / releasing to. Methods transform objects from valid state to valid state and send messages to other objects. The only natural pairs I see are object creation / destruction and method input / output.

Background: This question was inspired by: " NSMutableDictionary is not added to NSMutableArray ". The responder to this question was freeing objects, but in a way that would cause a memory leak. Alloc / copy / keep calls were usually release-balanced, but in a way that could leak memory. The class was a delegate; some members were created in the delegate's ( -parser:didStartElement:...

) method and released in -dealloc

rather than the corresponding ( -parser:didEndElement:...

) method. In this case, properties seemed like a good solution, but the question still remained how to handle the release when properties are not involved.

+1


source to share


3 answers


Properties keep things simple (the auto / release rule happens in -dealloc and setters), but sometimes properties are not a viable option (for example, not everyone uses ObjC 2.0).

This is a misunderstanding of the history of properties. While properties are new, accessors have always been a key part of ObjC. Properties just made it easier to write accessories. If you always use accessors, and you should, then most of these questions go away.



Before we had properties, we used Xcode's built-in accessor constructor (in the Script> Code menu) or with helpful tools like the Accessorizer to make things easier (Accessorizer still makes property code simpler). Or we just typed in a lot of getters and setters by hand.

+3


source


The question is not where it should happen, when.

Free or auto-update an object if you created it with alloc, new, or -copy, or if you sent this message -retain.



Submit - select if you don't care if the object continues to exist. Send -autorelease if you want to return it from the method you are in, but you don't care what happens to it after that.

+3


source


I would not say that dealloc

is where you would call it autorelease

. And if your object, whatever it is, is related to the life of the class, it does not need to be maintained for retain

c dealloc

.

Here are my rules. You can do things to others.

  • I use release

    if the life object that I use is limited by the routine I am now. Thus, the object is created and routine. It is also preferred if I create many objects in a routine, for example in a loop, and I may need release

    each object before the next one is created in the loop.
  • If the object I created in the method needs to be returned, but I believe the use of the object will be temporary and limited by this runloop run, I use autorelease

    . Here I am trying to mimic many of Apple's handy routines. (Want a short string to be used for a short period of time? Here you go, don't worry about owning it and it will be disposed of appropriately.)
  • If I believe that the object should be stored semi-persistently (e.g. longer than this runloop run), I use create / new / copy in my method name so that the caller knows they are the owner of the object and will have the release

    object.
  • Any objects created by a class and stored as a property with retain

    (whether through a property declaration or not) I am release

    those in dealloc

    (or in viewDidUnload

    as needed).

Try not to let all this memory management overwhelm you. It's much easier than it sounds, and looking at a bunch of Apple samples and writing your own (and scary mistakes) will make you understand it better.

+2


source







All Articles