Home > Software engineering >  Winform getting an error of CallbackOnCollectedDelegate
Winform getting an error of CallbackOnCollectedDelegate

Time:04-20

An exception was thrown when trying to clear datatimepicker.

A callback was made on a garbage collected delegate of type 'System.Windows.Forms!System.Windows.Forms.NativeMethods WndProc::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called.'

The error happens where the arrow is below -->

#Region " Load Controls to Clear "
    Private Sub ControlsClear()
        Try

            'Load blank defaults
            Dim a As Control
            For Each a In Me.Controls
                If TypeOf a Is TextBox Then
                    a.Text = Nothing
                End If
                If TypeOf a Is ComboBox Then
                    If Not a.Name.Contains("PrinterComboBox") Then
                        a.Text = Nothing
                    End If
                End If
                If TypeOf a Is DateTimePicker Then
                    Dim dtp As DateTimePicker = CType(a, DateTimePicker)
                    dtp.Format = DateTimePickerFormat.Custom
                    dtp.CustomFormat = " "
                End If
            Next
            For Each GroupBoxCntrol As Control In Me.Controls
                If TypeOf GroupBoxCntrol Is GroupBox Then
                    If Not GroupBoxCntrol.Name.Contains("Search") Then
                        For Each cntrl As Control In GroupBoxCntrol.Controls
                            If TypeOf cntrl Is TextBox Then
                                cntrl.Text = Nothing
                            End If
                            If TypeOf cntrl Is ComboBox Then
                                If Not cntrl.Name.Contains("PrinterComboBox") Then
                                    cntrl.Text = Nothing
                                End If
                            End If
                            If TypeOf cntrl Is DateTimePicker Then
                                Dim dtp As DateTimePicker = CType(cntrl, DateTimePicker)
                       ------>  dtp.Format = DateTimePickerFormat.Custom
                                dtp.CustomFormat = " "
                            End If
                        Next

                    End If
                End If

            Next

        Catch ex As Exception
            _log.Error(ex.ToString & vbCrLf & ex.StackTrace.ToString)
            MessageBox.Show(ex.ToString, "", MessageBoxButtons.OK, MessageBoxIcon.Error)

        Finally
            Me.Invalidate()

        End Try
    End Sub
#End Region

After the continue is hit then I have a exception unhandled error

System.NullReferenceException: 'Object reference not set to an instance of an object.'

Any idea why my winforms is throwing this error?

I do have this at the top Implements IDisposable

thanks in advance

CodePudding user response:

Come to find out I solved my own issue. It had to do with I had two subs with the same code trying to clear and set the formatting so I was trying to format the same fields twice.

I added the following to the code to check if the formatting was already set

                        If dtp.CustomFormat <> " " Then
                            dtp.Format = DateTimePickerFormat.Custom
                            dtp.CustomFormat = " "
                        End If
  • Related