Null reference exception in the close function - Dispose pattern

Wojciech Rajchel 21 Reputation points
2023-03-01T16:31:45.6633333+00:00

Hello

I am using dispose pattern to clear Postgresql resources. Occasionally following exception is thrown. Do you have ideas what can cause this exception?

Tools.DebugOut - 01.03.2023 16:44:14:004 - err - SystemClass.Destroy - PMem : 15045.852/32767.551 - VMem : 132099361.086/134217727.875 - de-CH -  - trace: System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
   bei Npgsql.NpgsqlConnection.Close(Boolean wasBroken)
   bei Npgsql.NpgsqlConnection.Dispose(Boolean disposing)
   bei System.ComponentModel.Component.Dispose()
   bei DataFactory.Dispose(Boolean disposing)
   bei SystemClass.Destroy() - 
 Protected Overrides Sub Finalize()
        Dispose(False)
    End Sub

    Public Sub Dispose() Implements IDisposable.Dispose
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
    Protected Overridable Sub Dispose(disposing As Boolean)
        If _disposed Then
            Exit Sub
        End If
        If disposing Then
            Dim conn As Npgsql.NpgsqlConnection = CType(Me.m_Conn, Npgsql.NpgsqlConnection)

            If conn IsNot Nothing Then
                conn.Dispose()
                conn = Nothing
            End If

            Dim trans As Npgsql.NpgsqlTransaction = CType(Me.m_Transaction, Npgsql.NpgsqlTransaction)

            If trans IsNot Nothing Then
                trans.Dispose()
                trans = Nothing
            End If
        End If
        _disposed = True
    End Sub

    Private _disposed As Boolean
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,718 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.