Is it bug in the SDK ?

Discussion in 'iOS Programming' started by namanhams, Jul 2, 2009.

  1. namanhams macrumors regular

    Joined:
    Jun 3, 2009
    #1
    Hi everyone,

    I'm using Iphone SDK 3. There's some funny thing i've just seen and here's the code :

    A.
    Code:
    - (void) someMethod { 
         NSArray *dumbArray;
         NSLog(@"%@", dumbArray)
    }
    
    ==> Application crashes.

    B.
    Code:
    - (void) someMethod { 
         NSLog(@"xyz");
         NSArray *dumbArray;
         NSLog(@"%@", dumbArray)
    }
    
    ==> it prints out the current object that we send the someMethod message.
    Actually i've tested in some places and sometimes it crashes.

    C.

    ==> it prints out the content of array 'a'


    For B and C, i think in any circumstances it could not do that.
    For A, i'm not sure but i think it should print out 'null'



    Any idea ?
     
  2. Troglodyte macrumors member

    Joined:
    Jul 2, 2009
    #2
    Is dumbArray a string? No, it's an array. If you want a string description of an array use [dumbArray description].
     
  3. namanhams thread starter macrumors regular

    Joined:
    Jun 3, 2009
    #3
    I think NSLog can be used to print any object.
     
  4. ChOas macrumors regular

    Joined:
    Nov 24, 2006
    Location:
    The Netherlands
    #4
    As far as I know uninitialised pointers can point anywhere.

    So sometimes to data which is recognisable, and sometimes to null (or whatever) which results in crashes when accessed.
     
  5. Troglodyte macrumors member

    Joined:
    Jul 2, 2009
    #5
    Actually, on checking you're right. NSLog automatically tries to use the description method on objects to get a string value. I've always used an explicit call to it just in case.
     
  6. mpatric macrumors newbie

    Joined:
    Oct 20, 2008
    #6
    A and B crash, or exhibit inconsistent behaviour as dumbArray is nil.
     
  7. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #7
    WHS.
     
  8. Kingbombs macrumors member

    Joined:
    Jun 24, 2009
    #8
    Yep i believe this is true, which is why sometimes you will get a result and sometimes you won't (because it will be pointing to something it cannot print)
     
  9. mccannmarc macrumors 6502

    mccannmarc

    Joined:
    Aug 15, 2008
    Location:
    Manchester, UK
    #9
    And just to be really picky, you didn't terminate the NSLog() lines in your A and B examples with ;
     
  10. johnmcglone macrumors newbie

    Joined:
    Jun 30, 2009
    #10
    I've noticed that objective-c can be quite temperamental, so to keep it safe, always use [objectName description], equivalent to java's objectName.toString();
     
  11. namanhams thread starter macrumors regular

    Joined:
    Jun 3, 2009
    #11
    Thanks for all the replies. Now i know why sometimes when i define something as type A but XCode keeps claiming type B, C or D.... When i want to use nil value, i have to explicitly set its value to nil first.
     
  12. drivefast macrumors regular

    Joined:
    Mar 13, 2008
    #12
    the declaration NSAnything *myObject tells the compiler that myObject will have to be considered of type NSAnything. some compilers would add the necessary low level code to reserve 4 bytes, or 8 bytes, or whatever is sufficient, to store a number that will represent the memory pointer to your object when it will be created; some wont. some compilers may go the extra mile and set that pointer value to 0; some wont. however, this behaviour is not documented, and you shouldnt assume that if something happens once, it will always happen. to be more specific, there is no guarantee that your 3rd example will always work the same. an unassigned variable in your program is just that, an unassigned variable, and it's only luck that makes it point to anything at all.
     
  13. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #13
    This sounds like it could be a common error for people who try to learn Obj-C without learning C.

    Obj-C objects initialize their object pointer instance variables by default.

    C does not initialize its variables unless it's done explicitly.
    e.g.
    NSArray *dumbArray = nil;

    Accessing an uninitialized variable is usually considered an outright error by static analysis tools.

    So this is a bug in the OP's knowledge, not the SDK.
     

Share This Page