DeepCloner icon indicating copy to clipboard operation
DeepCloner copied to clipboard

InvalidAccessViolation, other exceptions and slow down

Open yarecky1 opened this issue 5 years ago • 3 comments

I discovered several issues using your library.

Situation 1: I have a parent form with datagridview. Mouse doubleclick on datagridview row retrieves binded record and open new child form that presents details of this record. In that child form I can click Save button to save updated record in database, after which child form is closed/disposed and parent form updates datagridview. In child form before saving to database I do deepclone using your library:

I removed everything unrelated.

' Property in form
Property Audit As AuditDefinition

' Entrypoint to form
Public Sub New(audit As AuditDefinition)
    InitializeComponent()
    Me.Audit = audit
End Sub

' Somewhere in form I await saving function and if return true I close the form
If Await SaveInDbAsync() then Close()

' Saving in database
Private Async Function SaveInDbAsync() As Task(Of Boolean)
    ' Cloning Audit property in case of saving in database failure (then restore changed child properties)
    cAudit = Audit.DeepClone
   ' Rest of code
End Function

Child form is properly initialized and destroyed with using/end using clause from parent form.

Problem: Each next time when child form in created (with new record Audit) and deepclone is performed it is slower and slower. Example times: 132 ms (first use of deepclone in application) 15 ms 25 ms 53 ms 163 ms 291 ms 569 ms

What do you think about it?

Situation 2: Since I switch making clone objects from my implementation to yours my application started to close unexpectedly with many different exceptions, mainly System.AccessViolationException in random System.xxxx.dll libraries.

For example during debugging situation 1 above I made a simple loop making cloning 10 times the same object receiving each time the same time (opposite to situation above where time is longer and longer with each call) but just after exiting the loop application receives AccessViolationException.

Dim cAudit As AuditDefinition

For i = 0 To 9
    Debug.WriteLine(i)
    cAudit = Audit.DeepClone
Next

Result:

0
1
2
3
4
5
6
7
8
Exception thrown: 'System.Runtime.InteropServices.SEHException' in System.Windows.Forms.dll
9
System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.

And debugger can move to next lines of code but soon application is closed with

Cross-thread operation not valid: Control '{0}' accessed from a thread other than the thread it was created on.
Or
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Exception occurs randomly after 4 to 9 cloning operations. Moving operation to non-async function doesn't help.

What do you think about it?

yarecky1 avatar Jun 29 '19 16:06 yarecky1

It seems, Windows.Forms has a lot of internal dependencies and every deep clone (it a deep clone) tries to clone all related objects. Library trying to exclude some handles and native objects, which is dangerous to clone, but for Windows.Forms it seems it does not exclude it correctly.

I'll try to objects need to be excluded for this to avoid these problems. Thanks for issue.

force-net avatar Jun 29 '19 18:06 force-net

Hi, have you solve the issue?

yarecky1 avatar Aug 24 '19 20:08 yarecky1

Sorry, no. I have no time now to investigate problem. When I'll find any time to develop, I'll check this situation

force-net avatar Aug 25 '19 08:08 force-net