4

I'm pretty new to Objective-C and i have lots of troubles with memory management and still i understand a little. If i have an object, NSArray * myArray for example, and i do this

myArray = [[NSArray alloc] initWithObjects:obj1,obj2,obj3,nil];

then i'm doing something and i want myArray to contain new objects and then i init it again

[myArray initWithObjects:obj4,obj5,obj6, nil];

seems like it does what i need but is it correct grom the point of view of memory management?does it increase retain count? should i release it twice then?

Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161

3 Answers3

4

Don't do that!

In general, if you want to reset the objects or things inside an already existing Objective C object, create and use some kind of Setter method.

For your array, again do not do this! The "initWithObjects" method you cite is a convenience to initialize a immutable (non-changeable) array with the items an array will be populated with over it's entire lifetime.

For what you are trying to do, just use NSMutableArray. The documentation for it is listed below:

http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
4

It doesn't increase the retain count. The initial alloc sets the retain count to 1.

That said, you should never call an init method more than once on an object. While your example might work, at best it's probably leaking its internal storage and not releasing your obj1, obj2, and obj3. At worst, it could have inconsistent internal state and could lead to a crash.

If you're just doing this a couple times, you might as well create a new array:

NSArray* myArray = [NSArray arrayWithObjects:obj1, obj2, obj3, nil];
// Do some stuff...
myArray = [NSArray arrayWithObjects:obj4, obj5, obj6, nil];
// Do more stuff

If you're doing it a lot, e.g., in a loop, you should probably use an NSMutableArray:

NSMutableArray* myArray = [NSMutableArray array];
for (...) {
  [myArray removeAllObjects];
  [myArray addObject:obj4];
  [myArray addObject:obj5];
  [myArray addObject:obj6];
  // Do stuff
}
Tony
  • 3,470
  • 1
  • 18
  • 23
  • thanks a lot for your reply, buddy. could you please then explain me why should i use [[myArray alloc] init](and this question concerns not arrays only but all types of objects) if i can just do as you said - myArray = [NSArray arrayWithPbjects];? Why people use alloc if it still works without being allocated???? – Andrey Chernukha Nov 07 '11 at 17:46
  • [NSArray arrayWithObjects:] is just a shortcut for [[[NSArray alloc] initWithObjects:] autorelease]. – Tony Nov 07 '11 at 19:42
2

It's the old question. But I have found similar answer by this link:

http://clang.llvm.org/docs/AutomaticReferenceCounting.html#semantics-of-init

Little quote from the link:

It is undefined behavior for a program to cause two or more calls to init methods on the same object, except that each init method invocation may perform at most one delegate init call.

Serge Maslyakov
  • 1,410
  • 1
  • 17
  • 23