S.J
S.J

Reputation: 3071

Need assistance regarding ARC concept

In ARC environment I have an object MyClass *mcObj = [[MyClass alloc]init] after using it should I leave it just like that or do this mcObj = nil; after using it.

This object can exist in any scope like global in viewcontroller or just inside a method -(void)aMethod; please explain both scenarios.

Upvotes: 0

Views: 192

Answers (3)

Midhun MP
Midhun MP

Reputation: 107221

You write like:

MyClass *mcObj = [[MyClass alloc]init];

So hoping it's a local variable, inside the function.

In both cases, when you write mcObj = nil; The object will be released and set to nil.

It's equivalent of:

[mcObject release];
mcObj = nil;

In non-arc mode.

Upvotes: 2

Abizern
Abizern

Reputation: 150705

The answer is - "it depends".

If you create it within a method and don't assign it to anything, then when it goes out of scope, it will be set to nil. (Actually, LLVM inserts the relevant release call for you).

If you pass the variable outside the function, then the local variable will still be set to nil when it goes out of scope, but if there is a strong reference to your object, it will not be released.

In general, you don't really have to think about this too much. If you want to keep an object around, then you keep a strong reference to it, otherwise you don't have to worry about retain release calls.

Upvotes: 6

Burhanuddin Sunelwala
Burhanuddin Sunelwala

Reputation: 5343

Just leave it like that!

ARC will take care of it! ARC will release/retain it wherever it finds best to do so.

Upvotes: 0

Related Questions