A Bit of RB Trivia

March 31st, 2008 by Joe Ranieri

There’s a free license to CalendarKit 2 to the first person who can answer this question:

Name a situation when the global App object is nil (while executing user code).

* Offer not applicable to Norman Palardy or past or present REAL Software employees.

Update: We have posted the answers.


COMMENTS

16 Responses to “A Bit of RB Trivia”

  1. charles Says:

    Suppose you have a GUI application, a class Class1 with destructor, and a module Module1 that holds a non-nil reference Foo to a Class1 object in a property. When the application quits, the App object is destroyed before the module properties are freed. When the reference to Module1.Foo is released, the destructor of the Class1 object is called. At that time, App returns nil.

  2. Joe Ranieri Says:

    Excellent Charles, Jon will contact you about the license. However, it wasn’t the answer I was looking for — the contest remains open!

    If you’d like Charles, you can answer again and try for a source license.

  3. Steve Upton Says:

    Isn’t this the case when you don’t create the application class?

    If I remember right, the way to get an ‘app’ object is to create a class of type ‘application’. Then RB will make the ‘app’ object available at runtime. It’s kinda like the MDI class…

    All new projects now contain an application class so I expect the complete answer, using today’s IDE, is to delete the application class from the project and then run the app… At that point you’d probably need to have a default window setup or you wouldn’t have any code running to actually witness the missing ‘app’…

    this is from memory without any confirmation so I could also be very wrong…

    Steve Upton

  4. Ben Johnson Says:

    How about in a constructor method added to the app class? Never tried it, until I saw this question, but app seems to be nil there.

  5. Joe Ranieri Says:

    @Steve: Nope. There’s *always* a subclass of Application (or ConsoleApplication) built into the application.

    @Ben: Nice, Jon will contact you about the license. However, it wasn’t the answer I was looking for — the contest remains open! If you’d like, you can answer again and try for a source license.

  6. Massimo Valle Says:

    Since you said “while executing user code” I can suppose during a Thread execution. The App instance which is in the main thread) could go nil while the thread is still running.

  7. Jonathan Johnson Says:

    @Massimo: I can’t reproduce that case. Could you email an example project? It appears that threads get killed before the App object gets set to nil.

  8. Massimo Valle Says:

    @Jon
    This was a supposition based on fact in some cases I had the impression a running thread is still working when the application crashes. (Perhaps the thread was busy executing code in system libraries, while the app closed.) Additionally the documentation says threads are managed by REALbasic’s Thread Scheduler which also manage the main application thread. But this is just a guess and I can’t provide an example project now.

  9. Massimo Valle Says:

    May be I’m wrong, but, while it’s certainly interesting, the Ben Johnson’s answer seems uncorrect to me. Adding a constructor to the App class do return a valid self object. Just try to set a “break” into the constructor and examine the self object with debugger.

    Anyway, I guess the correct answer could be to rename the App class to something else (eg. myApp) and then create an instance of any class named App. This will cause the App object to be nil.

  10. daniel Says:

    Under RB2008r1, if the super of App is changed to None, a NilObjectException is thrown.

    It could be argued that this case is NOT running user-code, as this can be exhibited on a new project with only this change.

  11. Joe Ranieri Says:

    @ Massimo: Ben’s answer is correct. Self is not nil, but the global App object is.

    Also, renaming the Application subclass to something other than ‘App’ does not have any affect. The global App object is still correct.

  12. Tim Jones Says:

    Just reaching here, Is this what can happen if you create a new subclass of application but don’t instantiate it? Since App follows the new subclass, and the subclass is potentially Nil, doesn’t that mean that App would be Nil?

    Tim

  13. daniel Says:

    I should add that the exception is thrown when running under the debugger. One can build an WinXP executable that apparently does nothing when run.

  14. Joe Ranieri Says:

    @daniel: That’s in the IDE, not running user code.

    @Tim: REALbasic will always create an instance of your Application subclass, regardless of your actions. Or perhaps I’m not following?

  15. Massimo Valle Says:

    An app which launches another application and then quits. This will continue to execute user code while the mother app is gone.

  16. Alacatia Labs » Blog Archive » A Bit of RB Trivia: The Answer Says:

    […] the question I asked last time was this: Name a situation when the global App object is nil (while executing user […]