0

This may look like a dup of this, but I don't think that answers my question as it is about associated objects, not objects that were created by and whose only pointer resides within an object.

Let's say I had this example in MRC mode.

// In h file
@interface MyViewController : UIViewController {
   NSObject* myNsObject;
}

// In m file
-(void) viewDidLoad() {
   myNsObject = [[NSObject alloc] init];  // I'm never going to release myNsObject
}

I'm smart enough to release myViewController correctly. It's reference count goes to zero and it is de-allocated. But I never released myNsObject, so it had been hanging around with a reference count of 1. So would a release, and therefore de-alloc, automatically get done on myNsObject? Or would myNsObject get leaked in that case?

Community
  • 1
  • 1
Joe C
  • 2,728
  • 4
  • 30
  • 38

1 Answers1

2

The proper memory management here is to release myNsObject in the dealloc method of the view controller:

- (void)dealloc {
    [myNsObject release];

    [super dealloc];
}

If you create something then you are responsible for releasing it (under MRC).

Failure to do this results in memory leaks.

rmaddy
  • 314,917
  • 42
  • 532
  • 579