2

I want to release UIViewController object in iOS 5. Prior iOS versions (>5.0) we write always code or maintaining code with memory free leaks following,

if(myViewControllerObject != nil){
     [myViewControllerObject.view removeFromSuperView];
     [myViewControllerObject release];
     myViewControllerObject = nil;
} 
 myViewControllerObject = [[MyViewControllerObject alloc] initWithNibName:@"MyViewControllerObject" bundle:nil];
  [self.view addSubview: myViewControllerObject.view];

But in iOS 5 we can not use release method for release UIviewController object then what we have to do in iOS 5 to maintaining extra object allocation and leaks? What will be the best method to implement this hierarchy?

Thanks.

Sachin D
  • 1,370
  • 7
  • 29
  • 42
Tirth
  • 7,801
  • 9
  • 55
  • 88

3 Answers3

0

If you're using ARC (automatic retain/reference counting) then you don't need to use release at all. The compiler now counts all your retains and releases them automatically. Its not really garbage collection, but the compiler just works out where the object is not being used any more and adds the release automatically.

So in answer to your question, just leave it, the compiler will sort it out.

Thomas Clayson
  • 29,657
  • 26
  • 147
  • 224
  • Thanks Thomas providing me brief explanation....nw i understand ur answer also acceptable :) – Tirth Mar 14 '12 at 12:03
  • 1
    but to force arc to release it, use `myViewControllerObject = nil;`. – Simon Mar 14 '12 at 18:32
  • If we are not release Objects isn't it a Chance to Crash ? – Vineesh TP Feb 17 '13 at 08:46
  • @VineeshTP if you are using ARC then the compiler will automatically add the release and retain calls for you. So, no, your app won't crash. – Thomas Clayson Feb 18 '13 at 09:07
  • @Thomas Clayson: Thank you., I studied and read that you are said. But, Previous week I had attand an inteview I had a machiene test I used ARC (select Option at the time of create project) But, The company project leader said to me. It will not release whole Object After sometime It will crash. I coudn't answer for that question that is why I asked this . – Vineesh TP Feb 20 '13 at 05:14
  • 1
    I don't know your whole situation and the particulars. But if what you're saying is correct then your project leader is wrong. ARC will add retain and release calls for all objective c objects (NSObject subclasses) automatically. You can assign to nil (`self.object = nil;`) to force ARC to release, but this isn't NECESSARY and your app won't crash. There are still ways to get your app to crash from an `EXC_BAD_ACCESS` (premature release) but these are edge cases and you've probably done something wrong. – Thomas Clayson Feb 21 '13 at 12:22
0

Your code will not change to iOS 5 unless you activate Automatic Reference Counting for your project. Then you will not be able to use release, dealloc and so on, because the system handles memory management.

To learn more about ARC, see this question: How does the new automatic reference counting mechanism work?

Community
  • 1
  • 1
Sebastian Wramba
  • 10,087
  • 8
  • 41
  • 58
  • If we are not release Object isn't it a Chance for Crash ? – Vineesh TP Feb 17 '13 at 07:17
  • No, not releasing an object is a chance for unnecessary memory consumption and a memory leak. But as I said, using ARC will prevent this – releases are done automatically (more or less), this way you don't have to write `release` statements yourself. – Sebastian Wramba Feb 20 '13 at 12:10
0

You should not call

[myViewControllerObject.view removeFromSuperView];

The UIViewController class (base class of myViewControllerObject) can automatically set this property to nil during low-memory conditions and also when the view controller itself is finally released.

So this should be fine

[myViewControllerObject release];
 myViewControllerObject = nil;

Update: For ARC refer below answer

msk
  • 8,885
  • 6
  • 41
  • 72