Functions getting called multiple times?

Member
Posts: 61
Joined: 2009.01
Post: #1
I'm stepping through some code here and I've noticed some very odd behavior happening. In code like this:

Code:
doA();
[myVector add:otherVector];
doB();

Where the Vector class is:
Code:
@interface Vector2 : NSObject
{
@private
     float x;
     float y;
}
- (void) add:(Vector2 *)add;

@end

@implementation Vector2
- (void) add:(Vector2 *)add
{
     x += add.x;
     y += add.y;
}
@end

For some reason when stepping through the code (or when using print outs), the code goes in this order:

1) doA
2) add (a value that is not equal to the value of otherVector)
3) add (a value that is not equal to the value of otherVector)
4) add (a value that is not equal to the value of otherVector)
5) add (a value that is not equal to the value of otherVector)
6) add (a value that is not equal to the value of otherVector)
7) doB

And between the 5 times add is called, the x and y of the vector end up being 0 anyway. So basically that's a very roundabout way of doing absolutely nothing.
Quote:Adding: (3.035858,-5.346213)
Adding: (-5.690081,-5.689937)
Adding: (5.246247,-5.172186)
Adding: (-4.893734,5.067209)
Adding: (3.056985,-2.977011)

I've noticed this happen, like I said, with a few other functions as well, but sometimes a function gets called twice, and none of the other ones getting called twice called any problems like this one.

The only thing I could think of is because my parameter is also named "add." I'm coming from Java and C# development, where that would be completely inconsequential.

Anyone have any explanations? I can give exact code if necessary.
Quote this message in a reply
Member
Posts: 61
Joined: 2009.01
Post: #2
Well, the problem with nothing happening to the Vector was that it had not been retained and therefore was not initialized, which for whatever reason didn't give me any error or break the program.

Regardless, add is still being called 5 times even though I only call it once.

Hm, when using breakpoints and stepping through the code this time, add is only called once. However, if I put a printf in add, it prints 5 times. Huh?
Quote this message in a reply
Member
Posts: 345
Joined: 2002.04
Post: #3
Your method name "add", is the same as the parameter you pass through "add".
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #4
monteboyd Wrote:Your method name "add", is the same as the parameter you pass through "add".

It's not. The method is named "add:". The parameter is named "add". Even if they were the same, that would still work in Objective-C, due to the way the Objective-C messaging system works. The name of a method doesn't mean anything when used as a symbol by itself.
Quote this message in a reply
Member
Posts: 345
Joined: 2002.04
Post: #5
My mistake. Still, if it was my code I would prefer to have method names and parameter names more clearly different from each other for the sake of readability.
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #6
demonpants Wrote:Well, the problem with nothing happening to the Vector was that it had not been retained and therefore was not initialized, which for whatever reason didn't give me any error or break the program.

Regardless, add is still being called 5 times even though I only call it once.

Hm, when using breakpoints and stepping through the code this time, add is only called once. However, if I put a printf in add, it prints 5 times. Huh?

You should post more code. And your first paragraph seems to be missing a fundamental understanding of Objective-C.

And finally... don't use an Objective-C object for a vector. Yes, it looks like it might be a bit easier to have methods and what not, but the overhead is going to kill you.
Quote this message in a reply
Member
Posts: 34
Joined: 2009.01
Post: #7
This may be a total noob response, but don't your class members have to be synthesized in order to use the "dot notation" accessors on them?
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #8
Unfortunately, no, they can be any old method (as long as it doesn't have a parameter). Which means if I have an object with a method "draw", I can just do:

Code:
object.draw;

Which not only doesn't look like Objective-C, but it is totally misleading as to what it is. Very confusing, if you ask me.
Quote this message in a reply
Member
Posts: 61
Joined: 2009.01
Post: #9
longjumper Wrote:You should post more code. And your first paragraph seems to be missing a fundamental understanding of Objective-C.

And finally... don't use an Objective-C object for a vector. Yes, it looks like it might be a bit easier to have methods and what not, but the overhead is going to kill you.

Well you're right that I worded it incorrectly.

It wasn't retained and had an autorelease, so it was created but then the next animation loop it had already been removed by the autorelease pool, the end result being a nil variable.

So you think I should just use an X and a Y in the class itself that contains the vectors?

Like instead of Vector2 size have float sizeX, sizeY, instead of Vector2 position have posX, posY, etc.

Or is there another way to do this? How expensive is object instantiation/retention in Objective-C? Should you typically stay away from an object-oriented approach?
Quote this message in a reply
Member
Posts: 283
Joined: 2006.05
Post: #10
You could use two floats, or a struct containing two floats. Objects aren't slow, but on a class you'd use thousands of times per frame you might want to avoid them (especially on the iPhone).

But nothing about objects would result in the behaviour you're describing. I think you'll have to post more code if it's still acting strangely.
Quote this message in a reply
Member
Posts: 61
Joined: 2009.01
Post: #11
I've fixed the problem with the code (it was the problem of pointing through null) and the I was mistaken about the code being called multiple times. Going through the code line by line with the debugger only went through once even though printf went off multiple times.

I was just curious about the vector thing.
Quote this message in a reply
Post Reply