开发者

When to make object nil and when to call release

Today I see code in which user is releasing the object first and then he is making that object nil. like this

[objectA release];
objectA=nil;

I have read at many books that we should make the object nil while leaving the view and release the object late开发者_JAVA技巧r(in dealloc method of course this method is called after viewWillDisappear or viewDidDisappear).

Now i want to know that which approach is better?


Setting to nil and releasing are two distinct operations.

You release an object to relinquish ownership of it. This is covered in the standard Memory Managemange Guidelines. If you are not familiar with them, you should read them before doing any further iOS programming.

After releasing an object, you should set it to nil if you know that some other code may attempt to access that variable later. This is most common with instance variables.

For example, you may use an instance variable to store some sort of cache:

- (NSArray *)items
{
    if (!cachedItems) {
        cachedItems = [[self calculateItems] retain];
    }

    return cachedItems;
}

Later on you may need to clear this cache:

- (void)invalidateCache
{
    [cachedItems release];
    cachedItems = nil;
}

We need to set cachedItems to nil because our items method may attempt to use it later. If we do not set it to nil, messages send to the (now released) cache can lead to a crash.

So set a variable to nil after releasing it when it can potentially be access by other methods in your class at a later point in time.


I don't think is important set object to nil, but is good to do.

If you do :

  objectA = nil;
  [objectA release];

You have LOST the memory, and this is a memory leak. If you only do [objectA release], you will release the memory, but the objectA still point to the memory, so if you try to do things like:

  if (objectA==nil)

This will return FALSE, because objectA is NOT a nil. But because you do this almost in

  - (void)dealloc;

you don't need set it to nil in this function.


If the object is created locally:

I would go with the first approach, it's a common practice to release the object first then assign an nil.

I haven'nt read about your second approach in any book.

If the object is the part of class variable and have retain and @synthesize :

The below will do the both work (first release then assign nil through setter function) at once.

self.object = nil ;


First approach is the way to go for you..

[objectA release];
objectA=nil;

Also making object nil is a good practice (not if you are using it later) because after releasing the object, if I accidentally refers to it again your app will crash. But if you gave nil to the object, and you refer to it later it won't crash in objective C.(Situations similiar gave nullpointerException in languages like java)

ie

[objectA doneSomeTask];

wont crash,even if objectA is nil. As objective C silently ignores refering to nil.


What you have read in books does not work. If you set the object to nil you can not release it later because you can not excess the object then. You should to the first approach.


Best way:

[objectA release]; // sightly sightly faster since less function calls
objectA=nil;

Lazy way:

self.objectA=nil;

it will call:

(void)setObjectA:(ObjectAClass *)objectA
{
    [objectA release];         // <-- original value is released
    objectA = [objectA retain];// <-- set the point to nil and do nothing since nil
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜