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-04-2010, 09:15 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 22
Ben. is on a distinguished road
Default Out of scope - Something is wrong with my model

Hi,

I created a subclass of NSObject called "subscription". In my UIViewController I have this "subscription"-Object as an instance variable.

In the "initWithNibName"-Method of the ViewController I also instantiate the "subscription"-Model:

Interface of the ViewController:
Code:
import "SubscriptionModel.h"
...
SubscriptionModel *subscription;
...
In the "initWithNibName"-Method I allocate the model as follows:
Code:
...
subscription = [[SubscriptionModel alloc] initWithDatabase:YES];
...
When I now try to set one of the attributes (e.g. "name") the debugger will show "out of scope". I add the attribute like this:
Code:
...
subscription.name = @"First Subscription";
...
The SubscriptionModel is a subclass of NSObject which has a init-Method which looks like this:
Code:
- (id)initWithDatabaseAccess:(BOOL)connectToDatabase {
	if ( (self = [super init]) ) {
		name		= [[NSString alloc] init];
		created	= [NSDate date];
	}
	return self;
}
The property "name" is defined as instance variable and property:
Code:
NSString *name;
...
@property (nonatomic, retain) NSString *name;
...
The "created"-Attribute is out of scope as well. If I call [NSDate date]; in the ViewController, it works fine.

What am I doing wrong? Has anybody an idea which I could go for?

If you need further input let me know.

Thanks for help!
Ben. is offline   Reply With Quote
Old 08-04-2010, 09:20 AM   #2 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 864
nobre84 is on a distinguished road
Default

Sometimes my debugger will creep out like that... I just use the debugger console and peek at variables manually using the comand "po" (print object)
po subscription
po [subscription name]
po [subscription created]
nobre84 is offline   Reply With Quote
Old 08-04-2010, 09:27 AM   #3 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,549
RLScott is on a distinguished road
Default

Questions about scope depend on where the code is. In the example you gave:
Code:
...
subscription.name = @"First Subscription";
...
where you say subscription is out of scope, you give no clue as to where this code is. But if subscription is an instance variable your view controller class, then it is in scope only for methods that are part of that view controller implementation. It is out of scope for code in any other class.

Edit: I just realized that my advice is irrelevant because you are dealing with a debugger problem, not a compiler problem. If it compiled without errors, then it is in scope syntactically.
RLScott is offline   Reply With Quote
Old 08-04-2010, 09:50 AM   #4 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 22
Ben. is on a distinguished road
Default

The po-Tool always tells me the right values. If I set "name" to "abc", in the UIViewController po says it is indeed "abc". So this seems to be fine.

As the app compiles and doesn't crash when I run over the methods maybe I worry too much? But it's really strange that it says "out of scope" even it might not me...

Any other ideas?

Thanks for your help!!
Ben. is offline   Reply With Quote
Old 08-05-2010, 06:48 AM   #5 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 22
Ben. is on a distinguished road
Default

You helped me a lot and I found the problem even I don't really know what I can do with it.

In one function I get an "int" as a parameter and call another function where I pass this "int"-Parameter.

Then it seems the "int" is released and the function crashes (BAD ACCESS).

I can't "retain" an int. But what can I do? Create a new int by copying the value?
Ben. is offline   Reply With Quote
Old 08-05-2010, 07:34 AM   #6 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 864
nobre84 is on a distinguished road
Default

Can you post the code you're referring to ? Usually values are automatically copied to the callee's stack and objects retained.
nobre84 is offline   Reply With Quote
Old 08-05-2010, 07:39 AM   #7 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 22
Ben. is on a distinguished road
Default

Code:
- (void)removeItemByUid:(int)uid {
	if ( db ) {
		[db runQuery:@"DELETE FROM table WHERE uid = %i", uid];
	}
}
I have "removeItemByUid" in a Model-Controller. This one passes "uid" to the database controller. But inside the database controller (which runs several functions internally like "escapeStrings", "openDb" etc) the passed uid is not available anymore.

I solved it now by creating the query-string as NSString inside the "removeItemByUid"-Function and passing it completely. That works fine.
Ben. is offline   Reply With Quote
Old 08-05-2010, 08:00 AM   #8 (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

There is rarely a reason to do this:

Code:
[[NSString alloc] init]
This should be retained:

Code:
created	= [NSDate date];
__________________
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-05-2010, 08:06 AM   #9 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 22
Ben. is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
There is rarely a reason to do this
Yeah, that was sample coding to make sure that "name" is not the reason.

Quote:
This should be retained:
Code:
created	= [NSDate date];
No, it's not necessary to retain this here. "created" is a property which is retaining automatically.

I will not waste time on this issue now. I created the string and everything is fine.

Thanks for your help!
Ben. is offline   Reply With Quote
Old 08-05-2010, 08:09 AM   #10 (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

Ok, you don't understand how properties work, so you should go read the link in my signature.
__________________
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-05-2010, 08:13 AM   #11 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 22
Ben. is on a distinguished road
Default

Ok, you don't know my code.
Ben. is offline   Reply With Quote
Old 08-05-2010, 08:17 AM   #12 (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

I know you posted a line of code that is not using a property.

I know you told me it is being retained automatically, despite not using a property.

I know this means you don't understand how properties work.

The rest of your code is irrelevant.
__________________
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-05-2010, 09:50 AM   #13 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 864
nobre84 is on a distinguished road
Default

In fact that NSDate variable really need to be retained by using self.created = [NSDate date] to assign it. It can give you BAD_ACCESS later on when you try to use it...
nobre84 is offline   Reply With Quote
Old 08-05-2010, 10:00 AM   #14 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 22
Ben. is on a distinguished road
Default

Guys, this is a very short part of the complete coding. If this is the only thing being done here, then yes, it needs to be retained. But this is not related to the problem I had before. The "created" property is retained. Trust me. So please don't waste your time on discussing the "created" property which is not related to the problem at all.

@BrianSlick: For my taste you judge people by too little.

My problem is solved now. Thanks for your help.
Ben. is offline   Reply With Quote
Old 08-05-2010, 10:04 AM   #15 (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

You show a line of code doing the wrong thing.

You then give an incorrect explanation for why you believe it is the right thing.

What other conclusion should I draw?

Tell you what, prove it: Show how that date object is being retained. At worst, you are wrong. At best, your technique is horrible.
__________________
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.

Last edited by BrianSlick; 08-05-2010 at 10:11 AM.
BrianSlick 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: 332
8 members and 324 guests
alexP, gordo26, headkaze, mistergreen2011, nobstudio, rayjeong, Sloshmonster, stanny
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,655
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, pungs
Powered by vBadvanced CMPS v3.1.0

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