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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.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 03-15-2009, 06:53 PM   #1 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 5
Default Debugger says variable is "Out of scope"

I have a method which has suddenly started giving me some problems. It starts like this:

Code:
- (double) calcJulianDayNumber: (NSDate *) dateToConvert {
	
	// Get the day, month and year
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setDateFormat:@"M"];
	double month = [[dateFormatter stringFromDate:dateToConvert] doubleValue];
	[dateFormatter setDateFormat:@"YYYY"];
	double year = [[dateFormatter stringFromDate:dateToConvert] doubleValue];
	[dateFormatter setDateFormat:@"d"];
	double day = [[dateFormatter stringFromDate:dateToConvert] doubleValue];
	[dateFormatter release];
	
	// Work out Julian Date / Julian Day Number
	...
And is called like this, in the same class:

Code:
- (void) refresh: (NSDate *) dateToRefreshTo {
	workingJDN = [self calcJulianDayNumber:dateToRefreshTo];
	...
However, when I run the code, the iPhone simulator crashes. The debugger tells me that 'dateToConvert' on the line
Code:
double month = [[dateFormatter stringFromDate:dateToConvert] doubleValue];
is "Out of scope".

The only problem I can think is that I'm calling refresh:aDate from a different class. This could mean that if I'm passing things by reference rather than by value, then in the methods above I'm trying to access a variable which belongs to the calling class, not self. Which I guess isn't allowed. Right?

As you can tell, I'm new to all of this! Any guidance or general thoughts would be much appreciated!
haydnw is offline   Reply With Quote
Old 03-22-2009, 02:13 PM   #2 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 71
Default

Quote:
Originally Posted by haydnw View Post
I have a method which has suddenly started giving me some problems. It starts like this:

Code:
- (double) calcJulianDayNumber: (NSDate *) dateToConvert {
	
	// Get the day, month and year
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setDateFormat:@"M"];
	double month = [[dateFormatter stringFromDate:dateToConvert] doubleValue];
	[dateFormatter setDateFormat:@"YYYY"];
	double year = [[dateFormatter stringFromDate:dateToConvert] doubleValue];
	[dateFormatter setDateFormat:@"d"];
	double day = [[dateFormatter stringFromDate:dateToConvert] doubleValue];
	[dateFormatter release];
	
	// Work out Julian Date / Julian Day Number
	...
And is called like this, in the same class:

Code:
- (void) refresh: (NSDate *) dateToRefreshTo {
	workingJDN = [self calcJulianDayNumber:dateToRefreshTo];
	...
However, when I run the code, the iPhone simulator crashes. The debugger tells me that 'dateToConvert' on the line
Code:
double month = [[dateFormatter stringFromDate:dateToConvert] doubleValue];
is "Out of scope".

The only problem I can think is that I'm calling refresh:aDate from a different class. This could mean that if I'm passing things by reference rather than by value, then in the methods above I'm trying to access a variable which belongs to the calling class, not self. Which I guess isn't allowed. Right?

As you can tell, I'm new to all of this! Any guidance or general thoughts would be much appreciated!
Don't know if you have solved this yet, but my guess would be that you need to retain your date object at the point of creation.
__________________
Vines
jnorton is offline   Reply With Quote
Old 03-22-2009, 03:37 PM   #3 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,178
Default

Quote:
Originally Posted by jnorton View Post
Don't know if you have solved this yet, but my guess would be that you need to retain your date object at the point of creation.
That might cause other issues, but shouldn't cause the debugger to display "Out of scope" for the variable name.

To the OP, is is possible that you have multiple instances of variable named 'dateToConvert' within your method? I've seen behavior in the compiler where I have multiple 'count' or 'i' variables (each in their own scope) and the compiler will show "Out of scope" for all instances of that variable except the one that is currently IN scope. Take a good look at all of the variables in the variable list window in the debugger to see if this is perhaps the case.
Kalimba is offline   Reply With Quote
Old 03-22-2009, 06:08 PM   #4 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 71
Default

Quote:
Originally Posted by Kalimba View Post
That might cause other issues, but shouldn't cause the debugger to display "Out of scope" for the variable name.
It will if the variable was created in a separate thread without being retained. I have seen this behavior before.
__________________
Vines
jnorton is offline   Reply With Quote
Old 03-22-2009, 06:49 PM   #5 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,178
Default

Quote:
Originally Posted by jnorton View Post
It will if the variable was created in a separate thread without being retained. I have seen this behavior before.
It makes no sense how the retained state of a variable would affect the debugger's ability to determine whether said variable was in or out of scope. I'd like to see an example of this.
Kalimba is offline   Reply With Quote
Old 03-23-2009, 10:36 AM   #6 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 71
Default

Quote:
Originally Posted by Kalimba View Post
It makes no sense how the retained state of a variable would affect the debugger's ability to determine whether said variable was in or out of scope. I'd like to see an example of this.
I agree that it makes no sense for the debugger to do this, but here you go:

Code:
//
//  TestRetainAppDelegate.m
//  TestRetain
//
//  Created by James Norton on 3/23/09.
//  Copyright __MyCompanyName__ 2009. All rights reserved.
//

#import "TestRetainAppDelegate.h"

@implementation TestRetainAppDelegate

@synthesize window;

NSTimer *timer1;
NSDate *date;

-(void)printDate:(NSString *)d{
	NSLog(d);
}

-(void)test:(NSDate *)parmDate {
	NSString *desc = [parmDate description];
	[self printDate:desc];
}

-(void)callBack1 {
	
	[self test:date];
}

-(void)callBack2 {
	NSAutoreleasePool *pool = [ [ NSAutoreleasePool alloc ] init ];
	
	date = [NSDate date];
//	[date retain];
	
	[pool release];
}


- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    // Override point for customization after application launch
	timer1 = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(callBack1) userInfo:nil repeats:NO];
	
	[NSThread detachNewThreadSelector:@selector(callBack2) toTarget:self withObject:nil];
	
    [window makeKeyAndVisible];
}


- (void)dealloc {
    [window release];
    [super dealloc];
}


@end
Create a new Window-Based Application and replace the App-delegate with this code. Set a break point on line 23 (the first line of the test method) and debug. You should see parmDate labeled as "Out of scope".

Now uncomment line 26 (the retain) and debug again. Scoping problem goes away.

I don't know why this is, but it happens and I suspect this is the cause of the OP's problem.
__________________
Vines
jnorton 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: 426
13 members and 413 guests
bev6a8hl, beyondstop, Edsilmars, HowEver, Hyde, john234ny, linkmx, MozyMac, phillipie99, pill5b3rry, pochuang, pufftissue, Rudy
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 157,847
Threads: 88,913
Posts: 379,291
Top Poster: BrianSlick (7,072)
Welcome to our newest member, bev6a8hl
Powered by vBadvanced CMPS v3.1.0

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