Re: Memory management question
Re: Memory management question
- Subject: Re: Memory management question
- From: Thomas Deniau <email@hidden>
- Date: Mon, 29 Apr 2002 19:09:18 +0200
Le 29/04/02 18:49, Philippe Martin disait :
>
Hi,
>
>
I'm quite new at Cocoa, and I guess I have some troubles to understand when a
>
new instance of an object or of a variable is created and when an existing
>
instance is reused. For example, I came up with a method that went something
>
like this:
>
>
-( void )doSomeThing {
>
int i;
>
NSArray *myArray;
>
for ( i = 0; i < [ anotherArray count ]; i++ ) {
>
myArray = [ [ NSArray alloc ] init ];
Here you are allocating a NEW array which you will have to release.
>
myArray = [ [ anotherArray objectAtIndex:i ]
>
componentsSeparatedByString:@"\t" ];
And here you make myArray point to another array (i.e. It no longers points
to the instance you've just allocated) which is autoreleased (since you got
it from a method which isn't alloc, or new, copy).
>
// do something here
>
[ myArray release ];
And here you are releasing the already autoreleased instance, which produces
a crash.
So you have BOTH a leak (the first array which is never released) and a
crash (release on an autoreleased object).
--
Thomas Deniau
_______________________________________________
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.