views:

746

answers:

1

Hi,

I have a table view, the mutable array pointer itemList holds the objects for its cells. I allocate and init the mutable array,

itemList = [[NSMutableArray alloc] init];

and add the objects into the array and the table view is loaded.

Now i need to dealloc the whole array and allocate again to hold a new array of objects to be shown in the table view. Here is what i am doing

if(self.itemList != nil) { [self.itemList removeAllObjects]; [self.itemList release];

}

self.itemList = [[NSMutableArray alloc] init];

and then add the objects into the array to show in the table view.

I am calling the [tableview reloadData];

The table loads properly but after this occurrs three or four times my app crashes

Now, 1. when i check the retain count after alloc called it shows 2 y? 2. My app crashes after some time with error "iphone Program received signal: “0”. warning: check_safe_call: could not restore current frame"

I suspect the itemList has not been deallocated properly.

Can any one guide me please?

+4  A: 

Ok many things here:

1) self.itemList = Since you have this, I am assuming you are using properties. if your @property is set as retain, calling self.itemList = xxx is much different than itemList = xxx. You see, when you use the self.itemList assignment, anything you had already in itemList is automatically dealloc'd for you and the new one is retained. So with this line:

self.itemList = [[NSMutableArray alloc] init];

you are actually retaining twice, while with this line:

itemList = [[NSMutableArray alloc] init];

you are retaining only once. Using properties is really easy and convenient way to do mem management, but all you need in this case is:

self.itemList = [NSMutableArray array]; //returns an autoreleased array that is then retained by the self.

Then you just need a single release call in your dealloc method to cleanup

[itemList release];

2) When an item is added to an array, it is retained by the array so you can release it right afterwords

Foo *f = [[Foo alloc] init];
[itemList addObject:f];
[f release];

When the array is released, it will send a release message to all its constituents, so you don't have to worry about them from this point on.

3) I am not sure of your crash, but try using the debugger to step through and find where it is crashing. It does not appear to be related to the memory management you have. You can search through this site for many questions about debugging and finding the source of memory errors, including my favorite, NSZombieEnabled

coneybeare
Hi coneybeare,I am passing the pointer to the array to a method.Does this increment a pointer. I think so as it is a copy operation.
sorry its not increment the pointer rather increment the retain count
passing the array pointer to a method does not naturally increment the retain count unless you are retaining it in the target method
coneybeare