Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should we release?

I often see when we release ab object we immediately set it to nil. I know that release and nil both free the old value associated with object but in case of release it leaves the object as a dangling pointer so we have to set it to nil.

So my question is if nil frees the old value of the object and set the object to nil why should not we only use nil why w should use release too.

Way1:

MyClass *obj = [[MyClass alloc] init];
[obj release];
obj = nil;

Way2:

MyClass *obj = [[MyClass alloc] init];
obj = nil;

What is the real difference in way1 and way2 if use way1 why don't use way2 only?

like image 868
Madhup Singh Yadav Avatar asked Nov 19 '09 05:11

Madhup Singh Yadav


People also ask

Why Release early and often?

What Is Release Early, Release Often? Release early, release often (RERO) is a development strategy focused on frequent, early releases. It is designed to create a feedback loop between testers, users, and developers. The goal is to promote faster, higher software quality that better meets user needs and expectations.

Why is release management important?

The primary objective of release management is to plan, schedule and control the deployment of IT services and updates into the live environments. Companies evolve and as they do, their needs change, so the IT environment needs to change too.

What does it mean to release an application?

A release is the distribution of the final version or the newest version of a software application. A software release may be public or private and generally signifies the unveiling of a new or upgraded version of the application.

What is a benefit of frequent product releases scrum?

Expected Benefitsit mitigates the well-known planning failure mode of discovering delays very late. it validates the product's fit to its market earlier. it provides earlier information about the quality and stability of the product. it allows for a quicker return on the economic investment into the product.


2 Answers

Setting a pointer to nil does not release the memory occupied by the former destination of that pointer. In plain english, assigning it to nil does not release it.

If your application is garbage collected, release is a no-op and can be left out. Otherwise, it's very, very necessary. Hence, Way 1 is always correct, and Way 2 is correct only under garbage collection.

Note: This answer does not apply to projects using Automatic Reference Counting. Under ARC, setting a pointer to nil does send a release to the object.

like image 188
BJ Homer Avatar answered Sep 24 '22 22:09

BJ Homer


It's as BJ said setting it to nil won't free up the memory, and in a non-gc collected environment would cause a memory leak. An alternative that'd possibly be valid as well would be

MyClass *obj = [[[MyClass alloc] init] autorelease];  

obj = nil;
like image 40
Bryan McLemore Avatar answered Sep 24 '22 22:09

Bryan McLemore