Web1, Xojo 2019 R1.1 What calls the close event in App

Hello all,

Suddenly I am seeing my app close spontaneously. Unfortunately, by the time I can trap it, it already is in the App.Close Event. The Session.Close event does fire before App.Close event - I think…

Can anyone suggest where to start looking for the cause of this behavior?

Thanks,
Tim

Are there any active sessions when this happens?

Hi Tim!
Only one.

If there’s still an active session, then the framework shouldn’t be closing the app. You may be hitting an unhandled exception. Do you have an UnhandledException event in your session class? That might shed some light on what’s going on.

Something else to try is intentionally raising an exception in the close event and then logging the stack.

It looks like the Session.close is NOT related the main problem of App.Close.

I started seeing Session.Close when I was doing debugging. If there were any tables left open, they would automatically create a new session (as they should). Closing the tab caused the Session.close which is how it should be,

I’ll look into your suggestions!
Tim

I have this in the session.unhandled…
Dim Flds() As String = Array(“EXCEPTION”, CurrentMethodName , CStr( error.Type) + " Err Msg = " + Error.Message )
Logs.AddFields Flds
Logs.LogFileAppend( Logs.K_Trace_App)
Return False

I have this is the App.Unhandled…
System.DebugLog( "Unknown exception in: " + CurrentMethodName + " no error type available " + CStr( error.Type) + " Err Msg = " + Error.Message )
Return False

Is it correct to return false?

It depends. If you return false, the app will close after an exception occurs. Returning true will prevent that but only you can decide if that means your app is in an unstable state.

How do I do this? Never had the opportunity…

Tim

Something like

var oob as new OutOfBoundsException
raise oob

exception e as OutOfBoundsException
   var s() as String = e.Stack
   // process the stack

I chose OutOfBoundsException at random.

This would go in App.Close to get the stack trace as Greg suggested.

You don’t need to break this up. just use:

Raise New OutOfBoundsException
1 Like

Where should this code be placed? Somewhere in the Session?
Thanks guys!
Tim

In app.close. By raising the exception, you can capture the stack trace that got it there. It may be helpful in diagnosing the problem. No guarantees, though. We’re still grasping at straws.

Thanks guys.
I put the code in the App.Close Event. This is the code… When it hits the line to raise the event, it goes right down to the Exception e as RuntimeException

Sub Close()
// App.Close

// Call UPnP.Router_ClosePort(0)
Raise New OutOfBoundsException

System.DebugLog( “APP - SHUT DOWN in: " + CurrentMethodName + " App Closing” )

Dim Flds() As String = Array(“APP - SHUT DOWN”, CurrentMethodName , “”)
Logs.AddFields Flds
Logs.LogFileAppend( Logs.K_Except_App_Err )
return

// GPIO.DigitalWrite( k_GPIO_GUI_On, GPIO.Off )

Exception e as RuntimeException
Flds() = array(“EXCEPTION”, CurrentMethodName, Cstr( e.Type) )
Logs.AddFields Flds
Logs.LogFileAppend( Logs.K_Except_App_Err)
End Sub

Uh yeah, you need to wrap the exception in a try-catch and in the catch, that’s where you grab and log the stack… because the manufactured exception shouldn’t be treated as an exception.

Raising the exception should be the last thing you do in your “normal” code. In place of the “return” line. Then in your exception handler, you should log the contents of e.Stack.

We are manufacturing an exception (via raise new OutOfBoundsException) purely to get to the contents of the stack. There are 2 ways to do this, with Exception e as OutOfBoundsException (be explicit here), or with Try/Catch as Greg suggests.

OK, so like this? And add a break point where stak=stak?
Thanks guys!
Tim

System.DebugLog( “APP - SHUT DOWN in: " + CurrentMethodName + " App Closing” )

Dim Flds() As String = Array(“APP - SHUT DOWN”, CurrentMethodName , “”)
Logs.AddFields Flds
Logs.LogFileAppend( Logs.K_Except_App_Err )

Dim x As integer
Try
Raise New OutOfBoundsException
Catch
x =x
Exception e as RuntimeException
Dim Stak As String = e.Stack
Stak = Stak
End Try

Take out the Exception e as … line. The two are mutually exclusive.

Dude. If you’re giving advice, read the code after. That’s not going to compile without the “e”

@Tim_Seyfarth
You don’t need both if you’re going to raise the exception at the end and allow it to crash. What I was suggesting was to simply use a tight scoped exception handler for getting the stack and not change any other code in your method. That would look like this:

Try
    Raise New OutOfBoundsException
Catch e as OutOfBoundsException
    Dim st() as string = e.stack
    Break
    // write the stack to a log for runtime use
    Logs.AddFields st
End Try
1 Like