English 中文(简体)
Suggest the best way of initialization of array ( or other objects )
原标题:

I am a bit confused in the following two ways of initialisations.....

Way 1:

- (void) myMethod{

    NSArray *myArray = [[NSArray alloc] initWithObjects:obj1,obj1,nil];
    [self setClassArray:myArray];
    [myArray release];
}

Way 2:

- (void) myMethod{

NSArray *myArray = [NSArray arrayWithObjects:obj1,obj2,nil];
[self setClassArray:myArray];
}

In way 1, I have used a alloc init method which is a instance method and as I have used an alloc statement I have to release the array myself.

In way 2, I have used a static method to initialze the array and as there is no alloc statement used I dont need to release the memory the system will take care of that.

Way 1, is time consuming and can to lead to memory leaks if not taken care

Way 2, is faster in writing and you dont need to take care of memory leaks

But , still i have seen the way1 used in standard source codes more often than the way2. I have no idea why people do this or if I am wrong at some place.

Answers and comments are oppenly invited. Please suggest the best programming practice.

最佳回答

Your second example uses a convenience constructor, which returns an autoreleased object. The question, then, is whether it s better to use autorelease or alloc/release. mmalc s answer on this StackOverflow thread explains the drawbacks of autoreleasing objects. (Basically, use alloc/release whenever possible.)

Also (this may be stating the obvious), some classes might not have convenience constructors, so when working with these you d have to use alloc/release.

问题回答

as i know,

[NSArray arrayWithObjects:obj1,obj2,nil];

returns an autoreleased object, smth like

[[[NSArray alloc] initWithObjects:obj1,obj1,nil] autorelease];

and I prefer not to manage memory with autorelease pool. maybe it s just a prejudice))

When using method 2 (autorelease) the object is released when the operating system feels it has no references. But when using the manual release, as you typed in your code, you can release directly, or whenever you need to.

I usually prefer the second way, and if I recall correctly, this is also what the Apple docs reccommend (use autorelease where possible).

In Way 1 the memory gets released faster, so if you have a method that gets called in a loop or recursively, using autorelease may accumulate much memory, whereas alloc/retain will keep your memory footprint low.

On the other hand I assume that using autorelease is more efficient, because the autorelease pool can release a big chunk of memory at once, instead of small chunks again and again. Tough I may be wrong here.





相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签