Setting an object to nil after release - TT_RELEASE_SAFELY

I started learning Three20, and I have a simple question about TT_RELEASE_SAFELY So far, I like to write code this way:

UILabel *lab = [[UILabel alloc] initWithFrame:rect];
[self.view addSubview:lab];
[lab release];

Here I think the main pool is responsible for freeing memory lab.

Now I found TT_RELEASE_SAFELYone that is defined as follows:

#define TT_RELEASE_SAFELY(__POINTER) { [__POINTER release]; __POINTER = nil; }

As you can see, after release, it sets the object to nil.

I would like to know the difference between the two methods and which method is better.

Thank.

+3
source share
3 answers

Sending a message to zero is valid in Objective-C. A message is not sent to the freed object.

Sending a message to a freed object:

id obj = [[MyClass alloc] init];
[obj release];
[obj doSomething]; // Crash!

:

id obj = [[MyClass alloc] init];
[obj release], obj = nil;
[obj doSomething]; // Valid

nil , , , , - . Sedate Alien:

[controlCenter dealloc];
...
float timeLeft = [controlCenter timeToWaitBeforeBombDetonation];

, controlCenter . .

[controlCenter dealloc], controlCenter = nil;
...
float timeLeft = [controlCenter timeToWaitBeforeBombDetonation];

0.0 timeLeft, , , , controlCenter .


, Objective-C, , , , , , . , , , Ada.

+6

, " " - .

, , nil, . , NSZombieEnabled.

+1

The only difference is that TT_RELEASE_SAFELY sets the pointer to zero after release, so the link will not be used after release. The template is good, and the macro TT_RELEASE_SAFELY simplifies the implementation.

0
source

All Articles