Crash with random() function

Discussion in 'iOS Programming' started by Sergio10, May 27, 2009.

  1. Sergio10 macrumors regular

    Joined:
    Oct 3, 2007
    #1
    Hi,

    During 3 days I investigate my code and suggest current source is bad
    PHP:
    - (int)randomWithRange: (NSIntegermin: (NSIntegermax
    {
        return 
    random() % max min
    }
    . . .
    while(
    YES)
    {
        
    int firstValue = [self randomWithRange20100];
        
    int secondValue = [self randomWithRange20100];
        
    int resultValue firstValue secondValue;
                                
        if(
    resultValue 100)
        {
            break;
        }
    }
    So current code crashes about 70% when I run it. But if I write the following code I'll get about 10% of crashes. And this is much better than previous case:
    PHP:
    - (int)randomWithRange: (NSIntegermin: (NSIntegermax
    {
        return 
    random() % max min
    }
    . . .
    while(
    YES)
    {
        
    int firstValue = [self randomWithRange19];
        
    int secondValue = [self randomWithRange19];
        
    int resultValue firstValue secondValue;
                                
        if(
    resultValue 9)
        {
            break;
        }
    }
    How to fix this problem?

    Thanks
     
  2. jnic macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    I've run the first piece of code a whole bunch of times. No crashes. Perhaps the issue is with some other part of the code? A part you haven't shown us yet? It would help if you could tell us where (which line) the code is crashing.
     
  4. Sergio10 thread starter macrumors regular

    Joined:
    Oct 3, 2007
    #4
    I'd like to know which line crashes too. But debug shows me assembler code when crashes (not function)

    Here is crash report:
    And console log:
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    Try commenting out the while-loop and see if it's still crashing. Oh, and are you trying to access resultValue outside of the while-loop? If so, you can't. It's instantiated within the while-loop and, therefore, is uninstantiated when the loop ends.
     
  6. Sergio10 thread starter macrumors regular

    Joined:
    Oct 3, 2007
    #6
    Problem is that application crashes time to time
     
  7. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #7
    The crash log you show isn't related to the code you show. The 'crash' is a runtime exception.

    Somewhere in your code you're calling sizeWithFont and the object that this is being called on isn't correct. This is usually a memory management bug caused by failure to retain an ivar.

    Also, set up a gdbinit file as described in this thread

    http://forums.macrumors.com/showthread.php?t=696095&highlight=gdbinit
     
  8. Sergio10 thread starter macrumors regular

    Joined:
    Oct 3, 2007
    #8
    And also have next crash:
    How to find where exactly crashes???
    Thanks


    P.S. I did found in my code method called: sizeWithFont
     
  9. Sergio10 thread starter macrumors regular

    Joined:
    Oct 3, 2007
    #9
    I solved the problem. The problem was next:
    PHP:
        UILabel *label = [[UILabel allocinitWithFrameframe];
        [
    label autorelease];
        [
    label setFont:[UIFont systemFontOfSize50]];
    Thanks to all)
     

Share This Page