Willy-Kimura / SharpClipboard

A library for anonymously monitoring clipboard entries.
183 stars 34 forks source link

Unhandled Exception after cutting text #22

Closed wy-luke closed 1 year ago

wy-luke commented 3 years ago

System.ObjectDisposedException: '无法访问已释放的对象。 ObjectDisposed_ObjectName_Name'

This exception was originally thrown at this call stack: [External Code]

System.ObjectDisposedException HResult=0x80131622 Message=无法访问已释放的对象。 ObjectDisposed_ObjectName_Name Source=System.Windows.Forms StackTrace: at System.Windows.Forms.Control.CreateHandle() at System.Windows.Forms.Form.CreateHandle() at System.Windows.Forms.Control.get_Handle() at System.Windows.Forms.Control.SetVisibleCore(Boolean value) at System.Windows.Forms.Form.SetVisibleCore(Boolean value) at System.Windows.Forms.Control.Show() at WK.Libraries.SharpClipboardNS.SharpClipboard.StartMonitoring() at WK.Libraries.SharpClipboardNS.SharpClipboard.OnLoad(Object sender, EventArgs e) at System.Windows.Forms.Timer.OnTick(EventArgs e) at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) at System.Windows.Application.RunDispatcher(Object ignore) at System.Windows.Application.RunInternal(Window window) at System.Windows.Application.Run(Window window) at System.Windows.Application.Run() at CopyPlusPlus.App.Main()

This exception was originally thrown at this call stack: [External Code]

有关调用实时(JIT)调试而不是此对话框的详细信息,
请参见此消息的结尾。
************** 异常文本 **************
System.ObjectDisposedException: 无法访问已释放的对象。
对象名:“ClipboardHandle”。
   在 System.Windows.Forms.Control.CreateHandle()
   在 System.Windows.Forms.Form.CreateHandle()
   在 System.Windows.Forms.Control.get_Handle()
   在 System.Windows.Forms.Control.SetVisibleCore(Boolean value)
   在 System.Windows.Forms.Form.SetVisibleCore(Boolean value)
   在 System.Windows.Forms.Control.Show()
   在 WK.Libraries.SharpClipboardNS.SharpClipboard.StartMonitoring()
   在 WK.Libraries.SharpClipboardNS.SharpClipboard.OnLoad(Object sender, EventArgs e)
   在 System.Windows.Forms.Timer.OnTick(EventArgs e)
   在 System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
   在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
************** 已加载的程序集 **************
mscorlib
    程序集版本:4.0.0.0
    Win32 版本:4.8.3752.0 built by: NET48REL1
    基本代码:file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/mscorlib.dll
----------------------------------------
Copy++
    程序集版本:1.0.0.0
    Win32 版本:1.0.0.0
    基本代码:file:///D:/soft/Copy++.exe
----------------------------------------
PresentationFramework
    程序集版本:4.0.0.0
    Win32 版本:4.8.3752.0
    基本代码:file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/PresentationFramework/v4.0_4.0.0.0__31bf3856ad364e35/PresentationFramework.dll
----------------------------------------
PresentationCore
    程序集版本:4.0.0.0
    Win32 版本:4.8.3752.0 built by: NET48REL1
    基本代码:file:///C:/windows/Microsoft.Net/assembly/GAC_64/PresentationCore/v4.0_4.0.0.0__31bf3856ad364e35/PresentationCore.dll
----------------------------------------
System
    程序集版本:4.0.0.0
    Win32 版本:4.8.3752.0 built by: NET48REL1
    基本代码:file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
WindowsBase
    程序集版本:4.0.0.0
    Win32 版本:4.8.3752.0 built by: NET48REL1
    基本代码:file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/WindowsBase/v4.0_4.0.0.0__31bf3856ad364e35/WindowsBase.dll
----------------------------------------
System.Core
    程序集版本:4.0.0.0
    Win32 版本:4.8.3752.0 built by: NET48REL1
    基本代码:file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System.Xaml
    程序集版本:4.0.0.0
    Win32 版本:4.8.3752.0 built by: NET48REL1
    基本代码:file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/System.Xaml/v4.0_4.0.0.0__b77a5c561934e089/System.Xaml.dll
----------------------------------------
System.Configuration
    程序集版本:4.0.0.0
    Win32 版本:4.8.3752.0 built by: NET48REL1
    基本代码:file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
    程序集版本:4.0.0.0
    Win32 版本:4.8.3752.0 built by: NET48REL1
    基本代码:file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
ToggleSwitch
    程序集版本:1.2.0.0
    Win32 版本:1.0.0.0
    基本代码:file:///D:/soft/Copy++.exe
----------------------------------------
PresentationFramework.Aero2
    程序集版本:4.0.0.0
    Win32 版本:4.8.3752.0 built by: NET48REL1
    基本代码:file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/PresentationFramework.Aero2/v4.0_4.0.0.0__31bf3856ad364e35/PresentationFramework.Aero2.dll
----------------------------------------
SharpClipboard
    程序集版本:3.5.2.0
    Win32 版本:1.0.0.0
    基本代码:file:///D:/soft/Copy++.exe
----------------------------------------
System.Windows.Forms
    程序集版本:4.0.0.0
    Win32 版本:4.8.3752.0 built by: NET48REL1
    基本代码:file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    程序集版本:4.0.0.0
    Win32 版本:4.8.3752.0 built by: NET48REL1
    基本代码:file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
PresentationCore.resources
    程序集版本:4.0.0.0
    Win32 版本:4.8.3752.0 built by: NET48REL1
    基本代码:file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/PresentationCore.resources/v4.0_4.0.0.0_zh-Hans_31bf3856ad364e35/PresentationCore.resources.dll
----------------------------------------
System.Windows.Controls.Ribbon
    程序集版本:4.0.0.0
    Win32 版本:4.8.3752.0
    基本代码:file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Controls.Ribbon/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Controls.Ribbon.dll
----------------------------------------
PresentationFramework-SystemXml
    程序集版本:4.0.0.0
    Win32 版本:4.8.3752.0
    基本代码:file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/PresentationFramework-SystemXml/v4.0_4.0.0.0__b77a5c561934e089/PresentationFramework-SystemXml.dll
----------------------------------------
UIAutomationTypes
    程序集版本:4.0.0.0
    Win32 版本:4.8.3752.0 built by: NET48REL1
    基本代码:file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/UIAutomationTypes/v4.0_4.0.0.0__31bf3856ad364e35/UIAutomationTypes.dll
----------------------------------------
UIAutomationProvider
    程序集版本:4.0.0.0
    Win32 版本:4.8.3752.0 built by: NET48REL1
    基本代码:file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/UIAutomationProvider/v4.0_4.0.0.0__31bf3856ad364e35/UIAutomationProvider.dll
----------------------------------------
mscorlib.resources
    程序集版本:4.0.0.0
    Win32 版本:4.8.3752.0 built by: NET48REL1
    基本代码:file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/mscorlib.resources/v4.0_4.0.0.0_zh-Hans_b77a5c561934e089/mscorlib.resources.dll
----------------------------------------
PresentationFramework-SystemDrawing
    程序集版本:4.0.0.0
    Win32 版本:4.8.3752.0
    基本代码:file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/PresentationFramework-SystemDrawing/v4.0_4.0.0.0__b77a5c561934e089/PresentationFramework-SystemDrawing.dll
----------------------------------------
System.Windows.Forms.resources
    程序集版本:4.0.0.0
    Win32 版本:4.8.3752.0 built by: NET48REL1
    基本代码:file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms.resources/v4.0_4.0.0.0_zh-Hans_b77a5c561934e089/System.Windows.Forms.resources.dll
----------------------------------------
************** JIT 调试 **************
要启用实时(JIT)调试,
该应用程序或计算机的 .config 文件(machine.config)的 system.windows.forms 节中必须设置
jitDebugging 值。
编译应用程序时还必须启用
调试。
例如:
<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>
启用 JIT 调试后,任何未经处理的异常
都将被发送到在此计算机上注册的 JIT 调试程序,
而不是由此对话框处理。
MatrixDoge commented 3 years ago

I assume that this could happen if you call StartMonitoring() after calling StopMonitoring(). So this cannot be restarted once stopped as the internal code does not recreate the ClipboardHandle.

For sure we can fix this by making StartMonitoring() ensure the ClipboardHandle creation. Or we can just re instantiate a new SharpClipboard instance.

I wouldn't worry about this too much, And I guess it has nothing to do with cutting text. Copying text or anything need to access the handle could also trigger this issue.

wy-luke commented 3 years ago

I assume that this could happen if you call StartMonitoring() after calling StopMonitoring(). So this cannot be restarted once stopped as the internal code does not recreate the ClipboardHandle.

For sure we can fix this by making StartMonitoring() ensure the ClipboardHandle creation. Or we can just re instantiate a new SharpClipboard instance.

I wouldn't worry about this too much, And I guess it has nothing to do with cutting text. Copying text or anything need to access the handle could also trigger this issue.

yeah, you're right. In my app, I need to monitor the clipboard, once it changed, I make some changes to the text and make the new text to the clipboard. If I don't stop monitor before set the new text, it will be in endless loop.

But I recreate a new instance before I start monitoring again, I don't know if that helps.

Now I have switch to another method to monitor the clipboard and the annoying error dialog don't show up any more.🤣But the problem is not be solved completely. Because I dont konw why even I -= the ClipboardChanged event before set the modified text to clipboard, it still invoke the event once more. And sometimes it will invoke just once.😭😭😭

Can you please help me? thanks so much!