[02/03] Sometimes my app displays error when it exits
Hi,
Sometimes my app displays error when it exits. It is developed in VB.NET 2003 and I use 3rd party components. They do not have any news that this error comes from their components.
I don't know where and wich is the problem. I reviewed sometimes the code. All code is in "Try...Catch....Finally" but it doesn't catch the error.
This error it displays when I debug the application :
"An unhandled exception of type 'System.NullReferenceException' occurred
in system.windows.forms.dll
Additional information: Object reference not set to an instance of an
object."
And this is the error that displays in the Pc where the app is installed:
unknown software exception (0*c0020001) occurred in the application at location 0*7c812a5b
Re: [02/03] Sometimes my app displays error when it exits
Hi,
yes, the first line of code of the start form:
Code:
Public Class frmLogin
Before, I had some code like this:
Code:
Module mdlStart
Public Sub Main()
Try
SubMain()
Catch ex As Exception
HandleUnhandledException(ex)
End Try
End Sub
Public Sub SubMain()
AddHandler System.AppDomain.CurrentDomain.UnhandledException, AddressOf OnUnhandledException
Application.Run(New frmLogin)
End Sub
Private Sub OnUnhandledException(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
HandleUnhandledException(e.ExceptionObject)
End Sub
Private Sub HandleUnhandledException(ByVal o As Object)
Dim e As Exception = DirectCast(o, Exception)
MessageBox.Show(e.StackTrace, "Unhandled exception.")
Application.Exit()
End Sub
End Module
Re: [02/03] Sometimes my app displays error when it exits
Originally Posted by jmcilhinney
Did you add any code to your form's constructor?
Code:
#Region " Windows Form Designer generated code "
Public Sub New(Optional ByVal tablaDatos As DataTable = Nothing, Optional ByVal listaCampos As ArrayList = Nothing)
MyBase.New()
Yes I have code in the start form and others.
Start form:
Code:
Dim sqlStr As String = ""
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
DevExpress.UserSkins.BonusSkins.Register()
DevExpress.Skins.SkinManager.EnableFormSkins()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
.
.
in other forms, I have code like this:
Code:
Region " Código generado por el Diseñador de Windows Forms "
'singleton patern
Private Shared fALm As frmAlmacen = Nothing
Public Shared Function Instance() As frmAlmacen
If fALm Is Nothing Then
fALm = New frmAlmacen
End If
fALm.Focus()
Return fALm
End Function 'Instance
Public Sub New()
MyBase.New()
'El Diseñador de Windows Forms requiere esta llamada.
InitializeComponent()
'Agregar cualquier inicialización después de la llamada a InitializeComponent()
End Sub
'Form reemplaza a Dispose para limpiar la lista de componentes.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
fALm = Nothing
End Sub
Are you initialising any member variables where they're declared?
Yes. For example:
Code:
dim identificador As Integer = 0
dim printPdf As Boolean = False
Re: [02/03] Sometimes my app displays error when it exits
just a guess, but perhaps sometimes when an exception is thrown, there is no valid exception object.
In this code
Code:
Private Sub HandleUnhandledException(ByVal o As Object)
Dim e As Exception = DirectCast(o, Exception)
MessageBox.Show(e.StackTrace, "Unhandled exception.")
Application.Exit()
End Sub
there is no actual error handling... so if for some reason the o param is nothing, this code will fail.
Code:
Try
Private Sub HandleUnhandledException(ByVal o As Object)
if o is nothing then return
try
Dim e As Exception = DirectCast(o, Exception)
MessageBox.Show(e.StackTrace, "Unhandled exception.")
catch ex as exception
MessageBox.Show(ex.message, "Unhandled exception.")
finally
Application.Exit()
end try
End Sub
Re: [02/03] Sometimes my app displays error when it exits
Hi,
thanks for your replies.
but now I no have that code in my project and continues to show the error. Is there any way to capture it? Because the code of my fifth place, it did not catch.
The code that runs when I close any form, I always within "Try ...Catch ....Finally", so I do not know where it comes from the error.
Re: [02/03] Sometimes my app displays error when it exits
Originally Posted by kleinma
there is no actual error handling... so if for some reason the o param is nothing, this code will fail.
Code:
Try
Private Sub HandleUnhandledException(ByVal o As Object)
if o is nothing then return
try
Dim e As Exception = DirectCast(o, Exception)
MessageBox.Show(e.StackTrace, "Unhandled exception.")
catch ex as exception
MessageBox.Show(ex.message, "Unhandled exception.")
finally
Application.Exit()
end try
End Sub
Hi,
I changed my code following what you say in the last message. By now, the app doesn't crash
Re: [02/03] Sometimes my app displays error when it exits
Even I have this issue. I have not found any solution. This error appears in debug mode:
"An unhandled exception of type 'System.NullReferenceException' occurred
in system.windows.forms.dll
Additional information: Object reference not set to an instance of an
object."
and this error appears when the app is already installed in any PC.
unknown software exception (0*c0020001) occurred in the application at location 0*7c812a5b
I would be grateful for any help.
Regards
PD: I want to add that this code calls when I close the application.
Code:
Private Sub cerrarForms()
Try
frmListados.Instance.Close()
For Each frmC As Form In Me.MdiChildren
frmC.Close()
frmC.Dispose()
Next
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Though it never jumps the error message in this point
Re: [02/03] Sometimes my app displays error when it exits
anna, does your form run any code when it is closed? In the form closing or form closed events? You must be accessing something in your code that has already been disposed of, which makes it nothing.
Re: [02/03] Sometimes my app displays error when it exits
Sorry, I did not see that you had answered. I added code at the previous post and I have this code when the user push X button:
Code:
Protected Overrides Sub WndProc(ByRef m As Message)
Try
If m.Msg = Convert.ToInt32("0x0112", 16) Then
If IntPtr.op_Equality(m.WParam, IntPtr.op_Explicit(Convert.ToInt32("0xF060", 16))) Then
gCancelQuitParent = True
End If
End If
Catch ex As Exception
End Try
MyBase.WndProc(m)
End Sub
Re: [02/03] Sometimes my app displays error when it exits
Are you using more than one process thread that you know of? If you try to close with a background thread still running, you may get an error on shutdown.
Re: [02/03] Sometimes my app displays error when it exits
Try this:
Code:
Protected Overrides Sub WndProc(ByRef m As Message)
Try
If m.Msg = Convert.ToInt32("0x0112", 16) Then
If IntPtr.op_Equality(m.WParam, IntPtr.op_Explicit(Convert.ToInt32("0xF060", 16))) Then
gCancelQuitParent = True
End If
End If
MyBase.WndProc(m)
Catch ex As Exception
End Try
End Sub
If it's tripping up here on application exit, then just dump the exception since you're disposing the whole program anyways. I admit I've done a similar method in my programming as well.
Re: [02/03] Sometimes my app displays error when it exits
what exactly are you checking for with that wndproc code???
@Jenner, if you put the MyBase.WndProc(m) inside the try, and the code above it fails, then windows messages are going to get thrown out, which will produce undesired results. I think it does belong outside the try. Although as I mentioned above, I want to know what this wndproc override is for in the first place.
Re: [02/03] Sometimes my app displays error when it exits
What it sounds like, and the reason I've used it in the past, is to detect when windows is told to shutdown; which is going to automatically tell the program to exit whether it wants to or not. If you have an on-closing statement that pops up an "are you sure you want to close" dialog... and you tell the computer to shut-down, it'll hang with that dialog open until you close it. The computer won't shut down until you do. This little intercept throws a flag that says "I'm being forced to shut down! Don't fire the Messagebox!"
While I don't like to bypass a MyBase call in an Override, when I used it, I had the EXACT same problem until I just moved it within the Try statement. It was calling the function after my program was disposed and the override was trying to reference objects that didn't exist anymore. It errored with null reference unhandled exceptions on normal program shutdown because of that override. I remove the override, it always exits cleanly. I need the override though for the reason I mentioned above. I'm betting this is the EXACT same problem I was having.
I've actually found the instance where I've done this:
Code:
Public Const WM_QUERYENDSESSION As Integer = 17
Protected Overrides Sub WndProc(ByRef m As Message)
'This override is here to catch when the computer is being told to shut-down and allow it.
Try
If (m.Msg.Equals(WM_QUERYENDSESSION)) Then
booCloseNow = True
booCloseSilent = True
End If
MyBase.WndProc(m)
Catch ex As Exception
'This frequently errors on shutdown of the program. Ignore it.
'LogError(Me, "Shutdown Override", ex)
End Try
End Sub
If you got a better idea, I'm all ears. I'd love to hear it because I've never been happy with this fix either.
Re: [02/03] Sometimes my app displays error when it exits
Originally Posted by kleinma
what exactly are you checking for with that wndproc code???
similar to Jenner:
..is to detect when windows is told to shutdown; which is going to automatically tell the program to exit whether it wants to or not. If you have an on-closing statement that pops up an "are you sure you want to close" dialog...
Re: [02/03] Sometimes my app displays error when it exits
I had to go back and check (been sometime since I have used .NET 1.1)
2.0+ framework versions actually report the reason why a form is closing in the formclosing event. This wasn't available in 1.1
What results do you get if you move the processing call before your code?
Does it fire code prior to your booleans being set to true where you need them to be false?
Code:
Public Const WM_QUERYENDSESSION As Integer = 17
Protected Overrides Sub WndProc(ByRef m As Message)
'This override is here to catch when the computer is being told to shut-down and allow it.
MyBase.WndProc(m)
Try
If (m.Msg.Equals(WM_QUERYENDSESSION)) Then
booCloseNow = True
booCloseSilent = True
End If
Catch ex As Exception
'This frequently errors on shutdown of the program. Ignore it.
'LogError(Me, "Shutdown Override", ex)
End Try
End Sub
Re: [02/03] Sometimes my app displays error when it exits
I know my program that has the problem in it uses .NET 2.0. Is there a better way to do it?
Unfortunately, I'm now out of the office till Monday, so I won't be able to test that move till then. How about you Anna, does moving it to the beginning of the statement stop the error for you?
A little background on my application though for understanding. The app that I needed to use this in controlled some automation equipment. One function was to occasionally cycle a series of pumps to prevent the lines from drying out. If the program wasn't running, it couldn't do this, so I added an "are you sure" type message.
The pump cycle routine was actually added to a second looping thread. I used the "Form_Closing" event on the main MDI form to cleanly exit out of these threads, so the program took a few seconds to finish up everything properly on shutdown.
The override was to prevent the popup when the machine was told to shutdown so it would shutdown w/o interruption. booCloseSilent was to prevent the popup. booCloseNow was a flag if the option "close minimizes to system tray" was turned on. Hitting "X" would minimize it to the tray. To truely exit, you needed to right-click and hit Exit from there.
Setting them both to true told Form_Closing "Do not call the Are you Sure popup and close even if you've been told to minimize on exit."
Dunno if that helps any, but it's why I first asked Anna if she had any other process threads. I had a lot of similar issues getting them to exit gracefully.
This is a big bonus to me too if you can help figure this old bug out. I still got the code marked as "HACK" in my comments log, even though my program is like 2 years old since it's last revision.
Re: [02/03] Sometimes my app displays error when it exits
In my case when I exit it breaks in this point:
Code:
Module mdlStart
Public Sub Main()
Try
SubMain()
Catch ex As Exception
HandleUnhandledException(ex)
End Try
End Sub
Public Sub SubMain()
AddHandler System.AppDomain.CurrentDomain.UnhandledException, AddressOf OnUnhandledException
Application.Run(New frmLogin)
End Sub
Private Sub OnUnhandledException(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
Try
HandleUnhandledException(e.ExceptionObject)
Catch ex As Exception
End Try
End Sub
Private Sub HandleUnhandledException(ByVal o As Object)
If o Is Nothing Then Return
Try
Dim e As Exception = DirectCast(o, Exception)
MessageBox.Show(e.StackTrace, "Unhandled exception.")
Catch ex As Exception
MessageBox.Show(ex.Message, "Unhandled exception.")
Finally
Application.Exit()
End Try
End Sub
End Module
Re: [02/03] Sometimes my app displays error when it exits
I tried this code too and it continues displaying the same error
Originally Posted by Jenner
Try this:
Code:
Protected Overrides Sub WndProc(ByRef m As Message)
Try
If m.Msg = Convert.ToInt32("0x0112", 16) Then
If IntPtr.op_Equality(m.WParam, IntPtr.op_Explicit(Convert.ToInt32("0xF060", 16))) Then
gCancelQuitParent = True
End If
End If
MyBase.WndProc(m)
Catch ex As Exception
End Try
End Sub
If it's tripping up here on application exit, then just dump the exception since you're disposing the whole program anyways. I admit I've done a similar method in my programming as well.
Re: [02/03] Sometimes my app displays error when it exits
Originally Posted by anna7
Hi,
I tried the code of the message nº 21, but then it starts closing the child forms before the user decides if he wants to exit of the application.
Regards
Yea, I tried putting it before the check routine as well and it closes all the children as well. It seems like it calls Form_Closing immediately after the MyBase call.
I'm going to try that .NET 2.0 closing reason for this old program. That's good info, wish I knew it at the time I wrote that thing and glad I now know it if I ever have to intercept the form-close again.
Anna, I'm sorry but I was sure that function was it. When I took that function out of my program originally, it stopped giving me errors on shutdown.
I'd recommend a "process of elimination" strategy at this point for tracking it down. Start commenting out functions until the errors stop coming. That should narrow it down to exactly where the error is being thrown on shutdown. When you eliminate enough of your program to halt the errors, start, from the top again, re-enabling functions until you only have the last one you disabled still disabled; or until it starts throwing errors again.
This has got to be one of the most aggravating types of errors there is, since the debugger is little to no help on the matter.
Re: [02/03] Sometimes my app displays error when it exits
But there is no any way of preventing it displays this final error message so ugly? Because only it happens when the user closes the program. While he works with it the error does not appear.
Re: [02/03] Sometimes my app displays error when it exits
Hi,
I could have verified that in windows xp professional not displays this error. The error appears often in windows xp home and w98.
A user, with w98, has said to me that should confirm to him that this is not dangerous for his data. I think that not because this error only happens when he exits from the app and not when he works with the data. Might it be dangerous for his data?
Re: [02/03] Sometimes my app displays error when it exits
I think I see where the error comes from. In post#7, you have the code for the constructor like this:
Code:
Dim sqlStr As String = ""
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
DevExpress.UserSkins.BonusSkins.Register()
DevExpress.Skins.SkinManager.EnableFormSkins()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
You should always put ALL of your customized code after the InitializeComponent call. That is, the constructor should be like this
Code:
Dim sqlStr As String = ""
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
DevExpress.UserSkins.BonusSkins.Register()
DevExpress.Skins.SkinManager.EnableFormSkins()
End Sub
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it. - Abraham Lincoln -