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-28-2010, 05:30 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 4
Squatch is on a distinguished road
Default Several lines of code, that blows my mind (or NSString “out of scope” dilemma..)

Hey, everyone!
I'm totally confused with this small "out of scope" thing.
So here is some code to describe my situation:

Simple iphone view-based application.

View controller header:
Code:
#import <UIKit/UIKit.h>

@interface global_nsstring_testViewController : UIViewController {
	UIImageView*	image_view;
	NSString*		image_name;
}

@property (nonatomic,retain) UIImageView*	image_view;
@property (nonatomic,retain) NSString*		image_name;

- (void) fadeView:(UIImageView*)View andThenChangeImageTo:(NSString*)Name;
- (void) switchImageAfterFade;

@end
View controller .m:
Code:
#import "global_nsstring_testViewController.h"
@implementation global_nsstring_testViewController
@synthesize image_view, image_name;

- (void) viewDidLoad {
	image_view = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image1.png" ofType:nil]]];
	
//     ## Block 1 ##	
//	NSString* name = @"image2.png";
//	[self fadeView:image_view andThenChangeImageTo:name];

//	## Block 2 ##
//	NSString* name = [NSString stringWithFormat:@"image%d.png",2];
//	[self fadeView:image_view andThenChangeImageTo:name];
	
	[self.view addSubview:image_view];
	[super viewDidLoad];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//  ## Block 3 ##
//	NSString* name = @"image2.png";
//	[self fadeView:image_view andThenChangeImageTo:name];

//  ## Block 4 ##
//	NSString* name = [NSString stringWithFormat:@"image%d.png",2];
//	[self fadeView:image_view andThenChangeImageTo:name];
}

- (void) fadeView:(UIImageView*)View andThenChangeImageTo:(NSString*)Name {
	image_name = Name;
	image_view = View; // ## Point 1 ##
	
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:1];
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDidStopSelector:@selector(switchImageAfterFade)];
	View.alpha = 0.3;
	[UIView commitAnimations];
}

- (void) switchImageAfterFade {
	UIImage* image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:image_name ofType:nil]]; // ## Point 2 ##
	[image_view setImage:image];
}

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

@end
With uncommented #Block 4# I get “out of scope” message from debugger on image_name var at #Point 1#. And therefore I get EXC_BAD_ACCESS at #Point 2#.

However with uncommented #Block 3# or #Block 1# instead of #Block 4# everything works fine, so I suppose this is all about NSString...

I've tried to use NSMutableString, but without any success either.

So if anyone could tell me, what I do wrong or how to fix it, I would be greatly appreciate.


Another strange thing, that I don't get:

If to uncomment #Block 2# and not to use any breakpoints, it won't crash, but image will disappear. But if I set a breakpoint at #Point 2#, it will crash with EXC_BAD_ACCESS after just one step.

Additional information.
xCode version: 3.2.3

Last edited by Squatch; 08-28-2010 at 05:45 AM.
Squatch is offline   Reply With Quote
Old 08-28-2010, 05:36 AM   #2 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: London/Peterborough
Posts: 562
QuantumDoja is on a distinguished road
Default

Lets try and break this down to it's basics, ie, does this work?:

Code:
@interface global_nsstring_testViewController : UIViewController
{
NSString*		ImageName;
}

@property (nonatomic,retain) NSString*		ImageName;
-(void)Start
-(void)DoSomething(NSString*)new_Name;
Code:
@implementation global_nsstring_testViewController
{

- (NSString*) ImageName {	
    return ImageName;
}
- (void) setImageName: (NSString*) newValue {
    [ImageName autorelease];
    ImageName = [newValue retain];
}

-(void)Start
{
NSString* name = @"image2.png";
[self DoSomething:name];
}
-(void)DoSomething(NSString*)new_Name
{
ImageName = new_Name;
//can we get here?
}
}
It would also be great to get your full backtrace, just type backtrace in the console when it crashes.
QuantumDoja is offline   Reply With Quote
Old 08-28-2010, 06:09 AM   #3 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 4
Squatch is on a distinguished road
Default

Quote:
Originally Posted by QuantumDoja View Post
It would also be great to get your full backtrace, just type backtrace in the console when it crashes.
Here is a backtrace:
Code:
#0  0x30d654f4 in objc_msgSend ()
#1  0x321c621a in CFStringGetCharactersPtr ()
#2  0x321c8c2e in CFStringGetFileSystemRepresentation ()
#3  0x321da3b2 in _CFFindBundleResources ()
#4  0x321d53b6 in CFBundleCopyResourceURL ()
#5  0x30c4b000 in -[NSBundle pathForResource:ofType:] ()
#6  0x00003574 in -[global_nsstring_testViewController switchImageAfterFade] (self=0x13b370, _cmd=0x3db7) at /Users/Squatch/Documents/global_nsstring_test/Classes/global_nsstring_testViewController.m:43
#7  0x34b7b268 in -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] ()
#8  0x34b7b100 in -[UIViewAnimationState animationDidStop:finished:] ()
#9  0x31c8c960 in run_animation_callbacks ()
#10 0x31c8c750 in CA::timer_callback ()
#11 0x3223525c in __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ ()
#12 0x3223796c in __CFRunLoopDoTimer ()
#13 0x322383f0 in __CFRunLoopRun ()
#14 0x321df0c2 in CFRunLoopRunSpecific ()
#15 0x321defd0 in CFRunLoopRunInMode ()
#16 0x3390ff90 in GSEventRunModal ()
#17 0x34b0ab48 in -[UIApplication _run] ()
#18 0x34b08fc0 in UIApplicationMain ()
#19 0x00002f7c in main (argc=1, argv=0x2ffff4ac) at /Users/Squatch/Documents/global_nsstring_test/main.m:14
Your sample works fine, no matter where i call [self Start]
( in a viewDidLoad or touchesBegan )
or how init name
( NSString* name = [NSString stringWithFormat:@"image%d.png",2]; )
or just
( NSString* name = @"image1.png"; )

Thanks for a quick reply.

UPDATE: Here is the solution: link

Last edited by Squatch; 08-28-2010 at 06:33 AM.
Squatch is offline   Reply With Quote
Old 08-28-2010, 06:32 AM   #4 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,549
RLScott is on a distinguished road
Default

The property image_view is never initialized. It is only set to itself, which is still nil. Also, if you have defined image_view and image_name as properties, then you should always set them with their synthesized setters, as in

Self.image_name = ...
RLScott is offline   Reply With Quote
Reply

Bookmarks

Tags
nsstring, out of scope

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: 342
7 members and 335 guests
dre, freewind, hain, HemiMG, lendo, Newbie123, PlutoPrime
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,894
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

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