Bug: Null pointers aren't really null pointers?

About Monkey 2 Forums Monkey 2 Development Bug: Null pointers aren't really null pointers?

Tagged: 

This topic contains 10 replies, has 5 voices, and was last updated by  Samah 2 years, 10 months ago.

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #1133

    Samah
    Participant

    Looks like you can successfully call a method on a null reference. It only fails if it tries to access Self, both explicitly or implicitly.

    [/crayon]

    Works fine, but will crash if you uncomment Print bar. You can do pretty much anything else.

    I spent about an hour hunting down an uninitialised field in another class, because this bug told me it was initialised.

    #1135

    Mark Sibly
    Keymaster

    This is by design – Self can be Null for non-virtual methods.

    #1137

    Samah
    Participant

    Whether or not it’s by design, you should never be able to call a method on a non-existent object. This isn’t Objective-C where you can send a message to nil.

    #1140

    therevills
    Participant

    I can see that this’ll cause quite a few headaches in the future!

    @Mark – can you show an example on how this is useful design?

    #1147

    Mark Sibly
    Keymaster

    I’ve used it a few times in c++, usually to prevent code from having to constantly check for null pointers in frequently called methods, eg: when incrementing/decrementing reference counts etc, eg:

    This saves you the hassle of having to wrap ALL retain() calls in ‘if( blah ) blah->retain();’, eg: you can just use blah->retain(); (or blah->release() etc ) even if blah is null.

    So I guess it’s useful for frequently used methods of classes that may have many null instances.

    I can live without this if it’s causing people hassles, although it’s never caused me any problems in the past – the first field access will raise an exception anyway if the method tries to do anything with the instance.

    #1160

    Pharmhaus
    Participant

    The Variable is ‘clearly’ not initialized. The C# way of throwing a compile error because you try to call a method on a non initialized variable would be more reasonable to me.

    #1162

    Samah
    Participant

    Sure, in that example it’s “clear”, but in my situation it was one of ten or so fields in a class, that was supposed to be assigned later in the app lifecycle. I was trying to access the field before it got to that stage. If it had thrown an actual exception on the method call, I would have been able to track it down sooner.

    If code in a method is executing, I would expect that the object exists to actually call it.

    Can’t you inline some of those checks anyway?
    Could you make it some kind of pragma that only does runtime checks in debug mode?

    #1163

    Pharmhaus
    Participant

    Well, for local variables it should be a compile error but for fields this is indeed absolutely unexpected.

    #1164

    dmaz
    Participant

    this is nice only sometimes as pointed out. it would be nice to keep this behavior but only if it can be explicit. maybe something along the lines of the null conditional operator on the roadmap…
    something like
    [removed]
    [edit]
    Actually
    a??.Hello()

    although in Marks code example the class itself is checking so using a null conditional would amount to less code being executable, correct? So the only use case is if we never check for null anywhere right? and if thats the case, what is the use case?

    #1173

    Pharmhaus
    Participant

    Well, one could use the same approach like in many functional programming languages where null is not a valid state for a variable if not stated explicitly.

    e.g.

    This has two major benefits: It is very easy to eliminate unnecessary null checks (because null is not allowed) and it is also very easy to distinguish between a result that is null and a failed operation that returns nothing because null <> None.

    Here is one of the many article on this matter : *click*

    #1175

    Samah
    Participant

    I guess I’ve been spoiled somewhat with Swift’s optionals recently. Adding something like that would be a huge undertaking, and I would expect it in version 2.0 at the earliest.

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.