Re: memory management
Re: memory management
- Subject: Re: memory management
- From: Chris Hanson <email@hidden>
- Date: Tue, 13 Jan 2004 11:53:07 -0600
On Jan 13, 2004, at 8:03 AM, Ramakrishna Kondapalli wrote:
Note that selectedCells method returns an NSArray* type and is being
released by its own controller class at some point of time in future.
You mean that -selectedCells returns an NSArray. It returns an object,
not just a type. There's an important distinction: While in C++ people
often program to the compile-time behavior of the type system, in
Objective-C you program to the runtime behavior of objects.
Which of these 2 ways of using myCells array is better and why?
Sample 2 shows a deep misunderstanding of Cocoa, Objective-C, and C.
It should be very clear that it's incorrect.
NSArray* myCells = [[[NSArray alloc] init] autorelease];
This allocates and initializes an empty NSArray and adds it to the
current autorelease pool. This NSArray is referenced by myCells.
myCells = [[[UIManagerClass defaultManager] controllerForKeyWindow]
selectedCells];
This replaces the NSArray currently referenced by myCells with a
different NSArray.
The previous NSArray referenced by myCells does not leak because it was
placed in an autorelease pool. You've just done some extra work.
The reason is that you're not copying the result of -selectedCells into
the array referenced by myCells. You're making myCells reference the
result of -selectedCells. This is standard C pointer behavior;
anything else would imply that there's more going on "behind the
scenes" than there really is.
for(i=0;i<[myCells count];i++) {
The usual idiom is to use an NSEnumerator rather than a for loop to
iterate over the contents of an NSArray.
-- Chris
--
Chris Hanson <email@hidden>
bDistributed.com, Inc.
Outsourcing Vendor Evaluation
Custom Mac OS X Development
Cocoa Developer Training
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.