WebDialog RemoveHandler in WebPage Close Event nil object exception.

When I try and do RemoveHandler in the WebPage close event for a WebDialog I get a nil object exception.

Where should one make sure you tidy up the WebDialog?

Example below, also download.

Example

I create the WebDialog in a button action event on the WebPage

[code]
’ Define Modal
Modal1T = New Modal1

’ Add Handler
AddHandler Modal1T.Dismissed, AddressOf EDismissed
AddHandler Modal1T.Close, AddressOf EClose

’ Show Dialog
Modal1T.Show[/code]

In the WebDialog Dismissed event I remove the Handler

' Debug Message
  System.DebugLog "Modal1T Dismissed"
  
  ' Remove Handler
  RemoveHandler Modal1T.Dismissed, AddressOf EDismissed
  RemoveHandler Modal1T.Close, AddressOf EClose
  
  ' Set Nil
  Modal1T = Nil

However if you close the WebPage while the WebDialog is open, neither the Dismissed or the Close event happens for the WebDialog.

In the close event of WebPage I try to remove the Handler but get a nil object exception

[code]
’ Debug Message
System.DebugLog “WebPage1 Close”

If Modal1T <> Nil Then

' Remove Handler
RemoveHandler Modal1T.Dismissed, AddressOf EDismissed
RemoveHandler Modal1T.Close, AddressOf Close

’ Set Nil
Modal1T = Nil

End If[/code]

On which line is the exception?

Shouldn’t this be AddressOf EClose?

The exception is on RemoveHandler Modal1T.Dismissed, AddressOf EDismissed

Thats a typo, the example in the dropbox was correct for some reason lost the E when I pasted it here.

are you calling those removehandlers twice?

I open the Dialog and then close the web browser, the only other place that does RemoveHandler is in the WebDialog Dismissed event, this event never happens when you close the browser with the Dialog open. I’ve checked my example again.

2:54:23 PM My Application Launched 2:54:27 PM Oct 24 14:54:27 My Application.debug[37590] <Warning>: Button Action Event 2:54:44 PM Oct 24 14:54:44 My Application.debug[37590] <Warning>: WebPage1 Close Event

I have Debug statements in each event to make sure, above shows you what happens.

The debug statement in the webpage close event is before RemoveHandler Modal1T.Dismissed, AddressOf EDismissed

So It would appear it’s not being called twice.

My included dropbox example will show you what I mean.

Why not try to use delegate instead of handler?

This looks like a perfect place for try-catch exception handling

That was my plan, however am confused as to why I’m unable to remove it.

Little more confused now.

I assume I need to turn off break on exceptions otherwise it still breaks on RemoveHandler Modal1T.Dismissed, AddressOf EDismissed

With break on exceptions turned off it doesn’t seem to do the DebugLog in the catch for either of them.

[code] ’ Remove Handler for Dismissed
Try
RemoveHandler Modal1T.Dismissed, AddressOf EDismissed
Catch e As NilObjectException
System.DebugLog “Modal1T.Dismissed RemoveHandler NilObjectException”
End Try

' Remove Handler for Close
Try
  RemoveHandler Modal1T.Close, AddressOf EClose
Catch e As NilObjectException
  System.DebugLog "Modal1T.Close RemoveHandler NilObjectException"
End Try[/code]