Re: Memory management question
Re: Memory management question
- Subject: Re: Memory management question
- From: email@hidden
- Date: Mon, 29 Apr 2002 19:06:04 +0200
On lundi, avril 29, 2002, at 06:49 , Philippe Martin wrote:
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 ];
myArray = [ [ anotherArray objectAtIndex:i ]
componentsSeparatedByString:@"\t" ];
// do something here
[ myArray release ];
myArray = nil;
}
}
}
This crashed my application with an "EXC_BAD_ACCESS" signal. Not in
this method, but shortly after having exited from it (the method was
doing its job). When I commented out "[ myArray release ]" the crash
was gone, but then I assumed the application was leaking.
(1)myArray = [ [ NSArray alloc ] init ];
(2)myArray = [ [ anotherArray objectAtIndex:i ]
componentsSeparatedByString:@"\t" ];
// do something here
...
(3)[ myArray release ];
From a Stepwise Article
(
http://www.stepwise.com/Articles/Technical/HoldMe.html):
"If you allocated, copied, or retained an object, then you are
responsible for releasing the object with either -release or
-autorelease when you no longer need the object. If you did not
allocate, copy, or retain an object, then you should not release it."
(1) you're allocating an object and setting it to myArray
(2) you are setting the myArray pointer to an auto-released object
(3) you're releasing an auto-released object.
_______________________________________________
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.