8 Replies Latest reply on Apr 16, 2018 12:17 PM by Jacob Corder

    Getting a "Memory is locked" exception in VB.NET

    J. R.

      Hello,

       

      I am transferring a big macro project from VBA to VB.NET (working with Visual Studio). Since the languages are almost identical, most things work right out of the box.

      However, a certain procedure that uses a swModeler.CreateBodyFromBox3 method, using a 8 Double array as an argument, throws this exception:

       

      An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in Hydrostatics Calculator.exe

       

      Additional information: Memory is locked. (Exception from HRESULT: 0x8002000D (DISP_E_ARRAYISLOCKED))

       

      This is the part of the code that is relevant to this error:

       

              swModeler = swApp.GetModeler
              vBox = BodyCopy.GetBodyBox()
              width = BoatWidth * 10
              height = 0.001
              length = BoatLength * 10
              dBoxData(0) = (vBox(3) + vBox(0)) / 2 : dBoxData(1) = 0 : dBoxData(2) = (vBox(5) + vBox(2)) / 2
              dBoxData(3) = 0 : dBoxData(4) = 1 : dBoxData(5) = 0 'vector
              dBoxData(6) = width : dBoxData(7) = length : dBoxData(8) = height 'size
              swCutBody = swModeler.CreateBodyFromBox3(dBoxData)
      
      

       

      The exception happens on line 9. I can't find much about this issue on google. This code worked fine when I used it in VBA macro, but doesn't work anymore in VB.NET compilation. I reviewed the examples of CreateBodyFromBox3 in the official documentation, but I can't seem to find any relevant difference between the sample code, and my code.

       

      I would appreciate any suggestions. Thank you for your time.

        • Re: Getting a "Memory is locked" exception in VB.NET
          Jacob Corder

          what is dboxdata declared as?

          it should be Dim dboxdata(8) as double

            • Re: Getting a "Memory is locked" exception in VB.NET
              J. R.

              That's exactly how it's declared...

                • Re: Getting a "Memory is locked" exception in VB.NET
                  Jacob Corder

                  try this

                  just an idea.

                  swCutBody = swModeler.CreateBodyFromBox3(CObj(dBoxData))

                   

                  if that doesnt work.

                   

                  debug.print all the values to make sure that they are valid.  if there is a Double.NAN possibly in there it may be causing the issue.

                    • Re: Getting a "Memory is locked" exception in VB.NET
                      J. R.

                      Tried CObj, didn't change anything... Yes, all the values in the array are correct, no NaN's. That was the first thing I checked.

                       

                      I am unsure if the locked memory exception refers to that array. Maybe it refers to SolidWorks memory, as in being unable to write it? Functions in my code that only read values work perfectly, but CreateBodyFromBox3 is meant to create a virtual body...

                        • Re: Getting a "Memory is locked" exception in VB.NET
                          Jim Sculley

                          J. R. wrote:

                           

                          Tried CObj, didn't change anything... Yes, all the values in the array are correct, no NaN's. That was the first thing I checked.

                           

                          I am unsure if the locked memory exception refers to that array. Maybe it refers to SolidWorks memory, as in being unable to write it? Functions in my code that only read values work perfectly, but CreateBodyFromBox3 is meant to create a virtual body...

                          It's a bug on the SW side:

                           

                          Use CreateBodyFromBox (without the '3') and it doesn't throw an exception.

                          • Re: Getting a "Memory is locked" exception in VB.NET
                            Jacob Corder
                                Public Shared Function CreateBoxBody(ByVal ISwApp As SldWorks, ByVal BoxLengthMetric As Double, ByVal BoxWidthMetric As Double, ByVal BoxHeightMetric As Double, ByVal StartX As Double, ByVal StartY As Double, ByVal StartZ As Double) As Body2
                                    CreateBoxBody = Nothing
                                    If IsNothing(ISwApp) = False Then
                                        Dim SwModeler As Modeler = CType(ISwApp.GetModeler, Modeler)
                                        If IsNothing(SwModeler) = False Then
                                            If IsNothing(SwModeler) = False Then
                                                Dim BoxDimArray(8) As Double
                                                BoxDimArray(0) = StartX
                                                BoxDimArray(1) = (StartY + (BoxHeightMetric / 2)) * -1
                                                BoxDimArray(2) = (StartZ + (BoxWidthMetric / 2)) * -1
                                                If StartX < BoxLengthMetric Then
                                                    BoxDimArray(3) = 1
                                                ElseIf StartX > BoxLengthMetric Then
                                                    BoxDimArray(3) = -1
                                                End If
                                                BoxDimArray(4) = 0
                                                BoxDimArray(5) = 0
                                                BoxDimArray(6) = BoxWidthMetric
                                                BoxDimArray(7) = BoxHeightMetric
                                                BoxDimArray(8) = BoxLengthMetric
                                                CreateBoxBody = SwModeler.CreateBodyFromBox3(BoxDimArray)
                            
                            
                                                'If IsNothing(CreateBoxBody) Then
                                                '    If BoxDimArray(0) > BoxDimArray(6) Then
                                                '        If BoxDimArray(3) <> 1 Then
                                                '            BoxDimArray(3) = 1
                                                '            CreateBoxBody = SwModeler.CreateBodyFromBox3(BoxDimArray)
                                                '        End If
                                                '    Else
                                                '        If BoxDimArray(3) <> -1 Then
                                                '            BoxDimArray(3) = -1
                                                '            CreateBoxBody = SwModeler.CreateBodyFromBox3(BoxDimArray)
                                                '        End If
                                                '    End If
                                                'End If
                                            End If
                                        End If
                                               End If
                                End Function
                            

                            this method i have been using for years and never fails yet.

                             

                            you can see that my axis is along the Z axis.  any other axis direction always gives me problems.  i believe it has to do with Width height and length not being correctly passed.