Pointer Issue

Discussion in 'iOS Programming' started by iPhoneN00bz, May 30, 2009.

  1. iPhoneN00bz macrumors newbie

    Joined:
    May 30, 2009
    #1
    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!
     
  2. RobertBlackburn macrumors newbie

    Joined:
    Oct 7, 2008
  3. iPhoneN00bz thread starter macrumors newbie

    Joined:
    May 30, 2009
    #3
    i get a compiler error stating incompatible types in assignment...
     
  4. iPhoneN00bz thread starter macrumors newbie

    Joined:
    May 30, 2009
    #4
    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!
     
  5. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #5
    If you remove the * , you will be assigning values to address pointers. That's an obvious error.

    e
     
  6. BlackWolf macrumors regular

    Joined:
    Apr 9, 2009
    #6
    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;
           ...
    
     
  7. iPhoneN00bz thread starter macrumors newbie

    Joined:
    May 30, 2009
    #7
    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.
     
  8. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #8
    If you pass in just a simple type variable, you can't return any modifications to that variable to the caller. Look up "variable scoping".
     
  9. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #9
    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.
     
  10. mccannmarc macrumors 6502

    mccannmarc

    Joined:
    Aug 15, 2008
    Location:
    Manchester, UK
    #10
    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?
     
  11. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #11
    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.
     
  12. iPhoneN00bz thread starter macrumors newbie

    Joined:
    May 30, 2009
    #12
    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.

    agreed, thanks!

    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.
     
  13. mccannmarc macrumors 6502

    mccannmarc

    Joined:
    Aug 15, 2008
    Location:
    Manchester, UK
    #13
    Fair enough, suppose that makes sense but thats only one idea out of plenty more potentials.

    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
     

Share This Page