1

I know the instance variable in ARC are by default __strong. How can I release an instance variable when the containing class is still retained. In the following example v is __strong

and c is allocated when object of A is created some where and retained. I want to release the c instance variable. How to should I do that?, What should be in releaseC method that will release the c instance variable.

@interface A {
  Obj *c;
}

@implementation A {

 - (id)init {
   if((self = [super init])){
     c = [[Obj alloc] init];
   }
   return self;
 }

 - (void)releaseC {
  //what should be here?
 }

}

5 Answers 5

2
Obj *c; = [[Obj alloc] init];     

- (void)releaseC {
c = nil;
}

You cannot directly control when an object is released BUT you can indirectly cause it to happen. How? Remember what ARC does EXACTLY. Unlike human coding convention, ARC parses your code and inserts release statements AS SOON AS OBJECTS CAN be released. This frees up the memory for new allocations straight away, which is awesome/necessary. Meaning, setting an object to nil, or simply allowing a variable to go out of scope ... something that CAUSES A 0 RETAIN COUNT forces ARC to place its release calls there. It must ... because it would leak otherwise.

Sign up to request clarification or add additional context in comments.

1 Comment

Better provide link to source than copy & paste answers from SO. Because the answer is only partly true and can be improved later. (You can control ARC with autorelease pools, see comments under original answer)
2
- (void)releaseC {
    c = nil;
}

Comments

2

c = nil;

But some would argue it isn't productive from an efficiency standpoint. And while the release will be immediate in the sense it isn't any longer usable, the memory may not be freed immediately.

Comments

0

there is no need to release the variable in ARC. it done automatically

1 Comment

it will be done when instance of class A will be released. thats what arc does. puts the release calls in the dealloc for instance variables. but thats my question. i dont want the instance of A to be released and still want c to be released.
0

You are probably miss understanding what you want to do. I suppose you want to release the variable for memory issues. All you have to do is nil it. Instance variables are pointers to objects. As long as an object is pointed by something it is kept alive. As soon as you dont need something you can "stop pointing at it" and it will be released automagically.

As for the design, I am not so sure why you would have a public method that releases an instance variable. (I'm assuming its public because if it was not you would just nil it without actually having to write a method). If you do indeed intend to be able to release an instance variable from outside the class, I would simply make the Instance variable public and release it from anywhere setting it as nil.

1 Comment

the public method was just in example to explain the issue. i got it now. i just have to nil the instance variable and it will be released.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.