Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?
 }

}
share|improve this question
add comment

5 Answers

up vote 2 down vote accepted
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.

share|improve this answer
add comment
- (void)releaseC {
    c = nil;
}
share|improve this answer
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
 
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. –  g.revolution Jun 12 '13 at 4:48
add comment

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.

share|improve this answer
 
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. –  g.revolution Jun 13 '13 at 2:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.