Objective-C: I need help retaining the value of an int. It's changing on me without my command.
The original question was: "How do you declare and retain an int?", that was satisfied in another post here: Objective-C: How do you declare and retain an int?
Now I have a problem where an int that was 18 is changing to 2, somehow on its own.
Here's my code:
@interface Game : Layer // this is from cocos2d
{
int maxSprites;
}
@implementation Game
-(void)initVariables
{
maxSprites = 18;
}
Later on, when I print it out, like
NSLog(@" maxSprites = %d ", maxSprites);
I get:
maxSprites = 2
And operations that require it to be 18, crash or don't work, as if it's really just 2 now.
How would that be possible? =)
Apple + Shift + F reveals no other usage of the maxSprites
variable.
I've looked at other code examples and often they're exposing the variable with a getter and setter, and they are also using @property
. Am I missing something? I'm new to Objective-C, so I might as well just be!
I did a Apple + Shift + F for maxSprites" In Project, Textual, Contains, Ignore Case and only resulted in:
Game.h: int maxSprites;
Game.m: maxSprites = 18;
Game.m: NSLog(@" maxSprites = %d", maxSprites);
Game.m: NSLog(@" maxSprites = %d", maxSprites);
Game.m: NSLog(@"maxSprites is at %p", &maxSprites);
Game.m: NSLog(@"maxSprites is at %p", &maxSprites);
Game.m: NSLog(@" maxSprites = %d", maxSprites);
Game.m: NSLog(@" maxSprites = %d", maxSprites);
Game.m: NSLog(@"maxSprites is at %p", &maxSprites);
Game.m: NSLog(@"maxSprites is at %p", &maxSprites);
I found the location where it changes using a watchpoint. It changes
Expression: “*(int *) 67379960”
New Value: 2
Old Value: 18
on this line:
[self checkMatchBarAward:spriteTypeToAdd];
Odd? That function doesn't do anything with maxSprites
, nor does that line.
EDIT: here is the function, I commented everything inside it out and it still occurs: .h
-(void)checkMatchBarAward:(int)spriteTypeToAdd;
.m
-(void)checkMatchBarAward:(int)spriteTypeToAdd
{
}
EDIT:
Thanks for the recommendations. I have cleaned all targets and it still changed values. Because of the answers you guys/gals gave, you lead me to the problem. Thanks for all of your help.
I posted my results below in an answer. Here's a copy:
Guys/gals you wouldn't believe what was the cause. Thank you for all your help because telling me to clean and look and check my types, that helped.
I looked over my arrays. I found one that was declared like this:
int matchBarArray[8];
2 lines up from the breakpoint halt where it says that the value changed from 18 to 2, I have this line:
matchBarArray[spritesCaptured-1] = spriteTypeToAdd;
And guess what, I overstepped the bounds of the array by 1. If I increase the size of the array to 9, I no longer get the int change from 18 to 2.
Also, if I overstep the bounds by more than 1, that is, I reduce the array size to smaller, there are other 开发者_运维问答things that start changing such as my score, booleans, the whole game ! =)
I can't believe hitting memory outside the array in Objective-C can cause such a riot =) AND IT'S SO HARD TO DEBUG!
Guys/gals, you wouldn't believe what was the cause. Thank you for all your help because telling me to clean and look and check my types, that helped.
I looked over my arrays. I found one that was declared like this:
int matchBarArray[8];
2 lines up from the breakpoint halt where it says that the value changed from 18 to 2, I have this line:
matchBarArray[spritesCaptured-1] = spriteTypeToAdd;
And guess what, I overstepped the bounds of the array by 1. If I increase the size of the array to 9, I no longer get the int change from 18 to 2.
Also, if I overstep the bounds by more than 1, that is, I reduce the array size to smaller, there are other things that start changing such as my score, booleans, the whole game ! =)
I can't believe hitting memory outside the array in Objective-C can cause such a riot =) AND IT'S SO HARD TO DEBUG!
If your checkMatchBarAward:
method truly is empty, then you need to clean your project (Shift + cmd + k) and rebuild it.
I didn't see the line you specify in your code.
Chances are, though, that you are referring to something as a long* when it's an int* or something like that (although how, after all these years, C STILL can't see that is beyond me).
Anyway, check your types where the sizes of what they point to may differ. If you reference an int * memory location with a long * (say, by passing in an int* to a method that expects a long*), you will fubar the stack and start modifying adjacent variables.
I've done this more than once, but in C, not Objective C--so I'm not sure it's the correct answer.
精彩评论