I have the following variable in my .h file:
NSMutableArray *arr;
Then I have a method in implementation:
void aMethod
{
if (something)
{
arr= [[NSMutableArray alloc] init];
arr = [NSMutableArray arrayWithCapacity:0];
[arr addObject:someObject];
}
now if I try to access arr from any other method or even another if block within the same method, the app crashes and I can’t access that arr. For example:
//same method:
if (something else)
{
SomeObject *obj = [arr objectAtIndex:0]; //<---- it crashes on this line
}
Any light guys?
Thanks in advance
There are 2 errors here:
You have a leak
arr= [[NSMutableArray alloc] init]; //<–here
it crashes cause you’re creating autoreleased object and then try to access it when it is already deallocated:
arr = [NSMutableArray arrayWithCapacity:0];
remove this line: