Hi,
I am attempting to write a library for encrypting image files. It all works
except for one section that creates a digital signature for the file using
RSA. The code itself works, but when called repeatedly in a loop, it uses
increasingly more memory, until where the physical free memory would be
zero, it suddenly clears the memory and after that doesn't have the same
problem. If it doesn't reach zero, the memory isn't reclaimed until the
program is terminated. I have tried calling the garbage collector
explicitly and this doesn't make any difference. The code causing the issue
is:
Private Function SignImage() As Boolean
Dim Signature() As Byte
Dim RSA As RSACryptoServiceProvider
Dim csp As New CspParameters
csp.Flags = CspProviderFlags.UseMachineKeyStore
csp.KeyContainerName = "Test"
RSA = New RSACryptoServiceProvider(csp)
Try
Signature = RSA.SignData(Image, "SHA1")
WriteString("<Signature>")
WriteBytes(Signature)
WriteString("</Signature>" & vbNewLine)
Signature = Nothing
Return True
Catch ex As Exception
Return False
Finally
Signature = Nothing
If Not RSA Is Nothing Then RSA.Clear()
RSA = Nothing
End Try
End Function
If I comment out the line
Signature = RSA.SignData(Image,"SHA1")
, then the problem doesn't occur. I know that the problem isn't caused by
the WriteBytes & WriteString subs (which just write data to a stream) as
these are used in many other places. 'Image' is just a memory stream
containing the image data (for my tests, this is around 1.5MB each time).
Can anyone suggest what is causing this and what I can do to avoid it?
Thanks for any help,
Chris