06-21-2011, 03:02 AM
#1 (permalink )
Registered Member
Join Date: Feb 2009
Posts: 58
Initialization of NSMutableArray in a loop
I have an IBAction like this:
Code:
if([checkinArray count]!=0){
[checkinArray removeAllObjects];
}
loop{
Checkin *checkinsA = [[Checkin alloc] init];
.....
[checkinArray addObject:checkinsA];
[checkinsA release];
}
And when checkinArray is not empty I am getting exc bad access error and the app is crashing. What I am doing wrong here? What I am trying to do is to empty the array and then refill ti with updated data. PS. The checkin array is declared in .h and is initialized on viewdidload.
06-21-2011, 03:58 AM
#2 (permalink )
Senior Member
iPhone Dev SDK Supporter
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
Where are you crashing (are you stepping through the code?), and what does the crash log look like?
06-21-2011, 06:21 AM
#3 (permalink )
Registered Member
Join Date: Feb 2009
Posts: 58
Its crashing at [checkinArray removeallobjects ]; with ECD_BAD_ACCESS sometimes.
06-21-2011, 08:07 AM
#4 (permalink )
Emphasizing Fundamentals
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
How do you create checkinArray?
And I don't see much point for this test:
Code:
if([checkinArray count]!=0){
[checkinArray removeAllObjects];
}
If it is already empty, then what does it matter if you remove the objects again? It's not like the operation will take that long. But if your most common situation involves a populated array, then all you've done is add a test in front of the operation that you really want to perform anyway. Test time + operation time > operation time, every time.
06-21-2011, 02:06 PM
#5 (permalink )
Registered Member
Join Date: Feb 2009
Posts: 58
Quote:
Originally Posted by
BrianSlick
How do you create checkinArray?
And I don't see much point for this test:
Code:
if([checkinArray count]!=0){
[checkinArray removeAllObjects];
}
If it is already empty, then what does it matter if you remove the objects again? It's not like the operation will take that long. But if your most common situation involves a populated array, then all you've done is add a test in front of the operation that you really want to perform anyway. Test time + operation time > operation time, every time.
I am trying to updated the content of the array with new data. Thats why I am testing if is empty. If is not then I am adding the data.
06-21-2011, 02:08 PM
#6 (permalink )
Registered Member
Join Date: Feb 2009
Posts: 58
Quote:
Originally Posted by
BrianSlick
How do you create checkinArray?
I am creating it in .h:
NSMutableArray *checkinArray;
....
@property (nonatomic, retain) NSMutableArray *checkinArray;
and initializing in viewdidload:
checkinArray = [[NSMutableArray alloc] init];
06-21-2011, 02:48 PM
#7 (permalink )
Emphasizing Fundamentals
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
Quote:
Originally Posted by
mavris
I am trying to updated the content of the array with new data. Thats why I am testing if is empty. If is not then I am adding the data.
So if it is not empty, you don't want new data?
Quote:
Originally Posted by
mavris
I am creating it in .h:
NSMutableArray *checkinArray;
....
@property (nonatomic, retain) NSMutableArray *checkinArray;
and initializing in viewdidload:
checkinArray = [[NSMutableArray alloc] init];
Looks ok, although you aren't using your property. Are you changing this anywhere else? Error sounds like an over-release.
And you are
declaring it in .h. You are
creating it in viewDidLoad.
06-21-2011, 03:42 PM
#8 (permalink )
Registered Member
Join Date: Feb 2009
Posts: 58
Quote:
Originally Posted by
BrianSlick
So if it is not empty, you don't want new data?
Looks ok, although you aren't using your property. Are you changing this anywhere else? Error sounds like an over-release.
And you are declaring it in .h. You are creating it in viewDidLoad.
what do you mean I am not using my property?
I am using it in another method:
Checkin *tempcheck2 = [checkinArray objectAtIndex:i];
but this should be autoreleased right? Also this method is finished before calling the [checkinArray removeAllobjects];
06-21-2011, 03:45 PM
#9 (permalink )
Emphasizing Fundamentals
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
See the properties link in my signature. You aren't using it, you are using the instance variable.
Post the entire code for your IBAction. You have a typo in the first post anyway.
06-21-2011, 03:50 PM
#10 (permalink )
Registered Member
Join Date: Feb 2009
Posts: 58
N/A
Last edited by mavris; 06-22-2011 at 05:19 PM .
06-21-2011, 04:14 PM
#11 (permalink )
Reading the Documentation
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
Isn't your Checkin class retaining it's attributes (it should)? in which case these would be leaks:
Code:
checkinsA.taggedName =[[NSMutableArray alloc] init];
checkinsA.taggedID =[[NSMutableArray alloc] init];
06-21-2011, 04:31 PM
#12 (permalink )
Registered Member
Join Date: Feb 2009
Posts: 58
I was releasing them at the end of the loop but I removed them to check if this is the reason of bad access error.
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Stats
Members: 175,664
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Leslie80