Results 1 to 3 of 3

Thread: [RESOLVED] Marshal.ReadInt32 -> AccessViolationException

  1. #1

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Resolved [RESOLVED] Marshal.ReadInt32 -> AccessViolationException

    I am getting an AccessViolationError with the following code.

    vb.net Code:
    1. Sub Blend(PosX As Int32, PosY As Int32)
    2.             GetDrawingDimensions(PosX, PosY)
    3.  
    4.             Data = New BitmapData
    5.             CanvasData = New BitmapData
    6.  
    7.             Select Case True
    8.                 Case Me.X < 0 AndAlso Me.Y < 0
    9.                     Bounds = New Rectangle(Math.Abs(Me.X), Math.Abs(Me.Y), WidthToDraw, HeightToDraw)
    10.                     CanvasBounds = New Rectangle(PosX, PosY, WidthToDraw, HeightToDraw)
    11.                 Case Me.X < 0
    12.                     Bounds = New Rectangle(Math.Abs(Me.X), Y, WidthToDraw, HeightToDraw)
    13.                     CanvasBounds = New Rectangle(PosX, Y + PosY, WidthToDraw, HeightToDraw)
    14.                 Case Me.Y < 0
    15.                     Bounds = New Rectangle(X, Math.Abs(Me.Y), WidthToDraw, HeightToDraw)
    16.                     CanvasBounds = New Rectangle(X + PosX, PosY, WidthToDraw, HeightToDraw)
    17.                 Case Else
    18.                     Bounds = New Rectangle(X, Y, WidthToDraw, HeightToDraw)
    19.                     CanvasBounds = New Rectangle(X + PosX, Y + PosY, WidthToDraw, HeightToDraw)
    20.             End Select
    21.  
    22.             Data = TiledImage.LockBits(Bounds, ImageLockMode.ReadOnly, PixelFormat.Format32bppPArgb)
    23.             CanvasData = bmpCanvas.LockBits(CanvasBounds, ImageLockMode.ReadWrite, PixelFormat.Format32bppPArgb)
    24.             For y As Int32 = 0 To HeightToDraw - 1
    25.                 For x As Int32 = 0 To WidthToDraw - 1
    26.  
    27.                     C1 = Color.FromArgb(Marshal.ReadInt32(Data.Scan0, (Data.Stride * y) + (4 * x)))
    28.                     C2 = Color.FromArgb(Marshal.ReadInt32(CanvasData.Scan0, (CanvasData.Stride * (y + Me.Y)) + (4 * (x + Me.X))))
    29.  
    30.                     If Not C1.ToArgb = Alpha.ToArgb Then
    31.  
    32.                         R1 = wA * (C1.R / 8) : G1 = wA * (C1.G / 8) : B1 = wA * (C1.B / 8)
    33.                         R2 = wB * (C2.R / 8) : G2 = wB * (C2.G / 8) : B2 = wB * (C2.B / 8)
    34.  
    35.                         R = Math.Min(31, Math.Floor(R1 + R2)) * 8
    36.                         G = Math.Min(31, Math.Floor(G1 + G2)) * 8
    37.                         B = Math.Min(31, Math.Floor(B1 + B2)) * 8
    38.  
    39.                         Try
    40.                             Marshal.WriteByte(CanvasData.Scan0, (CanvasData.Stride * (y + Me.Y)) + (4 * (x + Me.X)), CByte(B))
    41.                             Marshal.WriteByte(CanvasData.Scan0, (CanvasData.Stride * (y + Me.Y)) + (4 * (x + Me.X)) + 1, CByte(G))
    42.                             Marshal.WriteByte(CanvasData.Scan0, (CanvasData.Stride * (y + Me.Y)) + (4 * (x + Me.X)) + 2, CByte(R))
    43.                         Catch ex As Exception
    44.                             MessageBox.Show(ex.Message)
    45.                         End Try
    46.  
    47.                     End If
    48.                 Next
    49.                 blendProgress.PerformStep(WidthToDraw)
    50.             Next
    51.  
    52.             TiledImage.UnlockBits(Data)
    53.             bmpCanvas.UnlockBits(CanvasData)
    54.         End Sub

    The error happens on the line:
    vb.net Code:
    1. C2 = Color.FromArgb(Marshal.ReadInt32(CanvasData.Scan0, (CanvasData.Stride * (y + Me.Y)) + (4 * (x + Me.X))))

    I have read up on the exception and found out it comes from an invalid pointer. The weird part is the violation only happens sometimes. Even if I put that sub in a loop, it won't happen every pass. Anyone seen another error that may be causing it?
    Prefix has no suffix, but suffix has a prefix.

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Marshal.ReadInt32 -> AccessViolationException

    It means most likely your math is off somewhere and this caused you to read memory that doesn't belong to you. I'd review the math in that procedure to make sure there aren't any off-by-one type errors.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: Marshal.ReadInt32 -> AccessViolationException

    You were absolutely right, I was trying to read memory out of my range. I got it now. Thanks for your response.
    Prefix has no suffix, but suffix has a prefix.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width