ILSpy icon indicating copy to clipboard operation
ILSpy copied to clipboard

Fix/issue 3221 argument out of range exception

Open 1samrand opened this issue 1 year ago • 2 comments

issue :

Solution

  • Handle out of bound array index

1samrand avatar Aug 26 '24 20:08 1samrand

This looks like fixing the symptom, but not the root cause - because some code must be picking an out-of-bounds index (maybe a race condition or some other problem). Preferably we should try to debug/find the actual culprit that hands over the out of bounds index.

christophwille avatar Aug 27 '24 16:08 christophwille

Thank you for your feedback , I understand your concern about addressing the root cause rather than just the symptom. I'll take some time to investigate further to identify the underlying issue that might be causing the out-of-bounds index

1samrand avatar Aug 27 '24 17:08 1samrand

Hi @christophwille,

After further investigation, it seems the issue is related to OnRender from WPF for a disconnected item. In this case, the Node is not null but has a count of 0.

However, as mentioned earlier, I'm still unsure why this is happening. If you have any insights, I would appreciate it if you could share with me.

The orange area highlights the specific case where this occurs (which I added to check):

image

1samrand avatar Sep 08 '24 09:09 1samrand

diff --git a/ILSpy/Controls/TreeView/SharpTreeNodeView.cs b/ILSpy/Controls/TreeView/SharpTreeNodeView.cs
index a7cca8838..d144ca5c5 100644
--- a/ILSpy/Controls/TreeView/SharpTreeNodeView.cs
+++ b/ILSpy/Controls/TreeView/SharpTreeNodeView.cs
@@ -67,7 +67,6 @@ static SharpTreeNodeView()
                public override void OnApplyTemplate()
                {
                        base.OnApplyTemplate();
-                       LinesRenderer = Template.FindName("linesRenderer", this) as LinesRenderer;
                        UpdateTemplate();
                }

@@ -148,6 +147,7 @@ void OnIsEditingChanged()

                void UpdateTemplate()
                {
+                       LinesRenderer = Template.FindName("linesRenderer", this) as LinesRenderer;
                        var spacer = Template.FindName("spacer", this) as FrameworkElement;
                        spacer.Width = CalculateIndent();

@1samrand I found that this change fixes the issue and is likely the root cause. Can you confirm?

siegfriedpammer avatar Sep 08 '24 12:09 siegfriedpammer

@christophwille I have tested by this change, but the ArgumentOutOfRangeException still occurs when accessing list[index].

1samrand avatar Sep 11 '24 18:09 1samrand

How did you test it? I would like to take a look as well. I am following the steps given in the issue and I can no longer reproduce the error.

siegfriedpammer avatar Sep 12 '24 15:09 siegfriedpammer

I have tested the scenario described here: https://github.com/icsharpcode/ILSpy/issues/3221#issuecomment-2310906975. It seems that the issue persists, so perhaps there have been some changes in your branch that could be affecting the results.

If you're working on a specific branch, could you please share it with me so I can clone and test it as well?

1samrand avatar Sep 13 '24 14:09 1samrand

Thanks for getting back to me. You are right, the patch I posted above does not fix the issue. Yeah, unfortunately there is no "nice" fix for the weird issues with LinesRenderer.

An idea for a simple non-invasive fix I had previously was:

image

We can either do your second suggestion (the additional sanity check in OnRender) or go with my fix... not sure which is better.

siegfriedpammer avatar Sep 13 '24 18:09 siegfriedpammer

I have tested the changes you made, and I can confirm that the issue no longer occurs. I also prefer to use your solution moving forward

1samrand avatar Sep 14 '24 09:09 1samrand