A Bit of RB Trivia
March 31st, 2008 by Joe RanieriThere’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.
March 31st, 2008 at 1:03 pm
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.
March 31st, 2008 at 1:18 pm
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.
March 31st, 2008 at 4:48 pm
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
March 31st, 2008 at 5:14 pm
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.
March 31st, 2008 at 5:51 pm
@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.
April 4th, 2008 at 1:57 pm
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.
April 4th, 2008 at 2:07 pm
@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.
April 4th, 2008 at 2:34 pm
@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.
April 4th, 2008 at 2:49 pm
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.
April 4th, 2008 at 2:50 pm
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.
April 4th, 2008 at 2:52 pm
@ 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.
April 4th, 2008 at 2:52 pm
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
April 4th, 2008 at 2:57 pm
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.
April 4th, 2008 at 2:59 pm
@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?
April 4th, 2008 at 3:33 pm
An app which launches another application and then quits. This will continue to execute user code while the mother app is gone.
April 4th, 2008 at 5:58 pm
[…] the question I asked last time was this: Name a situation when the global App object is nil (while executing user […]