0

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,

 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!

EDIT: hrmph, why'd I get a -1?

Thanks, I will try to learn how to do a Watchpoint.

Until then, I would like to say that 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);

2nd EDIT:

I found the location where it changes using a watchpoint. It changes here:

Expression: “*(int *) 67379960”
New Value: 2
Old  Value: 18

On this line:

[self checkMatchBarAward:spriteTypeToAdd];

Odd? That function doesn't do anything with maxSprites.

EDIT: -I'm going to make a new question now to find out why the value is changing on its own. Thank you for your help guys, great job.

New post will be taken up here: Objective-C: int value changing without cause

Community
  • 1
  • 1
Jeremiah
  • 73
  • 1
  • 7
  • May we see that method (or some reduced version of it that still creates the same error)? – Chuck Dec 21 '09 at 18:07
  • Hi Chuck, I'd like to give NSD the "answer" and green flagged him. I continue this problem in another thread here: http://stackoverflow.com/questions/1941686/objective-c-int-value-changing-without-cause – Jeremiah Dec 21 '09 at 18:13
  • since he satisfied the original question : "How do you declare and retain an int?" – Jeremiah Dec 21 '09 at 18:14

2 Answers2

6

You don't retain an int because it's not an object. Use a watchpoint and find out when your variable is changing.

Azeem.Butt
  • 5,855
  • 1
  • 26
  • 22
  • I got this problem and can't watch =( http://stackoverflow.com/questions/1598444/iphone-xcode-3-1-4-3-1-2-sdk-watchpoints-dont-work – Jeremiah Dec 21 '09 at 17:34
  • ok, found it. I used the simulator instead of device. and I get : Expression: “*(int *) 67379960” New Value: 2 Old Value: 18 on this line: [self checkMatchBarAward:spriteTypeToAdd]; odd? – Jeremiah Dec 21 '09 at 17:54
0

Are you sure that initVariables is called at all? Is the value always 2? Are you refering to the same variable called maxSprites? Try:

NSLog(@"maxSprites is at %p", &maxSprites);

It's hard to believe that the content of a variable just changes.

Edit: First I thought it could be "garbage" from the stack, but then I realized, that, of course, Objective-C objects are not stored on the stack, but the heap. And the MacOS X malloc implementation "blanks" allocated memory with 0.

Chris
  • 1,133
  • 7
  • 14
  • Thank you sir for the address printing, I totally forgot about using that in Objective-C. I'm more of a C guy. I sampled it at 3 intervals. here are the results: maxSprites = 18 maxSprites is at 0x8174f8 maxSprites = 18 maxSprites is at 0x8174f8 maxSprites = 2 maxSprites is at 0x8174f8 initVariables is called twice. It must be a grievous coding error on my part that I will laugh at terribly after I figure it out.. =) I will have to do watchpoint thang now. – Jeremiah Dec 21 '09 at 17:04