Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-21-2011, 03:02 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 58
mavris is on a distinguished road
Default 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.
mavris is offline   Reply With Quote
Old 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
dljeffery is on a distinguished road
Default

Where are you crashing (are you stepping through the code?), and what does the crash log look like?
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Old 06-21-2011, 06:21 AM   #3 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 58
mavris is on a distinguished road
Default

Its crashing at [checkinArray removeallobjects ]; with ECD_BAD_ACCESS sometimes.
mavris is offline   Reply With Quote
Old 06-21-2011, 08:07 AM   #4 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

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.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 06-21-2011, 02:06 PM   #5 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 58
mavris is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
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.
mavris is offline   Reply With Quote
Old 06-21-2011, 02:08 PM   #6 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 58
mavris is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
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];
mavris is offline   Reply With Quote
Old 06-21-2011, 02:48 PM   #7 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Quote:
Originally Posted by mavris View Post
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 View Post
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.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 06-21-2011, 03:42 PM   #8 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 58
mavris is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
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];
mavris is offline   Reply With Quote
Old 06-21-2011, 03:45 PM   #9 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

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.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 06-21-2011, 03:50 PM   #10 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 58
mavris is on a distinguished road
Default

N/A

Last edited by mavris; 06-22-2011 at 05:19 PM.
mavris is offline   Reply With Quote
Old 06-21-2011, 04:14 PM   #11 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

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];
baja_yu is offline   Reply With Quote
Old 06-21-2011, 04:31 PM   #12 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 58
mavris is on a distinguished road
Default

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.
mavris is offline   Reply With Quote
Reply

Bookmarks

Tags
array, iphone

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 326
8 members and 318 guests
chemistry, Dnnake, iOS.Lover, lendo, leostc, Leslie80, pbart, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,664
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Leslie80
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:27 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0