iDevGames Forums
Weird problem passing integer variables.. - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Programming Languages & Scripting (/forum-8.html)
+--- Thread: Weird problem passing integer variables.. (/thread-1630.html)



Weird problem passing integer variables.. - quarus - Mar 14, 2009 05:53 AM

Hey guys,

I am having a weird problem with an Objective-C class I wrote. The problem itself is explained pretty quickly.

In my class GridPosition I have (among other things) two variables and a constructor:

Code:
//in GridPosition.h
int xPos;
int yPos;

//in GridPosition.m
-(id)initWithX: (int)xtmp Y:(int)ytmp{
    if (self = [super init]){
        xPos = xtmp;
        yPos = ytmp;
    }
    return self;
}


The problem is that whatever arguments I pass to the instance method, the values xPos and yPos always seemed to be randomly assigned !
For example:
Code:
[[GriPosition alloc] initWithX:6 Y:6]
Gives me values like 1086324736 for xPos and yPos.. Blink

Debugging shows that the instance method is indeed called with these values above. But how is this possible ? I passed in 6 and 6 ! I remember stumbling over this very problem a few month ago, but it somehow resolved itself - unfortunately that's not the case this time Annoyed

Any hints ?


I am using Mac OS 10.5.6 XCode version 3.1.2


Weird problem passing integer variables.. - ThemsAllTook - Mar 14, 2009 10:10 AM

Are xPos and yPos declared inside your @interface { ... } block? I can't tell from the snippet you pasted.


Weird problem passing integer variables.. - quarus - Mar 14, 2009 11:49 AM

ThemsAllTook Wrote:Are xPos and yPos declared inside your @interface { ... } block? I can't tell from the snippet you pasted.

Yup, the declaration is inside an interface block. I looked at the syntax over and over but there seems to be nothing wrong with it Sad


Weird problem passing integer variables.. - Josh - Mar 14, 2009 12:28 PM

Clean all + rebuild?


Weird problem passing integer variables.. - quarus - Mar 14, 2009 02:35 PM

Ok, I cleaned the project and built it again, but no luck. Next I removed the GridPosition class from my project (deleting the header and source files) and started to re-implement the class: The problem stayed Wacko After that I renamed the instance method from
Code:
-(id)initWithX: (int)xtmp Y:(int)ytmp

to
Code:
-(id)initWithXPos: (int)xtmp YPos:(int)ytmp

And things seem to work properly now. But this is so strange, I wish I knew what the cause for this problem was (or is).

Well thanks for your suggestions so far, anyway...


Weird problem passing integer variables.. - DoG - Mar 15, 2009 02:58 AM

there's probably an
Code:
-(id)initWithX: (double)x Y:(double)x;
method declared somewhere else, and since -alloc returns id, the compiler choses whichever comes first in the module. If you included the right header files, you should at least have seen a compiler warning.


Weird problem passing integer variables.. - quarus - Mar 15, 2009 12:47 PM

Hey DoG,

well I have a Vector class which has a similar method signature
Code:
-(id)initWithX:(int)x Y:(int)y Z:(int)z;

But I don't remember getting a compiler warning (if so I would have tracked down the problem, I'm sure)

But yeah, something must be getting mixed up during compilation.. I just can't pinpoint it. Next time the problem shows up, I will investigate further. For now I will just shrug it off and continue development.. Ninja

Thanks for your help !