Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
At this code i have "Attempted to read or write protected memory.This is often an indicating that other memory is corrupt"

Unsafe codec is in StreamLibrary.dll a referenced c# dll includes unmanaged codes.Which i have download from github and compiled it.https://github.com/AnguisCaptor/StreamLibrary[^]

And this is my code
VB
Dim unsafeCodec As IUnsafeCodec = New UnsafeStreamCodec(80)
       Dim stream As New MemoryStream(10000000)
       Try
           While streamDesktop And client.Connected
               Dim bmp As Bitmap = CaptureDesktop()
               Dim rect As New Rectangle(0, 0, bmp.Width, bmp.Height)
               Dim size As Size = New System.Drawing.Size(bmp.Width, bmp.Height)
               Dim bmpData As BitmapData = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat)
               unsafeCodec.CodeImage(bmpData.Scan0, rect, size, bmp.PixelFormat, stream)'Error at this line
               If stream.Length > 0 Then
                   MsgBox(stream.ToArray.Length)
                   Send("Desktop|", , stream.ToArray)
               End If
               bmp.UnlockBits(bmpData)
               bmp.Dispose()
           End While
       Catch ex As Exception
           MsgBox(ex.Message)
       End Try


How can i fix it ? Thanks
Posted
Updated 17-Feb-17 13:22pm
v3
Comments
Sergey Alexandrovich Kryukov 30-Jun-14 14:17pm    
In what line?
—SA
suleymankenar 30-Jun-14 16:07pm    
Sorry forget to write it it gives at this line
unsafeCodec.CodeImage(bmpData.Scan0, rect, size, bmp.PixelFormat, stream)
Sergey Alexandrovich Kryukov 30-Jun-14 16:24pm    
Good, but, unfortunately, I can only guess what unsafeCodec does and how it works... probably, it does something too unsafe... :-)
—SA
Duncan Edwards Jones 7-Jul-14 12:07pm    
Are you passing bmpData.Scan0 by value, not by reference? (i.e. the address held in Scan0, not the address of the variable Scan0)?

1 solution

Hello,

That's because you don't use stream and bitmpa properly, after a few iterations you'll get this exception because you have a memory leak.

MemoryStream implements
C#
IDisposable
and it is good practice to dispose of it when you have finished using it. One easy way to do this is to use
C#
using


What I would do first is refactor a bit to eliminate the leaks:

Dim unsafeCodec As IUnsafeCodec = New UnsafeStreamCodec(80)
Try
    While streamDesktop And client.Connected
        Using bmp As Bitmap = CaptureDesktop()
            Dim rect As New Rectangle(0, 0, bmp.Width, bmp.Height)
            Dim size As Size = New System.Drawing.Size(bmp.Width, bmp.Height)
            Dim bmpData As BitmapData = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat)
            Using stream As New MemoryStream()
                unsafeCodec.CodeImage(bmpData.Scan0, rect, size, bmp.PixelFormat, stream) 'no more error here.
                If stream.Length > 0 Then
                    MsgBox(stream.ToArray.Length)
                    Send("Desktop|", , stream.ToArray)
                End If
                bmp.UnlockBits(bmpData)
            End Using
        End Using
    End While
Catch ex As Exception
    MsgBox(ex.Message)
End Try


Valery
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900