0

I'm wondering what the safest way is to release an object and Re-instantiate it again.

What I'm doing is this and it seems to work but I'm not sure if this is not creating a memory leak ?

- (void)reLoadCalendarObject
{
if (self.calViewController != nil)
    self.calViewController = nil;   //release instantiated object

CalViewController *tempCalViewController = [[CalViewController alloc]initWithDate:[NSDate dateWithTimeIntervalSinceNow:0.0f] observer:self];
self.calViewController = tempCalViewController;
[tempCalViewController release];
self.calViewController.dataSource = self.calendarDataSource;
}

self.calViewController property is declared as (nonatomic,retain)

Oysio
  • 3,486
  • 6
  • 45
  • 54
  • 1
    What makes you think it might cause a memory leak? ... this is fine – Paul.s Jun 09 '12 at 15:08
  • Because I thought you have to use the 'release' keyword to actually release an object from memory, just like this is happening in dealloc. – Oysio Jun 09 '12 at 15:15
  • Run it in Instruments to see if it's creating a leak. – skram Jun 09 '12 at 15:17
  • @Oysio if you `@synthesize` an `@property` with `retain` the compiler puts the correct memory management in the synthesized getters and setters. In this case the setter would correctly release the old value and set the ivar to `nil` – Paul.s Jun 09 '12 at 15:20
  • this might help: http://stackoverflow.com/questions/6091394/is-setting-a-property-to-nil-same-as-releasing-the-property – Rok Jarc Jun 09 '12 at 15:21

2 Answers2

1

When you use @property (nonatomic, retain) MyObject *myObject; and @synthesize myObject = _myObject; the compiler will generate the getters and setters for you. The setter will behave (not necessarily be implemented like this) something like this

1| - (void)setMyObject:(MyObject *)myObject;
2| {
3|     if (_myObject != myObject) {
4|         [_myObject release];
5|         _myObject = [myObject retain];
6|     }
7| }

At line 3 we do a check to make sure that the object you are setting is not the same object as the one passed in (we check the pointer as we are checking it is the exact same object)

At line 4 we know we have different objects so we need to release the old value

At line 5 we take a retain to the new object


As @thesaad pointed out your self.calViewController = nil; is superflous and so to in fact is the if (self.calViewController 1= nil) as it would be perfectly safe to send nil (or any object) if the property is already set.

In you example of passing nil this is what happens.

At line 3 we see that nil is not the same object as one that is previously set

At line 4 we release the old object

At line 5 we set the backing ivar to nil. Calling retain on nil is a no-op so that's fine as well

Paul.s
  • 38,494
  • 5
  • 70
  • 88
0

these lines are extra in your code.

if (self.calViewController != nil)
    self.calViewController = nil;   //release instantiated object

when you make a property then memory management goes to compiler end. and the way you are re instantiating it is perfact. so the code will be likse this

- (void)reLoadCalendarObject
{


CalViewController *tempCalViewController = [[CalViewController alloc]initWithDate:[NSDate dateWithTimeIntervalSinceNow:0.0f] observer:self];
self.calViewController = tempCalViewController;
[tempCalViewController release];
self.calViewController.dataSource = self.calendarDataSource;
}
Saad
  • 8,857
  • 2
  • 41
  • 51