NSString out of scope in init method
I have class, that has a NSString.
@interface AudioManager : NSObject {
AudioData *data_;
NSString *requiredMusic_;
开发者_如何学JAVA NSTimer *timer_;
MUSIC_ID musicId_;
float fadeIncrement_;
}
When i'm looking at requiredMusic_
variable via debugger in init
method it's written that NSCFString ... out of scope
. But if i will look at this variable from self
then it will be written it's nil!
-(id) init
{
self = [super init];
if (self)
{
data_ = &[[GameDataObject sharedObject] data]->audioData();
requiredMusic_ = @"bla bla bla";
musicId_ = MUSIC_OTHER;
}
return self;
}
Out of scope
is also written in other places and my code is not working as expected. What is the problem ?
-(void) changeBackgroundMusic:(NSString *)path
{
[requiredMusic_ release];
requiredMusic_ = path;
[requiredMusic_ retain];
...
}
In the code above it's also out of scope.
You are probably debugging a version of your program compiled with optimisation enabled. In optimized code, some variable may not be accessible via the debugger, because when compiling the compiler found that in this current function the variable scope can be reduced without changing the semantic and it did just that to improve the code (reduce register usage or memory access, ...).
You can try to recompile it with optimisation disabled. Depending on the compiler, it is possible to disable optimisation for just a part of a file with some #pragma
(for GCC this was introduced in version 4.4).
What Sylvain said. But...
data_ = &[[GameDataObject sharedObject] data]->audioData();
Don't do that! You will be taking the address of a return value which is pretty much guaranteed to not at all be what you want and I'm surprised the compiler even compiles that.
精彩评论