Weird problem with List(Of T) declaration and .add
I have this really weird problem in my code (.Net Framework 3.5). So, in a normal VB.NET program, I would do something like this :
Dim MyList As New List(Of Integer)
MyList.Add(3)
MyList.Add(5)
MyList.Add(9)
MyList.Add(17)
And if I put a "break" rigth after
Dim MyList As New List(Of Integer)
and put the mouse over "MyList" while code is paused, I should get : Count=0. However, I have a sub where I create a list just that way and get (when I break after it) : "Count = Property evaluation failed".
It seems like if my list has not enough space to hold the data... Here's the complete code I use to try out everything (by the way, the code itself doesn't really make sense since it's a part of my project). With it I can create the "weird" behavior and anyone can test this. Here's the code (sorry the Code Block button isn't working) :
Structure lPossibilitiesOutputStruct
Dim Pinion As GearOutputStruct
Dim Gear As GearOutputStruct
Dim Forces As ForcesStruct
Dim CenterDistance As Double
Dim Pitch As Double
Dim lStagePossibilities As List(Of lPossibilitiesOutputStruct)
End Structure
Structure GearOutputStruct
Dim TeethNbr As Integer
Dim RPM As Double
Dim FaceWidth As Double
Dim OutsideDiameter As Double
Dim Addendum As Double
Dim WholeDepth As Double
Dim OperatingPitchDiameter As Double
Dim OverPinData As OverPinOutputStruct
Dim SpanData As SpanOutputStruct
Dim AllowableBendingPower As Double
Dim AllowablePittingPower As Double
End Structure
Structure OverPinOutputStruct
Dim PinDiameter As Double
Dim OverpinMeasurement As Double
End Structure
Structure SpanOutputStruct
Dim TeethNbr As Integer
Dim SpanMeasurement As Double
End Structure
Structure ForcesStruct
Dim GearSetAxialForce As Double
Dim GearSetRadialForce As Double
Dim GearSetTangentialForce As Double
End Structure
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim lStagePossibilities As New List(Of lPossibilitiesOutputStruct)
Dim MyList As New List(Of Integer)
MyList.Add(3)
MyList.开发者_如何学运维Add(5)
MyList.Add(9)
MyList.Add(17)
lStagePossibilities = lGeneratePossibilities(Nothing, Nothing, 3, 1)
End Sub
Private Function lGeneratePossibilities(ByVal ActualStage As lPossibilitiesOutputStruct, ByVal CSL As List(Of Integer), ByVal MaxStage As Integer, ByVal CumulatedRatio As Double) As List(Of lPossibilitiesOutputStruct)
Return Nothing
End Function
All I did is create a new project with a form and created that code. There's nothing else in the project. I played with the "targeted CPU" and that's all...
If I run that program with compile option to use x86 CPU, and put a break on the "lStagePossibilities = ..." I get the "Count = Property evaluation failed" when i put my mouse over MyList. If I run it with the x64 CPU, then everything is working fine... ("Count = 4"). If I go back with the x86 CPU, I get back the error. Even worst, if I comment the line "Dim Pinion as GearOutputStruct" or the line "Dim Gear as GearOutputStruct" or the line "Dim Forces as ForcesStruct" in the lPossibilitiesOutPutStruct structure declaration, then everything works fine with x86 CPU...
Could it be related to a kind of maximum size of a list ???
Thank you !
Jean
I bet if you changed some of the Strucures into Classes the problem will go away. It is a well known bug in Visual Studio that when Structures get large and try to allocate a lot of local memory things go bad in the debugger. Some of the structures contain other structures and the memory adds up. I bet in x64 the size of the structs gets bigger, due to packing.
To quote some MSDN blogs
Just so others know too. This issue was because of a large struct in my code as well. I converted it to a class and I could read the values of variables hovering over the objects. It worked in some functions and classes, but the error message where the large struct was contained had the error "Cannot evaluate expression because a thread is stopped at a point where garbage collection is impossible, possibly because the code is optimized." and it went away upon making the change. Hope it helps and confirms the solution.
To clarify, what is too big. My Struct has (rather had) ...
- 55 public variables
- A constructor with 6 parameters
- Two public functions (one of those was small and one was large, about 200 lines of code)
I believe structs are intended to be used as small containers of straight forward, simple, cohesive data and functionality, and I would agree that this one got too far out of hand. It started out small, of course, and the needs ballooned. Now that it's in a class where the cohesive data and functionality is now contained there, I can sleep at night. =)
It took me all of 5 minutes to convert it to a class. Easy enough.
精彩评论