tags:

views:

82

answers:

3

Hi,

I am getting crazy over this error. Compiler is saying out of scope for an instance NSSString variable. Never had this thing before and used thousands of NSString instance variables!

Here is my class .h file

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>
#import "Snapshot.h"

@interface RecordAudioViewController : UIViewController <AVAudioRecorderDelegate, AVAudioPlayerDelegate> {
 NSString *filename;
}

@property (nonatomic, retain) NSString *filename;

- (IBAction) recordAudio;
- (IBAction) playAudio;

@end

Variable is synthesized properly. I initalize filename variable in viewDidLoad method. I want to use it in IBAction method recordAudio, but compiler always says out of scope? Why is that, is this a bug or something?

Here is .m code. viewDidLoad method where I set the filename instance variable:

- (void)viewDidLoad {
    [super viewDidLoad];

NSString *tmpDir = NSTemporaryDirectory(); filename = [NSString stringWithFormat: @"%.0f.%@", [NSDate timeIntervalSinceReferenceDate] * 1000.0, @"caf"]; NSLog(filename); }

And the IBAction method

- (IBAction) recordAudio 
{
    NSLog(filename); // here I get out of scope message when moving over with mouse cursor and when steping over this line EXC_BAD_ACCESS
}

The entire .m file can be seen here: http://pastie.org/1021993

A: 

You mentioned that you initialize the variable filename in the viewDidLoad method. if you mean nsstring alloc and init methods by initializing, i don't think that you are going the right way. It is not necessary to initialize a synthesized string, or more generically any strings. I'm not sure whether you meant this by initializing, but i gave my opinion based on the idea that i got from your Ques.

Nithin
No with initialize I ment I set a string in viewDidLoad. I use [NSString stringWithFormat] to do that.
Primoz Rome
then try using NSLog before and after the exact line where you need to use it, that will give some idea.
Nithin
I tried that. Immediately after I set the NSString *filename variable in viewDidLoad:filename = [NSString stringWithFormat: @"%.0f.%@", [NSDate timeIntervalSinceReferenceDate] * 1000.0, @"caf"];NSLog(filename);console output is correct: "299422764498.caf". But when I try to NSLog(filename) in an IBAction method that is triggered by a button tap I get: "Program received signal: “EXC_BAD_ACCESS”". If I break at that line and focus on the filename variable, the debugger says "out of scope". How can that be?
Primoz Rome
okay, i'm not answering your question here, but if you declare a property better access it using self.propertyname or in your case self.filename. also, edit you question and paste the part of the code where you initialize your string if you want more definite answers.
lukya
Hey lukya. No need to be rude. The first thing I tried was using "self.filename". No difference. "self." is not something that you need to use for your instance variables, and apple docs clearly state that out. I have modified my post with the code, as you stated.
Primoz Rome
A: 

Is viewDidLoad actually happening? If it doesn't get called, that would perfectly explain the crash in recordAudio as it hasn't been initialised.

JBRWilkinson
Yes it is... If I log out NSLog(filename) in viewDidLoad then everything is ok. But in any other method in this class the filename var is out of scope.
Primoz Rome
+3  A: 

Actually, if you set filename = [NSString stringWithFormat...], the autoreleased result is NOT retained.

However, if you use self.filename = [NSString stringWithFormat...] it WILL retain the string. Kinda looks like the string is getting released out from under you because you're not retaining it.

mharper
This is actually true and I didn't know about it. Thanks for helping out.
Primoz Rome
No problem! Glad I could help. Here's a blog post I wrote about the subject some time ago: http://blog.standalonecode.com/?p=101 and some zen I discovered about retain/release/autotrelease: http://blog.standalonecode.com/?p=96
mharper