RE: memory management
RE: memory management
- Subject: RE: memory management
- From: "Jonathan E. Jackel" <email@hidden>
- Date: Tue, 13 Jan 2004 09:58:35 -0500
This isn't so much a memory management question as a question about what
pointer variables really point to, or maybe the differences among declaring
a variable, initializing it, and actually assigning values to it.
Sample 1 is perfectly fine and quite standard. You should not allocate
space for a new array. That is done for you by the object to which you send
the [... selectedCells] method. That object will create the array, if
necessary, and hand back a pointer to it. You are assigning that pointer to
myCells.
In Sample 2, you initialize a new, empty, autoreleased, immutable array.
That's virtually never an efficient thing to do. The array is empty and you
can't add anything to it. Its only possible use is as a placeholder
argument for a method that requires an NSArray as an argument and won't take
nil.
That's not what you do with it, though. You immediately move myCells so it
points to a different array -- the one returned by [...selectedCells]. The
array you initially created is now untethered from any way to refer to it,
and would cause a memory leak if you had not autoreleased it. Since you did
autorelease it, that array is released some time after myMethod finishes.
Memory management-wise, there's no problem. But there's also no point to
[[[NSArray alloc] init] autorelease].
Jonathan
>
>
Sample 1
>
>
>
>
-(IBAction)myMethod:(id)sender
>
>
{
>
>
NSArray* myCells = [[[UIManagerClass defaultManager]
>
controllerForKeyWindow] selectedCells];
>
>
>
>
for(i=0;i<[myCells count];i++) {
>
>
MyCellInfo* info = [myCells objectAtIndex:i];
>
>
If([info isRelevant])
>
>
//do something
>
>
}
>
>
}
>
>
>
>
>
>
Sample 2
>
>
>
>
-(IBAction)myMethod:(id)sender
>
>
{
>
>
NSArray* myCells = [[[NSArray alloc] init] autorelease];
>
>
myCells = [[[UIManagerClass defaultManager] controllerForKeyWindow]
>
selectedCells];
>
>
>
>
for(i=0;i<[myCells count];i++) {
>
>
MyCellInfo* info = [myCells objectAtIndex:i];
>
>
>
>
If([info isRelevant])
>
>
//do something
>
>
}
>
>
}
>
>
Thanks & Regards,
>
>
Ramakrishna,
>
_______________________________________________
>
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.
_______________________________________________
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.