Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

iPhoneN00bz

macrumors newbie
Original poster
May 30, 2009
5
0
i'm writing a little collision detector and 1 method is making the simulator crash... i'm new to objective c and iphone dev, so i don't doubt that i'm doing something silly!

everything behaves as i'd expect it to except that it crashes somewhere inside the method below. i assume there's something i'm doing incorrectly with my min and max pointers because when i remove them from the equation, the simulator chugs along happily. though all tests i do to isolate an issue in this regard pass without fail.

Code:
+ (void) getProjectionPoly:(Polygon *)poly direction:(CGPoint)dir min:(float *)min max:(float *)max
{
	
	*min =  FLT_MAX;
	*max = -FLT_MAX;
	
	Edge *edge = [poly edges];
	
	while( edge != nil )
	{
		
		float projection = edge.A.x * dir.x + edge.A.y * dir.y;
		
		if( projection < *min ) *min = projection;
		if( projection > *max ) *max = projection;
		
		edge = edge.next;
		
	}
	
	[edge release];
	
}
and here's how i'm calling the method
Code:
float min;
float max;

[CollisionDetector getProjectionPoly:p1 direction:dir min:&min max:&max];
any help is appreciated!
 
looks like i was releasing something prematurely... i'm guessing! but i don't think the pointer is the cause anymore. sorry for newb'n it up!
 
isn't float a simple type anyway? so no object, no pointers, either in the method signature and in the method itself?
Code:
+ (void) getProjectionPoly:(Polygon *)poly direction:(CGPoint)dir min:(float)min max:(float)max
{
	
	min =  FLT_MAX;
	max = -FLT_MAX;
       ...
 
isn't float a simple type anyway? so no object, no pointers, either in the method signature and in the method itself?
Code:
+ (void) getProjectionPoly:(Polygon *)poly direction:(CGPoint)dir min:(float)min max:(float)max
{
	
	min =  FLT_MAX;
	max = -FLT_MAX;
       ...
no, that will just effect min and max in the context of this method; floats are passed by value, i believe. i need to write to the actual location in memory.
 
Why are you releasing edge?

Code:
[edge release];

You shouldn't do that.

Hmmm, actually I see that edge will be nil by the time that that line is executed. It's not going to cause a problem in that case but it's still wrong.

Also, just as a comment on the code, I would make local variables for min and max and only write them back to the calling code one time at the end. This is a minor optimization but might make the code clearer.
 
One idea:- Why don't you make min and max members of whatever class the method is part of and use them that way? If you declare them as properties and synthesize them you will have getters and setters for them.

Passing by reference seems pretty clumsy in this situation and there are definitely better alternatives even if the one i suggested above doesn't suit.

I guess you are from a windows api background where passing by reference is pretty much the norm?
 
One idea:- Why don't you make min and max members of whatever class the method is part of and use them that way? If you declare them as properties and synthesize them you will have getters and setters for them.

If there is exactly one polygon per object, this might work. But making min and max class members won't work if one has a lot of different polygons in each object that one wants to process in this function.
 
Why are you releasing edge?

Code:
[edge release];

You shouldn't do that.

Hmmm, actually I see that edge will be nil by the time that that line is executed. It's not going to cause a problem in that case but it's still wrong.
yes, releasing edge was the problem, though not right there... i had originally instantiated each edge as part of the routine but decided it was better done offline.

Also, just as a comment on the code, I would make local variables for min and max and only write them back to the calling code one time at the end. This is a minor optimization but might make the code clearer.
agreed, thanks!

mccannmarc said:
One idea:- Why don't you make min and max members of whatever class the method is part of and use them that way? If you declare them as properties and synthesize them you will have getters and setters for them.

Passing by reference seems pretty clumsy in this situation and there are definitely better alternatives even if the one i suggested above doesn't suit.

I guess you are from a windows api background where passing by reference is pretty much the norm?
my background is mostly all high-level languages like java and actionscript. pointers are something i've always understood conceptually but wanted to test myself on in a sense. that's part of the reason i expected the problem to be pointer related.

having said all of that, i have to respectfully disagree. i don't see it as clumsy, it's actually very clean and tidy imo. but that's the opinion of someone very new to the language so take it for what it's worth. also, it's really just a collection of static methods, so a class member solution doesn't really fit i don't think.
 
If there is exactly one polygon per object, this might work. But making min and max class members won't work if one has a lot of different polygons in each object that one wants to process in this function.

Fair enough, suppose that makes sense but thats only one idea out of plenty more potentials.

my background is mostly all high-level languages like java and actionscript. pointers are something i've always understood conceptually but wanted to test myself on in a sense. that's part of the reason i expected the problem to be pointer related.

having said all of that, i have to respectfully disagree. i don't see it as clumsy, it's actually very clean and tidy imo. but that's the opinion of someone very new to the language so take it for what it's worth. also, it's really just a collection of static methods, so a class member solution doesn't really fit i don't think.

Hmmm, clumsy was the wrong choice of word on my part. I suppose it all depends on the context of the program and its usage, but pass by reference is usually not the best way to access/modify values unless it is strictly necessary imo. Its all about preference though as is most of programming :D
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.