pdiddy
pdiddy

Reputation: 6297

IPhone memory management

I am a bit lost with the memory management. I've read that you should release whenever you alloc. But when you get an instance without the alloc, you shouldnt release.

What about this situation, just need to know If I was coding correctly. I'm still new on iphone dev.

I have a class CustomerRepository it has a method

- (MSMutableArray *) GetAllCustomers() {

  MSMutableArray *customers = [[MSMutableArray alloc] init];

  Customer *cust1 = [[Customer alloc] init];
  cust1.name = @"John";

  Customer *cust2 = [[Customer alloc] init];
  cust2.name = @"Tony";

  [customers addOjbect:cust1];
  [customers addOjbect:cust2];

  [cust1 release];
  [cust2 release];

  return customers;

}

Then I have a UIViewController

- (void) LoadCustomers() {

      CustomerRepository *repo = [[CustomerRepository alloc] init];

      MSMutableArray *customers = [repo GetAllCustomers];          

      // Iterate through all customers and do something

      [repo release];

} 

So in this scenario ... the MSMutableArray will never be release? Where should it be release?

Upvotes: 0

Views: 134

Answers (4)

taskinoor
taskinoor

Reputation: 46037

If you alloc an object in a function that you need to return from the function then you can't release it inside the function. The correct way to do this is to autorelease the object.

MSMutableArray *customers = [[MSMutableArray alloc] init];

// ..... do work

return [customers autorelease];

This is the approach taken by the connivence constructors like

[NSString stringWithString:@"test"];

This method will return you an autoreleased string so that you don't need to release it.

And if you don't do this then you should name your function accordingly that the caller knows that it owns the returned object and thus needed to be released. These are conventions, not a rule imposed by the compiler or run-time environment but following convention is extremely important, specially when multiple people are involved in the project.

Upvotes: 6

kperryua
kperryua

Reputation: 10534

Whenever you create and return an object from a method or function, that object should be autoreleased. The exceptions are when the method starts with Create or New (or Alloc, obviously), or when the object is being cached within the method.

The other answers which suggest releasing it in LoadCustomers are incorrect, because GetAllCustomers does not imply a transfer of ownership like CreateCustomersArray or NewCustomersArray would. However, you can't release the object in GetAllCustomers either because then the object would be deallocated before returning it. The solution is autorelease.

Upvotes: 2

Jesse Naugher
Jesse Naugher

Reputation: 9820

it should be released in your view controller, LoadCustomers() since you are allocing it in the method you are calling, it is still owned by YOU.

Upvotes: 0

highlycaffeinated
highlycaffeinated

Reputation: 19867

The customers array should be released after you are done iterating it. You delegated the creation of the array to your repo object but your LoadCustomers method owns the array.

Another approach would be to have your CustomerRepository expose an allCustomers property. You could lazily initialize the array in your getter and then release the array when the CustomerRepository is released. That would keep your calls to alloc and release in the same object.

Upvotes: 0

Related Questions