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 08-30-2011, 08:47 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 2
Magoolah is on a distinguished road
Default Problem adding objects to NSMutableArray

I am having problems adding objects to my NSMutableArray. It seems that something gets added (object count increases by 1 in debugger), but the only thing added is a 0x0 (null) instead of the address of an object. I've read through everything somewhat relevant that I could find, but I couldn't find anything that seemed to answer this issue. Most related posts seem to revolve around memory management, but the solution is not jumping out at me.

I appreciate any help you can provide.

I've included what I think are the relevant parts of the code. Please tell me if you need to see anything more.

Within GamePlayView.h

Code:
@interface GamePlayView : UIViewController {

    Player *gamePlayer;
    NSMutableArray *boardObjects;

}

@property (retain, nonatomic) Player *gamePlayer;
@property (retain, nonatomic) NSMutableArray *boardObjects;

@end
Within GamePlayView.m

Code:
- (void)viewDidLoad {

    // Create player

    Player *tempPlayer = [[Player alloc] initWithFrame: self.view.frame];
    if (tempPlayer == NULL) {
        NSLog(@"GamePlayView viewDidLoad: null Player");
    }
    else self.gamePlayer = tempPlayer;

    // Create array of board objects

    NSMutableArray *newArray = [[NSMutableArray alloc] init];

    self.boardObjects = newArray;
    [self.boardObjects addObject: gamePlayer];  // First breakpoint here
    topObject = 0;

    BoardObject *mine = [[BoardObject alloc] initWithFrame: self.view.frame];

    [self.boardObjects addObject: mine];  // Second breakpoint here
    topObject = 1;

    [super viewDidLoad];

} // End viewDidLoad
I put breakpoints at the addObject lines (commented in code). When execution stops at the first breakpoint, the debugger shows a good tempPlayer, and a good gamePlayer (both non-null, with the same address). It shows 0 objects in boardObjects, like this:

Quote:
boardObjects = (_NSArrayM *) 0x4b22080 0 objects
When I step over this breakpoint, the debugger shows 1 object in boardObjects, as follows:

Quote:
boardObjects = (_NSArrayM *) 0x4b22080 1 objects
0 = (NSObject *) 0x0

When I continue program execution, and the debugger stops at the next breakpoint, I also see a good mine object, with boardObjects still described as above. After stepping over this breakpoint, boardObjects now looks like this:

Quote:
boardObjects = (_NSArrayM *) 0x4b22080 2 objects
0 = (NSObject *) 0x0
1 = (NSObject *) 0x0
Magoolah is offline   Reply With Quote
Old 08-30-2011, 09:16 PM   #2 (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

You are leaking objects all over the place. You never release 'tempPlayer', 'newArray' or 'mine'.

Also, in viewDidLoad the call to super should come before all other code.
baja_yu is offline   Reply With Quote
Old 08-30-2011, 09:18 PM   #3 (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

Add this at the end of the method:

Code:
NSLog(@"self.boardObjects is: %@", [self.boardObjects description]);
Are you experiencing an actual problem in your app - which you didn't describe - or are you merely concerned about what you're reading from the debugger?
__________________
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 08-31-2011, 06:37 PM   #4 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 2
Magoolah is on a distinguished road
Default It works, but what's up with the debugger?

baja_yu: The releases are in the code, I just didn't show them. Thanks for the tip on moving the call to super to the top. I did that, with no change to what I was observing.

Brian: I was surprised to see that your NSLog revealed actual contents in the boardObjects array! Thank you. I suppose I had no problem after all.

However, I put a breakpoint at the NSLog line, and the debugger still shows 0x0 for each array entry, both before the NSLog line, and after it executes. Does this mean I'll never be able to get to array contents directly in the debugger?

To your question, I hadn't tried to proceed past this point in my app yet. I'll eventually be dynamically adding and removing hundreds of objects to this array, and I needed it to work properly before proceeding.
Magoolah is offline   Reply With Quote
Reply

Bookmarks

Tags
addobject, nsmutablearray

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: 398
5 members and 393 guests
JackReidy, jeroenkeij, Sami Gh, yomo710
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,671
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, JackReidy
Powered by vBadvanced CMPS v3.1.0

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