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 > Mac OS X Development Forums > Mac OS X Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-24-2010, 10:17 PM   #1 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 4
The Hoff is on a distinguished road
Default NSMutableArray Out of Scope

I am very confused by this Out of Scope problem I keep receiving. For some reason any time I try to add an object to the array it's data isn't stored. This problem when I use the function fireCanonBall. I tried storing objects in the array 2 different ways with the same data, the first way local to the method and released at the end of the method. The second way public with accessors in the class. For some reason when I try to create a ball object using the former the object wont properly create. But the later will properly create the object. If I dont use the array during the update the program will run fine, but when I try to use the array it will still run without errors, just the canon will not shoot the canon ball. Also, initObject from the canonBall class is basically the same as here in the canon class. Any ideas about why it is not storing the canon ball objects would be appreciated.

// .h
Code:
#import <OpenGLES/ES1/gl.h>
#import "3DObject.h"
#import "Canonball.h"


@interface Canon : NSObject
{
	Object3D canonObject; //contains rot, pos, deg, and scale.
	Object3D ballObject; //contains rot,pos,scale for canon ball object;
	NSMutableArray *canonBalls; 
	Canonball *curball;
	Vector3D vel; //different cannons might be able to shoot cannon balls faster, or slower.
	float gravity;
}


-(void) update: (float) dt;
-(id) initObject: (Object3D)O ballObject:(Object3D)B gravity:(float)g vel:(Vector3D)v;
-(void) fireCanonBall;

//dont understand @property. nonatomic means not multithreaded, and atomic is vice versa.
@property (nonatomic,retain) Canonball *curball;
@property (nonatomic,retain) NSMutableArray *canonBalls;
@property Object3D canonObject;
@property Object3D ballObject;
@property Vector3D vel;
@property float gravity;
@end
// .m
Code:
#import "Canon.h"

@implementation Canon

@synthesize curball;
@synthesize canonObject;
@synthesize canonBalls;
@synthesize gravity;
@synthesize vel;
@synthesize ballObject;


-(id) initObject: (Object3D)O ballObject:(Object3D)B gravity:(float)g vel:(Vector3D)v
{
	self = [super init];
	if (self)
	{
		canonObject = O;
		ballObject = B;
		gravity = g;
		vel = v;
		canonBalls = [[NSMutableArray alloc]init];
	}
	return self;
}
-(void) dealloc
{
	[curball release];
	[canonBalls release];
	[super dealloc];
}

-(void) update: (float) dt
{
        //update cannon and any launched cannon balls
	//[curball setBallObject:ballObject];
	//[curball update: dt];
	//ballObject = curball.ballObject;
	
.
	for(canonball *obj in canonBalls)
	{
		[obj update: dt];
		[obj setBallObject:ballObject];
		ballObject = obj.ballObject;
	}
	
}
-(void) fireCanonBall
{
        //first way I tried to add object
	Canonball *tmpBall = [[Canonball alloc]initObject:ballObject gravity:gravity initVel:vel];
	[canonBalls addObject:tmpBall];

        //second way I tried to add object not using my custom init.
	curball = [[Canonball alloc]init];
	[curball setBallObject:ballObject];
	[curball setGravity:gravity];
	[curball setInitVel:vel];					   
	
	[canonBalls addObject:curball];
	[tmpBall release];
	
	
	//create Canonball]
	//give it data
}

@end

EDIT: If I posted this in the wrong forum, sorry.

Last edited by The Hoff; 07-25-2010 at 12:17 AM.
The Hoff is offline   Reply With Quote
Old 07-24-2010, 11:04 PM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Try this before and after adding the cannonball to the array:

Code:
NSLog(@"canonBalls: %@",canonBalls);
That'll let you know how many cannonballs you have, and whether that methods is really being run. Also add a similar line in the cannon update: method.

Code:
NSLog(@"canonBalls in update: %@",canonBalls);
If any of those read 0x0 (nil) then you are probably not calling your [cannon initObject: BallObject: gravity: vel:] method. If you just call [[cannon alloc] init] then the array woulds still be nil and you'd be unable to store anything in it.

If you get some other result then something else is going on; let me know what the logs say.

PS if you capitalize your class names and lower-case your variable names it'll make it easier for others to scan your code quickly. Right-click a class or variable and choose "Refactor" to rename it everywhere in the program.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 07-25-2010, 12:01 AM   #3 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 4
The Hoff is on a distinguished road
Default

Quote:
Originally Posted by smasher View Post
...

PS if you capitalize your class names and lower-case your variable names it'll make it easier for others to scan your code quickly. Right-click a class or variable and choose "Refactor" to rename it everywhere in the program.
Thanks for the tip, didnt know I could do that.

Here is the output from the logs. Also, here is a code sample of where I am creating the canon object.

Code:
-(void)createCanon
{
	Vector3D Vel;
	Vel.x = 50;
	Vel.y = 0;
	Vel.z = 50;
	float G = -32.2;//-9.8;
	m_pCanon = [[Canon alloc]initObject:mGameObjects[GO_CANON] ballObject:mGameObjects[GO_CANONBALL] gravity:G vel:Vel];
	[m_pCanon fireCanonBall];
	
}
-(void)updateCanon: (float) dt
{
	
	[m_pCanon setBallObject:mGameObjects[GO_CANONBALL]];
	[m_pCanon update: dt];
	mGameObjects[GO_CANONBALL].mPosition =      m_pCanon.ballObject.mPosition;
	
}
//Before adding to array
2010-07-24 23:59:41.998 Cannon[5145:207] canonBalls: (
)
//After adding to array
2010-07-24 23:59:42.015 Cannon[5145:207] canonBalls: (
<canonball: 0x9062e00>,
<canonball: 0x9054a00>
)

//Update
2010-07-24 23:59:53.847 Cannon[5145:207] canonBalls in update: (
<canonball: 0x9062e00>,
<canonball: 0x9054a00>

EDIT: Applied refractoring to example codes in original post to make easier to read.

Last edited by The Hoff; 07-25-2010 at 12:13 AM.
The Hoff is offline   Reply With Quote
Old 07-25-2010, 12:30 AM   #4 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Okay, it sure looks like the cannonballs are getting added to the array. So what's the problem again?

If they're not moving I'd check the Canonball update method.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 07-26-2010, 01:45 PM   #5 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 4
The Hoff is on a distinguished road
Default

Quote:
Originally Posted by smasher View Post
Okay, it sure looks like the cannonballs are getting added to the array. So what's the problem again?

If they're not moving I'd check the Canonball update method.
When I run through debug and check immediately whats in the array it shows the 2 objects in the array are out of scope. If I check the array during the update it shows the 2 objects are out of scope. When I run the program using the array the cannon wont fire the canon balls. But whenever I dont use the array and just use the pointer" Canonball *curball during the update the canon fires the canon balls perfectly and the canonball does its trajectory perfectly. I really dont get why its not working...
The Hoff is offline   Reply With Quote
Old 07-26-2010, 11:22 PM   #6 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Quote:
Originally Posted by The Hoff View Post
When I run through debug and check immediately whats in the array it shows the 2 objects in the array are out of scope. If I check the array during the update it shows the 2 objects are out of scope.
Quote:

Sounds like an issue with the debugger; I don't think it's related to your trouble though.
When I run the program using the array the cannon wont fire the canon balls. But whenever I dont use the array and just use the pointer" Canonball *curball during the update the canon fires the canon balls perfectly and the canonball does its trajectory perfectly. I really dont get why its not working...

Do you mean this code works:
Code:
        //update cannon and any launched cannon balls
	[curball setBallObject:ballObject];
	[curball update: dt];
	ballObject = curball.ballObject;
but this one doesn't?
Code:
	for(canonball *obj in canonBalls)
	{
		[obj update: dt];
		[obj setBallObject:ballObject];
		ballObject = obj.ballObject;
	}
You swapped the order of update and setBallObject there. I assume the problem is inside one of those methods. The code you posted looks OK, although it's hard to tell what it's supposed to do. What do those two methods do? Why do you set ballObject = obj.ballObject and not use the value?
__________________

Free Games!
smasher is offline   Reply With Quote
Old 07-27-2010, 02:12 AM   #7 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 4
The Hoff is on a distinguished road
Default

Quote:
Originally Posted by smasher View Post
Do you mean this code works:
Code:
        //update cannon and any launched cannon balls
	[curball setBallObject:ballObject];
	[curball update: dt];
	ballObject = curball.ballObject;
but this one doesn't?
Code:
	for(canonball *obj in canonBalls)
	{
		[obj update: dt];
		[obj setBallObject:ballObject];
		ballObject = obj.ballObject;
	}
You swapped the order of update and setBallObject there. I assume the problem is inside one of those methods. The code you posted looks OK, although it's hard to tell what it's supposed to do. What do those two methods do? Why do you set ballObject = obj.ballObject and not use the value?
Wow... I didnt even look at that because when I would go in debug it would say everything in the array was out of scope so I was convinced it was a memory problem of some sort. But by making the switch you noticed above that fixed the problem. Basically the ball was never updating because I was never setting its new data before updating...

Thank you for your help, wow I feel dumb now...
The Hoff is offline   Reply With Quote
Reply

Bookmarks

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: 460
17 members and 443 guests
alexeir, David-T, Dj_kades, foslock, iAppDeveloper, j.b.rajesh@gmail.com, jeroenkeij, mer10, Mijator, mkjarred, myach, pipposanta, QuantumDoja, robsmy, sacha1996, SLIC, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,129
Posts: 402,928
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

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