A Bit of RB Trivia: The Answer

April 4th, 2008 by Joe Ranieri

So, the question I asked last time was this:
Name a situation when the global App object is nil (while executing user code).

The two answers that were submitted and correct are:

  • Charles Yeomans pointed out that properties in modules are destructed after the global App object has been set to nil. Example project.
  • Ben Johnson went the other way around and noticed that the global App object is nil in your Application subclass’ constructor. Example project.

However, neither of these were the answer I was looking for. What happens is that REALbasic does a bit of initialization before setting the global App object. Part of that initialization is setting up the menubar, which entails creating the MenuItems. If you have a subclass of MenuItem in that menubar, REALbasic invokes your constructor, and at that point, the global App object is nil. Example project.

Congratulations to Charles and Ben, both of which will be receiving a free license to CalendarKit.


COMMENTS

2 Responses to “A Bit of RB Trivia: The Answer”

  1. Massimo Valle Says:

    That’s (more or less) the Ben’s answer. With the difference that the Ben’s solution is pretty straightforward compared to the MenuItem one.

    I wonder if this is a bug or not. Surely it’s a potential troublemaker for those trying to query some App properties during the execution of the constructor of a MenuItem subclass instance. I always thought the App object is the first to be initialized and I think this should be the correct behavior.

  2. Jonathan Johnson Says:

    Actually, Ben’s answer happens later than the MenuItem situation, which is why we felt it was a separate answer. The MenuBar is constructed before the App object is constructed.

Leave a Reply